You should stick to the same topic. It keeps the confusion down and all the answers in one place.
You have the code correct, but you have your subscripts backwards. This is the same code, but this may make it easier to understand:
1 2 3 4 5 6 7 8 9 10 11 12 13
cout << "Enter row index 0-3:" << endl;
cin >> row;
s=0;
for (col=0; col < 4; col++)
{
s=s+a[row][col];
// or
// s += a[row][col];
}
// you may want to revise the next statement to better explain what the output is.
cout<<"The sum of column "<< row <<" numbers is "<< s;
cout<<endl;
Unless your idea is to add together only part of the columns.
As you can see row never changes its value which is what you want to add each column.
Hey Andy, his problem comes from out of range indexing.
CASH: in memory this is how your array is stored
0 2 3 4
5 6 7 8
9 3 5 2
so there are 3 rows and 4 columns.
1 2 3 4 5 6 7 8 9
//for(i=0;i<4;i++) ///you should use the outer not the inner loop
// { ///when i==3 you are out of range
// s=s+a[i][index];
// }
for(int i=0; i<3; i++)///the correct loop, "each col contains 3 elements"
{
s+=a[i][index];
}
also you create a space between line 8-9 and move line 12 - 14 there.
Thanks for the catch. I had the other "row" sum in mind when I started and did not see everything until I had made two posts. Then I had time to really look at the whole code.