二维数组查找矩阵中的列总和,“使用未初始化的局部变量”

发布于 01-18 08:51 字数 1273 浏览 3 评论 0 原文

#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);
}

我正在尝试制作一个二维数组矩阵,它可以让您输入数字,然后将列和行相加。但是,我收到一条错误消息,指出变量“col”和“row”未初始化。但是当我尝试将它们设置为 0 时,总和不会相加。有什么办法可以解决这个问题吗?

#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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖微凉心微凉 2025-01-25 08:51:14

在此函数中,

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++)
        //...         

您在 for 循环中使用未初始化的变量 row 和 col。

此外,此函数定义

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++)
        //...

没有意义,因为参数 a 被声明为具有 3 列的数组,但在函数内,由于这些提示,列数可能会有所不同,

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

而不是可以具有不同行数和列数的数组需要使用容器 std::vector> 因为 C++ 不支持变长数组。

否则,您将需要动态分配数组数组。

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.

那请放手 2025-01-25 08:51:14

下面的代码正常工作。

#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);
}

但是编写此预绘的更好方法是。

#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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文