C++设计一个在数组中找最大数的函数findmax, 要求该元素的下标通过参数返回,其地址通过函数值返回

2024-11-22 21:51:48
推荐回答(3个)
回答1:

#include "stdafx.h"
#include //.h
using namespace std;//
int *findthemax(int *pa,int ln,int &spt){
spt=0;
int tmp=pa[spt];
for(int i=0;i if(tmp tmp=pa[i];
spt=i;
}
}
return pa+spt;
}
void main(void){
int n,*pa,subscript;
cout << "Enter the size of the array:";
cin >> n;
if(!(pa=new int[n])){
cout << "Application memory failure...\n";
return;
}
cout << "Enter " << n << " integers(with ' '):\n";
for(int i=0;i> pa[i++]);
cout << "The address is " << findthemax(pa,n,subscript) << endl;
cout << "The max is " << *(pa+subscript) << endl;
delete [n]pa;
}

回答2:

#include
using namespace std;
int* findthemax(int * a,int n,int &max)
{
max=0;
int temp=a[max];
for(int i=0;i {
if(temp {
temp=a[i];
max=i;
}
}
return &a[max];
}
int main()
{
cout<<"input the size of the array:";
int size;
cin>>size;
int a[size];
int themax=0;
for(int i=0;i {
cin>>a[i];
}
void *p=NULL;
p=findthemax(a,size,themax);
cout<<"the max is "< cout<<"the address is "< return 0;
}

回答3:

函数返回值是地址
函数的参数要引用
就是这个意思,ipad不打太多字了
该元素参数前面加上&就行
int findmax(int []a,int &A);
iPad打字不容易啊