Write a C++ program to total each individual row and each individual column of a
ID: 3697330 • Letter: W
Question
Write a C++ program to total each individual row and each individual column of a two dimensional array consisting of 10 rows and 6 columns using a single nested loop to accomplish this task. Use a random number generator to store integers in the range of 1 to 10 in the array. Functions are not required for this exercise. Below is an example of a 10 by 6 array displaying the required output that should be generated by the program.
NOTE: This is an example only.
1 + 2 + 3 + 4 + 5 + 6 = 21
2 + 4 + 6 + 7 + 5 + 6 = 30
3
4
5
6 More values not included
7
8
9
10
-----
55
Explanation / Answer
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
int a[10][6];
int row[10],col[6];
srand (time(NULL));
for(int i=0;i<10;i++)
{
row[i]=0;
if(i<6)col[i]=0;
for(int j=0;j<6;j++)
{
a[i][j]=rand()%10+1;
}
}
for(int i=0;i<10;i++)
{
for(int j=0;j<6;j++)
{
row[i]+=a[i][j];
col[j]+=a[i][j];
}
}
for(int i=0;i<10;i++)
{
cout<<row[i]<<' ';
}
cout<<endl;
for(int i=0;i<6;i++)
{
cout<<col[i]<<' ';
}
cout<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.