我无法让我的简单程序将 2D 数组传递给函数
i can't get my simple program to pass a 2d array to a function
我尝试了几种方法,但无法通过引用获得函数"Matrixinputs"来接受2d数组。"Matrixinputs"将把数组的输入更改为用户选择的值。我是一个初学者,但我认为这与我试图传递一个参数由用户定义的动态数组有关,但这只是猜测。请帮忙,我的错误像这些
matrix2.C:15:11: error: invalid operands of types ‘int [(((sizetype)(((ssizetype)a) + -1)) + 1)][(((sizetype)(((ssizetype)b) + -1)) + 1)]’ and ‘int [(((sizetype)(((ssizetype)c) + -1)) + 1)][(((sizetype)(((ssizetype)d) + -1)) + 1)]’ to binary ‘operator*’
cour<<m1*m2;
或
$ g++ matrix.C -omatrixs -lm
matrix.C: In function ‘int main()’:
matrix.C:16:25: error: expression list treated as compound expression in initializer [-fpermissive]
这是我的代码
#include <iostream>
#include <cmath>
using namespace std;
//Prototypes
double Matrixsettings(int&, int&, int&, int&);
int Matrixinputs(int&, int &);
int main()
{
int a=2, b=2, c=2, d=2;
cout<< Matrixsettings( a,b,c,d);
cout<< a<<b<<c<<d;
int m1 [a] [b], m2 [c] [d];
cout<<m1 [a] [b]<< m2 [c] [d];
int Matrixinputs(m1 [a] [b],m2 [c] [d]);
return 0;
}
double Matrixsettings( int &a, int &b, int &c, int &d)
{
cout<< "how many rows in the first matrix: ";
cin>> a;
cout<< "how many columns in the first matrix: ";
cin>> b;
cout<< "how many rows in the second matrix: ";
cin>> c;
cout<< "how many columns in the second matrix: ";
cin>> d;
return 0;
}
int Matrixinputs(m1& [a] [b],m2& [c] [d]);
{
//this function will have a loop with cout and cin line defining each input of the matrix like array array
}
void Matrixinputs(int* matrix, int row, int column);
{
//this function will have a loop with cout and cin line defining each input of the matrix like array array
for (int i=0; i<row; i++)
{
for (int j=0; j<column; j++)
{
cin >> matrix[i*row+column];
}
}
}
在此函数之外,手动将内存定位到矩阵指针。
使用向量很方便,但这是使用new的一个示例。
#include <iostream>
using namespace std;
template <typename T>
T **make_2d(int r, int c){
T** array_2d = new T*[r];
for(int i=0;i<r;++i)
array_2d[i] = new T[c];
return array_2d;
}
template <typename T>
void drop_2d(T** matrix, int r){
for(int i=0;i<r;++i)
delete[] matrix[i];
delete[] matrix;
}
typedef int** Matrix;
void MatrixInputs(Matrix &m, int r, int c){
for(int i = 0;i < r; ++i){
for(int j = 0; j < c;++j){
cout << "[" << i << "][" << j << "] >";
cin >> m[i][j];
}
}
}
int main(){
int a,b;
cin >> a;
cin >> b;
Matrix m = make_2d<int>(a, b);
MatrixInputs(m, a, b);
//print
for(int i=0;i<a;++i){
cout << "[ " ;
for(int j=0;j<b;++j){
cout << m[i][j] << " ";
}
cout << "]" << endl;
}
drop_2d(m, a);
}
相关文章:
- 2D数组来自文本输入,中间有空格
- 如何使用用户输入在C++中正确填充2D数组
- 如何在C++中检查2D数组中负值的输入验证
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 如何将以逗号和空格分隔的整数读取到 2D 数组中?
- 如何在C++函数中声明静态 2D 数组?
- 如何声明一个可以在整个程序中使用的全局 2d 3d 4d .. 数组(堆版本)变量?
- 我已经阅读了很多关于 2d 数组的信息,但我在作业中使用它时遇到了麻烦
- 使用矢量将文本文件中的输入存储到 2D 数组中
- 获取 2D 数组 c++ 中的所有数字对
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 如何在构造函数中使用初始值设定项设置具有相同值的 2d 数组?
- 在 Python 和 c++ 2d 数组初始化之间.这是怎麽?为什么呢?
- 如何打印 2D 字符数组C++
- 如何将数组 2d 作为参数从函数传递并返回数组 2d 此函数
- 数组 2D 多个值
- 创建数组数组 (2D),而无需在 Java 中初始化内部数组
- 如何在C++中将文本文件传输到数组2D
- 从同一类的另一个方法调用数组2D-C++
- 获取地址在数组2d中的位置