The results for the sum of the rows is correct, but the sum for the columns gives wrong results for a 3 x 4 array

Viewed 64

I am trying to make a program where the sum of the rows and columns are counted individual, the rows turn out fine, but the columns is giving weird results. The correct result is suppose to look like this (example Matrix)

9 1 2 4
2 2 8 0
3 3 3 3
Sum of row 0 is 16
Sum of row 1 is 12
Sum of row 2 is 12
Sum of column 0 is 14
Sum of column 1 is 6
Sum of column 2 is 13
Sum of column 3 is 7

But mine comes out as

Sum of row 0 is 16
Sum of row 1 is 12
Sum of row 2 is 12
Sum of column 0 is 565359518
Sum of column 1 is 32772
Sum of column 2 is 13
Sum of column 3 is 7
#include <iostream>

using namespace std;

int main()
{

    int value[3][4];
    int sum = 0;
    int row, col;
    int i, j;

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
            cout << "Enter a value : " << endl;
            cin >> value[i][j];
        }
    }
    cout << "Value of the array: " << endl;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
            cout << value[i][j] << " ";
        }

        cout << endl;
    }

    void row_sum(int value[i][j]);
    {
        int i, j, sum = 0;

        for (i = 0; i < 3; i++) {
            for (j = 0; j < 4; j++) {

                sum = sum + value[i][j];
            }

            cout
                << "Sum of the row "
                << i << " is " << sum
                << endl;

            sum = 0;
        }
        void col_sum(int value[i][j]);
        {
            int i, j, sum = 0;

            for (i = 0; i < 4; i++) {
                for (j = 0; j < 4; j++) {

                    sum = sum + value[j][i];
                }

                cout
                    << "Sum of the column "
                    << i << " is " << sum
                    << endl;

                sum = 0;
            }
        }

        return 0;
    }
}

(Originally it was i = 3, but i changed it to 4 because I thought that was what was wrong with the code, but the column still gives weird results.)

1 Answers

I'm gonna do something I shouldn't, but you've put enough effort into this that I want to show you where the issues are with an answer that works:

#include <iostream>

using namespace std;

// Use Constants to declare the size of your array to make it easier to iterate in loops.
// Also allows the compiler to know at compile-time the array sizes of functions.
const int ColSize = 4;
const int RowSize = 3;


// Forward declare functions here so that main() knows what they are.
void col_sum(int value[RowSize][ColSize]);
void row_sum(int value[RowSize][ColSize]);

int main()
{
    // Replace those nasty magic number 3 and 4s here with constants
    int value[RowSize][ColSize];
    // Notice I have removed all the unused vars here (i,j,sum, etc.)

    // Replace magic numbers
    for (int i = 0; i < RowSize; i++) {
        for (int j = 0; j < ColSize; j++) {
            cout << "Enter a value : " << endl;
            cin >> value[i][j];
        }
    }

    cout << "Value of the array: " << endl;

    // Replace magic numbers
    for (int i = 0; i < RowSize; i++) {
        for (int j = 0; j < ColSize; j++) {
            cout << value[i][j] << " ";
        }

        cout << endl;
    }

    // Actually call the functions...
    row_sum(value);
    col_sum(value);

    return 0;
}

// Make actual functions to be called using our constants.
void row_sum(int value[RowSize][ColSize])
{
    // Notice I have removed all the repeated vars here (i,j,sum, etc.)
    // They are all declared within the for loops

    // Replace magic numbers
    for (int i = 0; i < RowSize; i++) {

        int sum = 0;  // Moved the sum 0 here as we only need it once.
        for (int j = 0; j < ColSize; j++) {
            sum += value[i][j];
        }

        cout
            << "Sum of the row "
            << i << " is " << sum
            << endl;

    }
}

void col_sum(int value[RowSize][ColSize])
{
    // Notice I have removed all the repeated vars here (i,j,sum, etc.)
    // They are all declared within the for loops

    // Replace magic numbers
    for (int i = 0; i < ColSize; i++) {
        int sum = 0;  // Moved the sum 0 here as we only need it once.
    
        for (int j = 0; j < RowSize; j++) {
            sum = sum + value[j][i];
        }

        cout
            << "Sum of the column "
            << i << " is " << sum
            << endl;
    }
}

Notice how the constants make it much easier to change the size of the for loops, manage memory, and make it easier to read the code. Also see how functions are declared and used.

Related