大城小爱歌曲王力宏:请教一条C++的程序题目,高手请进!

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/03 19:16:19
设计函数求一整形数组的最小元素及其下标.在主函数中定义和初始化整形数组,调用该函数,并显示最小元素值和下标值.

#include <iostream>
using namespace std;
void MyArray(int[],int);
int main()
{
int arr[]={12,7,5,2,6,4,15,36,78};/*在主函数中定义数组*/
int len=sizeof(arr)/sizeof(int);/*计算数组中元素个数*/
MyArray(arr,len);/*调用函数*/

system("Pause");
return 0;
}
void MyArray(int a[],int b)/*选择数组中最小数字下标的函数*/
{
int m,n;
m=0;
n=b-1;
for(m;m<b-1;m++)
if (a[m]<a[n])/*满足条件下标交换*/
n=m;

cout<<"最小的数字为:"<<a[n]<<endl;
cout<<"最小数的下标是:"<<n<<endl;
}

随便写的一个,调试通过了,希望对你有所帮助。