写出下面程序的运行结果,并分析原因。
#include
using namespace std;
void f (int &m, int n)
{ int temp;
temp=m;
m=n;
n=temp;
}
int main()
{
int a=5,b=10;
f(a,b) ;
cout<<a<<" "<<b<<endl;
return 0;
}
要传地址才能交换成功,n没有传地址,改为&n,代码如下:
void f (int &m, int &n)
{
int temp;
temp=m;
m=n;
n=temp;
}
void f (int &m, int n) //n前面少了 &
应该是void f (int &m, int &n)
你题目的解答代码如下:(如有帮助,望采纳!谢谢! 点击我这个回答右上方的【采纳】按钮)
#include <iostream>
using namespace std;
void f (int &m, int &n) //n前面少了 &
{ int temp;
temp=m;
m=n;
n=temp;
}
int main()
{
int a=5,b=10;
f(a,b) ;
cout<<a<<" "<<b<<endl;
return 0;
}
void f (int &m, int &n)