Hey I was playing around with 2d arrays and i im trying to find out how to get the average from just one row. im not sure how to do this. here is a 2d array below.
#include <iostream>
usingnamespace std;
int main() {
int mat[4][5]; // matrix can have 4 rows and 5 cols
int i, j;
cout << "Enter the matrix elements row-wise :- " << endl;
for (i = 0; i < 4; i++) { // outer loop iterates over each row
for (j = 0; j < 5; j++) { // inter loop iterates over each column
cout << "mat[" << i << "][" << j << "] : ";
// i -> row no. and j -> col no.
cin >> mat[i][j];
}
}
// display the matrix
cout << "You have entered the matrix :- " << endl;
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++) {
cout << mat[i][j] << " ";
}
cout << endl;
}
return 0;
}
^^ this is almost correct.
all index are 0-n-1 , so its 0,1,2,3,4 not 1,2,3,4,5
but the idea is correct.
also make sure sum1 is a double and consider 5.0 to ensure that it does floating point math instead of integer math, which loses the decimal place.
finally, if you gonna hard code it, put it all in one, there is no reason to do an extra copy. if you need sum1 for something else later, you can keep it your way.