Code #include <iostream> #include <iomanip> #include <stdlib.h> #include <conio.h> #include <time.h> #include <locale.h> #define N 5 float niz=0, verh=0, ch=0, sredz=0; using std::cout; using std::endl;
void maxmat (float a[N][N]) { float temp=0; for (int i=0;i<N;i++) for (int j=0;j<N;j++) if (temp < a[i][j]) temp = a[i][j]; cout << "Максимум матрицы = "<<temp << endl; }
void minmat (float a[N][N]) { float temp=a[0][0]; for (int i=0;i<N;i++) for (int j=0;j<N;j++) if (temp > a[i][j]) temp = a[i][j]; cout << "Минимум матрицы = " << temp << endl; }
void maxnizmat (float a[N][N]) {ch=0; float sum=0; float temp=a[1][0]; for (int i=0;i<N;i++) for (int j=0;j<N;j++) if (i>j) {sum +=a[i][j];if (temp < a[i][j]) {temp = a[i][j]; ch++; niz += temp;}} niz /= ch; cout << "Максимум нижнетреугольной матрицы = " << temp << endl; cout << "Сумма = " << sum << endl; }
void maxverhmat (float a[N][N]) { ch=0; float sum=0; float temp=a[0][1]; for (int i=0;i<N;i++) for (int j=0;j<N;j++) if (j>i) { sum +=a[i][j]; if (temp < a[i][j]) { temp = a[i][j]; ch++; verh += temp; } } verh /= ch; cout << "Максимум верхнетреугольной матрицы = " << temp << endl; cout << "Сумма = " << sum << endl; }
void minnizmat (float a[N][N]) { float temp=a[1][0]; for (int i=0;i<N;i++) for (int j=0;j<N;j++) if (i>j) if (temp > a[i][j]) temp = a[i][j]; cout << "Минимум нижнетреугольной части матрицы = " << temp << endl; }
void minverhmat (float a[N][N]) { float temp=a[0][1]; for (int i=0;i<N;i++) for (int j=0;j<N;j++) if (j>i) if (temp > a[i][j]) temp = a[i][j]; cout << "Минимум верхнетреугольной части матрицы = " << temp << endl; }
void minglav (float a[N][N]) { float temp=a[0][0]; for (int i=0;i<N;i++) for (int j=0;j<N;j++) if (i==j) if (temp > a[i][j]) temp = a[i][j]; cout << "Минимум главной диагонаи = " << temp << endl; }
void maxglav (float a[N][N]) { float temp=a[0][0]; for (int i=0;i<N;i++) for (int j=0;j<N;j++) if (i==j) if (temp < a[i][j]) temp = a[i][j]; cout << "Максимум главной диагонали = " << temp << endl; }
void minvtor (float a[N][N]) { float temp=a[0][4]; for (int i=0;i<N;i++) for (int j=0;j<N;j++) if (i+j==i) if (temp > a[i][j]) temp = a[i][j]; cout << "Минимум второстепенной диагонали = " << temp << endl; }
void maxvtor (float a[N][N]) { float temp=a[0][4]; for (int i=0;i<N;i++) for (int j=0;j<N;j++) if (i+j==j) if (temp < a[i][j]) temp = a[i][j]; cout << "Максимум второстепенной диагонали = " << temp << endl; }
void sred (float a[N][N]) { float temp=0; for (int i=0;i<N;i++) for (int j=0;j<N;j++) temp += a[i][j]; sredz=temp / (N*N); cout << "Среднеарифметическое значение элементов матрицы = " << sredz << endl; cout << "Среднеарифметическое значение элементов нижнетреугольной части = " << niz << endl; cout << "Среднеарифметическое значение элементов верхнетреугольной части = " << verh << endl; }
void sumstr (float a[N][N]) {float sum=0;
for (int i=0; i<N; i++) { float min=a[0][0]; for (int j=0; j<N; j++) {sum += a[i][j]; if (min > a[i][j]) min = a[i][j];} cout << "Строка " << i << "; Сумма = " << sum << endl; cout << "Среднеарифметическое = " << sum/N << endl; cout << "Минимум = " << min << endl; } }
void sumstol (float a[N][N]) {float sum=0; for (int i=0; i<N; i++) { float min=666666; for (int j=0; j<N; j++) {sum += a[j][i]; if (min > a[j][i]) min = a[j][i];} cout << "Столбец " << i << "; Сумма = " << sum << endl; cout << "Среднеарифметическое = " << sum/N << endl; cout << "Минимум = " << min << endl; } }
void searchelem () { }
void main() { setlocale(LC_ALL,"Rus"); float m[N][N]; int i,j; srand((unsigned) time(NULL)); using std::setw; using std::setprecision; for (i=0;i<N;i++) { for (j=0;j<N;j++) { m[i][j]= rand()/10.; cout << setw(8) << setprecision(5) << m[i][j]; } cout << endl; }
minmat(m); maxmat(m); maxnizmat (m); maxverhmat (m); minnizmat (m); minverhmat (m); minglav (m); maxglav (m); minvtor (m); maxvtor (m); sred (m); sumstr (m); sumstol (m);
system ("pause"); } Код немного сыроват, но, на 90% работает правильно. В этом примере вы не увидите высокотехнологичных приёмов ООП, а увидите смесь Си++ с процедурным программированием. Функция main не очень сложна, в ней мы создаём двумерный массив (размером NxN, где N мы задаём с помощью директивы define). Кириллицу конечно же не забываем, приятно лицезреть русские слова на экране. srand((unsigned) time(NULL)); - данная строчка позволяет нам генерировать случайные числа всегда разные, т.к. мы используем системное время. Заполняем массив значениями и выводим их на экран. Далее идёт выполнение функции описанных выше main'а. Функции довольно информативные, считаю, что их описание излишне. Если что-то не понятно пишите в комментариях, может пожелания какие будут.
|