Summing Numbers in grid rows

I have to make a grid 40 X 40 with random numbers 0-9. I have already done this and it prints out great. My problem is I need to be able to choose a row number and output the sum of the number in that row. I have no clue how to do this. Here is what I have so far. Can anyone help??

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
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;

int main()

{

	
	int Values[40][40];
	int rows, cols;

	for(rows = 0; rows<40; rows++)
		for (cols = 0; cols<40; cols++)
			Values[rows][cols] = rand () % 10;
	
	

	for(rows = 0; rows<=39; rows++)
	{
		cout<<'\n';
		for (cols = 0; cols<=39; cols++)
		
		{
		cout<<Values[rows][cols];
		cout<<" ";
		}
	
	}

	
	
	int R, C;
	cout<<"\n Which row (0-39) would you like to sum?\n";
		cin>> R;


		cout<<'\n';
		cout<<"The sum of the numbers in row "<<R<<" is "<<C;
	
	system ("pause"); 
}
are you dealing with random variables or you have imputed the numbers yourself..
35
36
37
38
    int sum = 0;
    for (cols = 0; cols<40; cols++)
        sum += Values[R][cols];    
    cout<<"The sum of the numbers in row "<< R<<" is "<< sum << endl;

your program in not working with random variables....you haven't declared the random variable directive
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()

{



int Values[40][40];
int rows, cols;

for(rows = 0; rows<40; rows++)
for (cols = 0; cols<40; cols++)
Values[rows][cols] = (rand () % 10) ;



for(rows = 0; rows<=39; rows++)
{
cout<<'\n';
for (cols = 0; cols<=39; cols++)

{
cout<<Values[rows][cols];
cout<<" ";
}

}



int R, C;
do
{
cout<<"\n Which row (0-39) would you like to sum?\n";
cin>> R;


cout<<'\n';
int sum = 0;
for (cols = 0; cols<40; cols++)
sum += Values[R][cols];
cout<<"The sum of the numbers in row "<< R<<" is "<< sum << endl;
} while (R!=0);
return 0;
}
alfredokang wrote:
your program in not working with random variables

Who are you directing that comment to?

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
you didnt add those directives to the program...Due to that it couldnt run in codeblocks...
these directives;

#include <cstdlib>
sorry, AbstractionAnon...am very sorry...
Topic archived. No new replies allowed.