May 14, 2013 at 9:29pm UTC
Hi! I recently began learning arrays and I was wondering if someone was able to help me with the question below.
Declare a two dimensional 5 by 10 array and initialize its values to random numbers between 1 and 100.
a) Print the content of the array row by row i.e. each row on a separate line.
b) Find the sum of all the elements
c) Print the average for the array
d) Find the row with the largest sum
e) Print the array row by row starting from the bottom row
here's my code:
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
srand(time(0));
int aray [5][10];
for (int row=0; row<5; row++)
for (int col=0; row<10; col++)
aray[row][col]=rand()%100+1;
for (int row=0; row<5; row++){
for (int col=0; row<10; col++){
cout<<aray[row][col]<<endl;
cout << endl;
int largestsum=0;
int rowsum=rowsum+aray[row][col];
cout<<"the sum of all elements=" <<rowsum<<endl;
int average=rowsum/aray[col][row];
cout<<"the average" <<average;
if (rowsum>largestsum)
largestsum=rowsum;
cout<<"largest sum" <<endl;
}
}
for (int row=4; row>=0; row--)
for (int col=9; col>=0; col--)
cout<<aray[row][col]<<endl;
I'm not sure where I went wrong, I really appreciate those who would take their time to help me. :)
Last edited on May 14, 2013 at 9:37pm UTC
May 14, 2013 at 9:35pm UTC
Put your code in code brackets so it shows up properly with line numbers etc, tell us what errors you're getting and on what lines.
May 14, 2013 at 9:38pm UTC
It just does not compile the code, I followed the instructions but I'm not sure where is wrong..
Last edited on May 14, 2013 at 10:50pm UTC
May 14, 2013 at 10:52pm UTC
i'm trying to teach arrays to myself cuz my teacher isn't very good at explaning and right now im not sure where im wrong with this code
May 14, 2013 at 11:14pm UTC
xsxs wrote:It just does not compile the code, I followed the instructions but I'm not sure where is wrong..
if it's a compiler error it would help if you would include the whole code so we can compile it.
xsxs wrote:i'm trying to teach arrays to myself cuz my teacher isn't very good at explaning and right now im not sure where im wrong with this code
in my experience most teachers arn't good at explaining stuff. There are some good ones out there though.
for (int col=0; row<10; col++){
I think you won't this
for (int col=0; col<10; col++){
int rowsum=rowsum+aray[row][col];
I don't think this will compile because rowsum isn't initialized.
int average=rowsum/aray[col][row];
i think you wan't this
int average=rowsum/col;
I couldn't compile the code so I can't guarantee I'm right
I would avoid declaring variables inside of loops its error prone
Last edited on May 15, 2013 at 9:39pm UTC
May 15, 2013 at 8:42pm UTC
ok, thanks for your suggestions but i have tried and it my code still doesn't work so i dont know where my actual problem is at :'(