二维数组查找矩阵中的列总和,“使用未初始化的局部变量”
#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 时,总和不会相加。有什么办法可以解决这个问题吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(2)
那请放手 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);
}
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
在此函数中,
您在 for 循环中使用未初始化的变量 row 和 col。
此外,此函数定义
没有意义,因为参数 a 被声明为具有 3 列的数组,但在函数内,由于这些提示,列数可能会有所不同,
而不是可以具有不同行数和列数的数组需要使用容器
std::vector>
因为 C++ 不支持变长数组。否则,您将需要动态分配数组数组。
Within this function
you are using uninitialized variables row and col in for loops.
Also this function definition
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
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.