Hi all, I have been working on this problem for quite some time now, and can't seem to find the errors in my code. The problem is: Write a program to total each individual row and each individual column of a 2d 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
I haven't gotten to the random number generator part yet, as I would like to get down the adding rows and columns down first. I am getting some errors when adding rows and columns. Thanks!
When asked to do two things at once, it is often useful to first program them individually. After that you can see where the two routines share commonality.
tl;dr
Write a program to total only the rows.
Then write a program to total only the columns.
Compare and contrast the two programs.
BTW, to create your table, structure it like this:
9 10 11 12 13
int table[ROWS][COLUMNS] =
{
{ 1,2,3,4,5,6 },
};
Now just select the entire line (line 11) and copy it nine times. ;^)
First thing: names have power. Different names do not solve any issue, but they might help to see the issues:
1 2 3 4 5 6
for ( int row = 0; row < ROWS; row++ ) {
for ( int col = 0; col < COLUMNS; col++ ) {
rowSum += table[ row ][ col ];
colSum += table[ col ][ row ]; // Does the table have ROWS columns?
}
}
1 2 3 4 5
int rowSum; // uninitialized, unknown value
rowSum += 7; // unknown+7 is still unknown
int colSum {0}; // initialized with known value
colSum += 42; // 0+42 == 42
The rowSum should be 0 before the first element of a row. You keep adding to same sum from all rows.
You cannot know the sum of a column before you have added elements from every row. That means after the loop, on line 23.
You have COLUMNS columns. You cannot store all sums in one int. You need an array.
You can use loops to show the sums. It is the calculation that is limited to two nested loops.
@lastchance
most of the code you putted there is new to me , what is ostream I know it stands for outputStream but you rreturned a value and that means its a printer?
at line 23 what did you do ?
auto & row : v.
that's some advanced c++ skills . :D
I could write it in a different way and I was about to but what you did there attracted me , could you please explain what you did?
ostream is ANY output stream. I was defining the operation << for a particular numerical array type called a valarray. The operation returns a reference to a stream so that multiple outputs can be chained together in a single statement. It is defined purely for convenience here.
auto is used where the compiler can identify the type itself and avoids having to explicitly write out a complicated type. If you have used Python you will know that that language can assign appropriate types without you being explicit about it: this is the nearest that c++ has to offer. The & is used (here) as a "reference" or "view" on the variable to avoid an expensive copy of the potentially large array row. It's used for a lot of other things as well, which is potentially confusing.
I used a valarray because it has useful little member functions like sum(), apply() and whole-array operations that are built into many other languages and I regard as "natural". It is good for numerical arrays. Some compilers (notably Intel) provide considerable optimisation for it.
There is nothing here that couldn't be done with a standard array, however. It's just that would take more lines of code and explicit loops.