'2D array to find sum of columns in a matrix, "using uninitialized local variable

#include <iostream>
#include <iomanip>
using namespace std;

void mArray(int a[3][3])
{
    int row, col;
    cout << "enter row :";
    cin >> row;
    cout << "enter column";
    cin >> col;
    cout << "enter elements";
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            cin >> a[i][j];
        }
    }
    cout << endl << endl;
    cout << "matrix is: " << endl;
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            cout << a[i][j] << " " << endl;
        }
        cout << endl;
    }
    cout << endl << endl;
}
void print(int a[3][3])
{
    int row, col, sum = 0;
    cout << "sum of all column:" << endl;
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            sum = sum + a[j][i];
        }
        cout << "sum of column is" << sum << endl;
        sum = 0;

    }
}
int main()
{
    int a[3][3];
    mArray(a);
    print(a);
}

I'm trying to make a 2D array matrix that will let you enter numbers and then add up the columns and rows. However, I am getting an error saying that the variables "col" and "row" are uninitialized. But when I try to set them as 0, the sum won't add. Is there any way I can fix this?



Solution 1:[1]

Within this function

void print(int a[3][3])
{
    int row, col, sum = 0;
    cout << "sum of all column:" << endl;
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        //...         

you are using uninitialized variables row and col in for loops.

Also this function definition

void mArray(int a[3][3])
{
    int row, col;
    cout << "enter row :";
    cin >> row;
    cout << "enter column";
    cin >> col;
    cout << "enter elements";
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        //...

does not make a sense because the parameter a is declared as an array with 3 columns but within the function the number of columns can be different due to these prompts

    cout << "enter row :";
    cin >> row;
    cout << "enter column";
    cin >> col;

Instead of the array that can have different number of rows and columns you need to use the container std::vector<std::vector<int>> because C++ does not support variable length arrays.

Otherwise you will need to allocate dynamically array of arrays.

Solution 2:[2]

Below code Works Fine.

#include <iostream>
#include <iomanip>
using namespace std;

void mArray(int *a,int row,int col)
{
    // int row, col;
    cout << "enter row :";
    cin >> row;
    cout << "enter column :";
    cin >> col;
    cout << "enter elements : "<<endl;
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            scanf("%d", ((a+i*col) + j));
        }
    }
    cout << endl << endl;
    cout << "matrix is: " << endl;
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            cout << *((a+i*col) + j) << " ";
        }
        cout << endl;
    }
    cout << endl << endl;
}
void print(int *a,int row,int col)
{
    int sum = 0;
    cout << "sum of all column:" << endl;
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            sum = sum + *((a+i*col) + j);
        }
        cout << "sum of column is : " << sum << endl;
        sum = 0;

    }
}
int main()
{
    int a[3][3];
    mArray((int *)a, 3, 3);
    print((int *)a, 3, 3);
}

But better way to write this Programe is.

#include <iostream>
#include <iomanip>
using namespace std;

void mArray(int *a,int row,int col)
{
    cout << "enter elements : "<<endl;
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            scanf("%d", ((a+i*col) + j));
        }
    }
    cout << endl << endl;
    cout << "matrix is: " << endl;
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            cout << *((a+i*col) + j) << " ";
        }
        cout << endl;
    }
    cout << endl << endl;
}
void print(int *a,int row,int col)
{
    int sum = 0;
    cout << "sum of all column:" << endl;
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            sum = sum + *((a+i*col) + j);
        }
        cout << "sum of column is : " << sum << endl;
        sum = 0;

    }
}
int main()
{
    int m, n;
    cout << "enter row :";
    cin >> m;
    cout << "enter column :";
    cin >> n;
    int a[m][n];
    mArray((int *)a, m,n);
    print((int *)a, m, n);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Bhautik Sudani