请编写一个函数void fun(int a [],int n),其中a为数组,n为数组a的长度。函数fun()的功能是冒泡排序法将数组a元素按从小到大的顺序排列,实现数组a的升序排列。
注意:部分源程序已存在文件PROC12.cpp中。
请勿修改主函数和其他函数中的任何内容,仅在函数fun()的花括号中填写若干语句。
文件PROC12.cpp的内容如下:
//PROC12. cpp
include <iostream>
using namespace std;
define MAX 100
void fun(int a[],int n);
int main ()
{
int a[MAX],n,i;
cout<<"Please enter the array size n:\n";
do {
cin>>n;
if (n>100)
cout<<"array size flowover! ReEnter a number(0-100)\n";
}while (n>100);
cout<<"Enter the array data:\n";
for (i=0; i<n; i++)
cin>>a [ii;
fun(a[],n);
for (i=0; i<n; i++)
cout<<a [i] <<" ";
cout<<end1;
return 0;
}
void fun(int a[ ],int n)
{
// * * * * * * * *
}
第1题:
若有函数
Void fun( double a[ ],int *n)
{ …… }
以下叙述中正确的是
A)调用fun函数时只有数组执行按值传送,其他实参和形参之间执行按地址传送
B)形参a和n都是指针变量
C)形参a是一个数组名,n是指针变量
D)调用fun函数时将把double型实参数组元素一一对应地传送给形参a数组
第2题:
请编写一个函数int fun(int *s,int t,int *k),用来求出数组的最大元素在数组中的下标并存放在k所指的存储单元中。
例如,输入如下整数:
876 675 896 101 301 401 980 431 451 777
则输出结果为6,980。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
include <conio.h>
include <stdio.h>
int fun(int *s,int t,int *k)
{
}
main()
{
int a[10]={ 876,675,896,101,301,401,
980,431,451,777},k;
clrscr();
fun(a, 10, &k);
printf("%d, %d\n ", k, a[k]);
}
第3题:
【其它】编写程序:设计一个一维数组的排序函数p_sort,并调用它对10个整数进行排序。p_sort函数原型如下: void p_sort(int *p,int n) 其中,p是指向int型一维数组的指针变量,n是数组长度。
第4题:
请编写一个函数fun(),它的功能是:找出一维数组元素中最大的值和它所在的下标,最大值和它所在的下标通过形参传回。数组元素中的值已在主函数中赋予。
主函数中x是数组名,n是x中的数据个数,max存放最大值,index存放最大值所在元素的下标。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
include<stdlib.h>
include<stdio.h>
void fun(int a[],int n, int *max,int *d)
{
}
main()
{
int i, x[20], max, index, n=10;
randomize();
for(i=0; i<=n; i++)
{
x[i]=rand()%50;
printf("%4d",x[i]);
/*输出一个随机数组*/
}
printf("\n");
fun(x,n,&max,&index);
printf("Max=%5d,Index=%4d\n",max,index);
}
第5题:
函数定义为Func(int *p),变量定义为n=100,则下面调用该函数正确的是?
A.Fun(20)
B.Fun(20+n)
C.Fun(n)
D.Fun(&n)