Norm of a Matrix C++

Hey guys!

I started to learn C + + on my own and I would like to ask for some help about an exercise that I got from a site and I couldn't find a solution. :(

The site asks you to calculate the Euclidean norm of any matrix and then show it to the user.
First thing I tried to do, was ask the user for the number of rows and columns of the matrix, but I think the way I did the numbers are not being stored.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main() { 
   int i,j,m,n,matrix[10][10],sum=0;
   double norm;
   cout << "Number of rows:" ;
   cin>>m;
   cout<<"Number of columns:";
   cin>>n;

   for ( int i = 0; i < m; i++ ) {
	for ( int j = 0; j < n; j++ ) {
	   cout<<"Nummber["<<i<<"]["<<j<<"]: ";
	   cin>>matrix[i][j];
   }} 
       for ( int i = 0; i < m; i++ ) {
	for ( int j = 0; j < n; j++ ) {
	   cout<<"Number["<<i<<"]["<<j<<"]: "<<matrix[i][j];
     }}
}


Second, I created a variable to store the sum of the array elements. So the program can calculate the norm.

1
2
3
4
5
6
7
sum=0;
for i=1 :m
for j=1... n (?)
sum= a(i,j)^2 + sum
end
end
norm=sum^(1/2)
What makes you think the numbers are not being stored?
Can someone check if I'm doing something wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cmath>
using namespace std;

int main() { 
   int i,j,m,n,a[10][10],sum=0,trace=0;
   float norm;
   cout << "Number of rows:" ;
   cin>>m;
   cout<<"Number of columns:";
   cin>>n;

   for ( int i = 0; i < m; i++ ) {
	for ( int j = 0; j < n; j++ ) {
	   cout<<"Element["<<i<<"]["<<j<<"]: ";
	   cin>>a[i][j];
   }} 
       for ( int i = 0; i < m; i++ ) {
	for ( int j = 0; j < n; j++ ) {
	   sum=sum+(a[i][j]^2);
	   if(i==j)
	   trace=trace+a[i][j];
}
}
norm=sqrt(sum);
cout<<""<<norm<<"";
}
Last edited on
Well, the obvious thing is that you're defining your matrix as being 10 x 10, but you're not checking the user-inputted dimensions to make sure they're not bigger than 10.
Also, according to the references at this site:
http://www.cplusplus.com/reference/cmath/sqrt/
there's no sqrt function that takes an int.
Hey MikeyBoy,

I'm really sorry because I just started to look for exercises and I saw some examples that the person creates a matrix just for testing, maybe I'm getting confuse with it, coz I want to make a matrix with the size determined by the user.
Btw, thanks again to correct me, I changed the way to use the function for that model u sent me.
I'm really lost but I'll keep trying :D
I did some changes, and I guess it's working now. Can u tell me if I'm getting into the right direction?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <cmath>

using namespace std;

int main(){ 
        int i,j,n,m,a[10][10],sum=0;
        double norm;
        
   cout << "Number of rows:";
   cin >> m;
   cout << "Number of columns:";
   cin >> n;


for (int i = 0; i < m; i++) {
	for (int j = 0; j < n; j++) {
	   cout << "Element["<<i<<"]["<<j<<"]: ";
	   cin >> a[i][j];
   }
 } 

cout << "\n Original Matrix is:";
for(i=0;i<n;i++){
  cout << "\n";
for(j=0;j<n;j++)
  cout << "\t"<<a[i][j]<<"";
}

for(i=0;i<n;i++)
for(j=0;j<m;j++){
  sum+=(a[i][j] * a[i][j]);
}

norm=sqrt((double)sum);

cout << "\n Result of norm: "<<norm<<"";

    system("PAUSE");
    return EXIT_SUCCESS;
}
Well, is it working the way you expect it to work? Are you getting the output you expect?
I guess so, but the problem is that is not working for matrixes with different numbers of mxn. Like 3x2
Topic archived. No new replies allowed.