请帮我看看我的C++程序为什么得不到我想要的答案

来源:百度知道 编辑:UC知道 时间:2024/06/02 21:57:12
#include<iostream>
using namespace std;
void print(int largest);
int main()
{
int test[3][3];
int row, col;
int a;
cout<<"enter the data:";
cout<<endl;
for(row=0;row<3;row++)
{
for(col=0;col<3;col++)
cin>>test[row][col];
}

for(row=0;row<3;row++)
{
for(col=0;col<3;col++)
cout<<test[row][col]<<" ";
cout<<endl;
}

print (a);
system("PAUSE");
return 0;
}

在print函数中并没有将主程序的test数组传递进来,而在函数内部定义的数组test[3][3]是另外一回事。
应该取消函数中的test[3][3]定义,将函数改为:
void print(int test[][]) 将数组变量传递进函数中去。

我记得是可以调试的,一步步的调试,再哪一步出了错会有提示,好久没玩这东西了

print (a);

void print(int largest)
{
int row,col;
int test[3][3];
for (row=0;row<3;row++)
{
largest=test[row][0];
for(col=1;col<3;col++)
if(largest<test[row][col])
largest=test[row][col];

cout<<"The largest in row"<<row+1<<"="<<largest<<endl;
}
}

这里a是一个指针变量,而你函数里定义的形式参数为一个整形变量。
前面应该没出问题。
你修改下这里应该就OK了。

错了