Please, how to call element array from 2dd array??

Hello!
Please, is there a way to CALL element of theat array?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#include<iostream>
#include<stdlib.h>
using namespace std;
int main(){
srand (time(NULL));


int m[5][3];


for (int i=0;i<5;i++){
     for (int j=0;j<3;j++){
              m[i][j]=rand()% 10;
              cout<<m[i][j]<<" ";
              
                             }
}

return 0;
}


  




That way we generate 2DD array consisting of 5 simple arrays (with random numbers as elements).

How can we CALL some of the arrays and , for example, sum all the elemnets of one (horizontal), simple array?

Many thanks!
Last edited on
So to sum a horizontal of m[][]:
1
2
3
4
5
6
7
 
int sum = 0, i; 
for(i = 0; i < 3; ++i) //Iterate through the horizontal elements of row 0. 
{
    sum = sum + m[0][i]; //Add this horizontal to the sum. 
}
std::cout << sum; //Print the sum  


To sum a vertical of m[][]:
1
2
3
4
5
6
7
 
int sum = 0, i; 
for(i = 0; i < 5; ++i)//Iterate through verticals of column 0. 
{
    sum = sum + m[i][0]; //This time we add the verticals. 
}
std::cout << sum; 


To access a specific element:
1
2
3
 
//Remember that index starts at 0. 
std::cout << m[2][1]; //Print element from column 3, row 2.  


Does this answer your question?
Many thanks!!!
Topic archived. No new replies allowed.