C++ 如何让用户使用C+;中的函数在数组中输入值+;?
好的,我有这段代码让用户在数组中输入数字,但我想把它变成一个函数。我是新的C++,我不知道该怎么办。有人能给我一些提示吗?我将不胜感激。代码如下:C++ 如何让用户使用C+;中的函数在数组中输入值+;?,c++,function,multidimensional-array,user-input,C++,Function,Multidimensional Array,User Input,好的,我有这段代码让用户在数组中输入数字,但我想把它变成一个函数。我是新的C++,我不知道该怎么办。有人能给我一些提示吗?我将不胜感激。代码如下: main() { int m; int n; int i; int j; int A[100][100]; m = get_input (); n = get_input2 (); cout << endl << "Enter positive integers for the elements of m
main()
{
int m;
int n;
int i;
int j;
int A[100][100];
m = get_input ();
n = get_input2 ();
cout << endl << "Enter positive integers for the elements of matrix A:" << endl;
for (i = 0 ; i < m ; i++)
for (j = 0 ; j < n ; j++)
cin >> A[i][j];
return;
}
main()
{
int m;
int n;
int i;
int j;
INTA[100][100];
m=获取_输入();
n=get_input2();
coutvoid初始化(int x[][],int m,int n){
cout>m
//n=get_input2();//您必须具有该方法或cin>>n
coutm;
coutn;
初始化(A,m,n);
返回;
}
重点是需要new
数组的内存,而不是在堆栈上定义它,然后释放它。传入维度,返回new
ed数据,完成后调用delete[]
。(除非你还想在分配的数组中传递数据),在这种情况下,你也可以使用局部变量。)它必须是一个数组。在C++中,习惯用法是使用一个STD::vector。你想对数组做些什么?除了[cc][]之外,你还需要做任何其他访问元素吗?在C++中避免删除通常更好。
void initialize(int x[][], int m, int n){
cout << endl << "Enter positive integers for the elements of matrix A:" << endl;
for (int i = 0 ; i < m ; i++)
for (int j = 0 ; j < n ; j++)
cin >> x[i][j];
}
main()
{
int A[100][100],m,n;
// m = get_input ();// you must have that method or cin>>m
//n = get_input2 (); //you must have that method or cin>>n
cout<<"Enter no of rows";
cin>>m;
cout<<"Enter no of columns";
cin>>n;
initialize (A,m,n);
return;
}