Reputation: 21
Good efternoon ,
I'm working on a matrix of number (2D array) and I want to know how to automatically obtain the sum of the elements of each line and their average. the file construct is as follows:
14 25 65 41 24 85 69
14 52 87 56 42 35 47
25 64 89 21 56 7 45
15 42 8 7 65 47 7
I used the following prtion of code:
for(i=0; i<7; i++){
for (j=0; j<4; j++){
double sum(0);
sum+=M[i][j];
average=sum/7;}}
but I don't get what I asked for. Any correction, please?!
Upvotes: 0
Views: 83
Reputation: 3178
You have 4 arrays of 7 elements.
So for each array, set sum
to zero. for each element in the array, add it to sum
. then calculate the average.
for(i=0; i<4; i++){
double sum(0);
for (j=0; j<7; j++){
sum+=M[i][j];
}
average=sum/7;
}
Upvotes: 1