Bayaz Bayaz - 1 month ago 6
C++ Question

C++ For loops and multidimensional arrays

So I have an assignment for my C++ class. Basically we have to create a 3x3 multidimensional array, calculate the sum of rows, sum of columns, sum of diagonal values and sum of anti-diagonal values, I usually just input 1 2 3 4 5 6 7 8 9 as values as a starting point.

Now, I'm not trying to be rude but my teacher is not very good, we basically spend 2 hours on a single problem without her doing much explaining. Other than that I started with C++ Primer and Programming: Principles and Practice Using C++ so I believe I'll be able to learn a lot on my own.

Anyhow my questions are probably quite stupid, but if anyone feels like helping, here they are:


  1. My /**/ commented for loop for anti-diagonal value gives me a wrong sum. I'm assuming it has something to do with the nature of for-loops or I just type it out wrong, but I didn't figure it out yet.



2.The teacher's solution for calculating the anti-diagonal values is the following:

for (i = 0; i < row_num; ++i)
for (j = 0; j < col_num; ++j)
if (i + j == row_num - 1)
anti-diagonal += A[i][j];


How is it different from my approach? I believe mine is simpler and works better.

3.In line:

int sumRows[row_num] = { 0 };


Why do {} have to be used? Our teacher didn't bother with explaining that. I tried without {} but I got an error.

Here is the full code for my version:

#include "../../std_lib_facilities.h"
#include <iostream>
using namespace std;

#define row_num 3 //no. of rows
#define col_num 3 //no. of columns

int main()
{
int i = 0;
int j = 0;
int diagonal = 0;
int antidiagonal = 0;

int sumRows[row_num] = { 0 };
int sumCol[col_num] = { 0 };

int A[row_num][col_num];

//Input to matrix
for(i=0; i<row_num; i++)
for (j = 0; j < col_num; j++)
{
cout << "A[" << i << "]" << "[" << j << "]: ";
cin >> A[i][j];

sumRows[i] += A[i][j];
sumCol[j] += A[i][j];
}

cout << endl;

//Print out the matrix
for (i = 0; i < row_num; i++)
{
for (j = 0; j < col_num; j++)
cout << A[i][j] << '\t';
cout << endl;
}

//prints sum of rows
for (i = 0; i < row_num; i++)
cout << "Sum of row " << i + 1 << " "<< sumRows[i] << endl;

//prints sum of columns
for (j = 0; j < row_num; j++)
cout << "Sum of column " << j + 1 << " " << sumCol[j] << endl;

//Sum of diagonal values
for (i = 0; i < row_num; i++)
diagonal += A[i][i];

//Sum of antidiagonal values
for (i = 0, j = 2; i < row_num, j >= 0; i++, j--)
antidiagonal += A[i][j];

/*for(i=0; i<row_num; i++)
for (j = 2; j >= 0; j--)
{
antidiagonal += A[i][j];
}
*/
cout << "\nSum of diagonal values: " << diagonal << endl;
cout << "Sum of antdiagonal values: " << antidiagonal << endl;

return 0;
}

Answer

1) Your commented out loop sums all values, not just those along the antidiagonal.

2) It is different from your approach in that it will iterate over every value in the matrix, but then it will only add to the total if it detects it is in one of the appropriate cells. Your solution only iterates over the appropriate cells and doesn't have to evaluate any ifs, so it will be more efficient. However, you need to change your loop condition to i < row_num && j >= 0. Using a comma here will discard the result of one of the checks.

3) int sumRows[row_num] = { 0 }; initializes the whole sumRows array with 0's.