C++ getting the average from just one line

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.
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
#include <iostream>
using namespace 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;

}
Last edited on
Thanks i was wondering how to do that.
treat the row as a one-d array and average it like that... somerow here is just a constant

for(index 0 .. < 5) total+= mat[somerow][index];
total/= 5;
Last edited on
something like this?

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
#include<iostream>
using namespace std;

int main() {
    int mat[4][5]; // matrix can have 5 rows and 4 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;
    }
    {
        double average1, sum1;
        sum1 = (mat[0][1] + mat[0][2] + mat[0][3] + mat[0][4] + mat[0][5]);
        average1 = sum1 / 5;

        cout << "the average in row 1 is \t" << average1 << endl;
    }
Last edited on
is that right?? I feel like it is not.
sum1 = (mat[0][1] + mat[0][2] + mat[0][3] + mat[0][4] + mat[0][5]);
average1 = sum1 / 5;

^^ 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.

all that ends up making it like this:

sum1 average1 = (mat[0][0] + mat[0][1] + mat[0][2] + mat[0][3] + mat[0][4]) / 5.0;
Last edited on
jonnin. thank you that makes sense. i will mark this as solved.
Topic archived. No new replies allowed.