//Author : Md Al Masum Bhuiyan #include #include using namespace std; #define N 100 class my_matrix { //variable declaration int max_entries; double matrix[N][N]; double column[N]; double row[N]; public: //functions declaration void set_values (int _max_entries); void set_entries () ; //using prototype double norm1 () ; double norm2 () ; double norm_infinity(); }; void my_matrix::set_values (int _max_entries) { max_entries = _max_entries ; } void my_matrix::set_entries () { //declaring the entries of the 2 dimensional array. for (int i = 0 ; i < max_entries ; ++i ) for (int j = 0 ; j < max_entries ;++j) cin >> matrix[i][j] ; } double my_matrix::norm1() { double max1=0; double norm1 =0; column[max_entries] = 0; for(int i = 0; i < max_entries; ++i){ for( int j = 0; j < max_entries; ++j){ column[j] = column[j] + abs(matrix[i][j]); //sum of absolute column entries } } for(int i = 0; i < max_entries; ++i){ if(column[i]>max1){ max1=column[i]; // the maximum absolute column sum } } return max1; } double my_matrix::norm2() { double norm2 =0; double k = 0; for (int i = 0 ; i < max_entries ; ++ i){ for (int j = 0 ; j < max_entries ;++j){ k +=matrix[i][j]* matrix[i][j]; } } norm2 +=norm2+ sqrt(k); // the square root of the sum of all the squares return norm2; } double my_matrix::norm_infinity() { double max=0; row[max_entries] = 0; for(int i = 0; i < max_entries; ++i){ for( int j = 0; j < max_entries; ++j){ row[i] = row[i] + abs(matrix[i][j]); // sum of row entries } } for(int i = 0; i < max_entries; ++i){ if(row[i]>max){ max=row[i]; // the maximum absolute row sum. } } return max; } int main (){ int n ; my_matrix mat; // creating object cout << " This program calculates the norms of a matrix. " << endl; cout << " Please enter the dimension of the matrix: " ; cin >> n; mat.set_values (n); mat.set_entries(); //calling member functions cout << "norm1 of the matrix = " << mat.norm1() << endl; cout << "norm2 of the matrix = " << mat.norm2() << endl; cout << "norm_inifnity of the matrix = " << mat.norm_infinity() << endl; return 0; }