简答题 C语言
题号 #2257001

请编写一个函数int proc(int*s,int t,int*k),用来求出数组的最小元素在数组中的下标并存放在k所指的存储单元中。
例如,输人如下整数:
273 934 453 624 89 342 111 831 527 246
则输出结果为4,89。
注意:部分源程序给出如下。
请勿改动main()函数和其他函数中的任何内容,仅在函数proc()的花括号中填入所编写的若十语句。
试题程序:
#include<stdlib.h>
#include<conio.h>
#include<stdio.h、>
void proc(int*S,int t.int*k)
{

}
void main()
{
int arr[10]={273,934,453,624,89,342,111,
831,527,246},K:
system("CLS"):
proc(arr,10,&k):
printf("%d,%d\n",k,arr[k]);
}

进入在线模考

参考答案

暂无

答案解析

int proc(int*s,int t,int*k)
{
int i,j;
j=0;J//是为了求得最小数的下标的
for(i=0;i<t;i++)
if(s[j]>s[i]) //从if中,我们可知当后面的数比
前面小时,把小的元素的下标放到j中
j=i:
*k=j; //最后把最小元素的下标赋给*k,使其
带回到主函数中
}
【解析】要求出数组P最小的数,需要比较数组中的每一个元素。将最小的元素的下标存放在变量k中,通过形参返回到主函数中.

你可能感兴趣的试题

3

学生的记录由学号和成绩组成,M名学生的数据已在主函数中放入结构体数组stu中,请编写函数[)roc(),它的功能是:把指定分数范围内的学生数据放在b所指的数组中,分数范围内的学生人数由函数值返回。
例如,输入的分数是70~79,则应当把分数在70~79 的学生数据进行输出,包含70分和79分的学生数据。主函数中把70放在low中,把79放在heigh中。
注意:部分源程序给出如下。
清勿改动main()函数和其他函数中的任何内容,仅在函数proc的花括号中填入所编写的若干语句。
试题程序:
#include<stdio.h>
#define M 16
typedef struet
{  char num[10];
int s;
}
STREC:
int proc(STREC*a,STREC*b,int 1,int h)
{

}

void main()
{
STREC stu[M]={{"GA005",85},{"GA003",
76},
{"GA002",69},{"GA004",85),{"GA001",96),
{"GA007",72),{"GA008",64),{"GA006",87},
{"GA015",85},{"GA013",94),{"GA012",64),
{"GA014",91},{"GA011",9O),{"GA017",64),
{"GA018",64),{"GAOl6",72));
STREC h[M];
int i,n,low,heigh,t;
printf("Enter 2 integer number low&
heigh:");
scanf("%d%d",&low,&heigh);
if(heigh<low)
{t=heigh;heigh=low;low=t;)
n=proc(stu,h,loW,heigh):
printf("The student’S data between
%d--%d:\n",low,heigh);
for(i=0;i<n;i++)
printf("%s%4d\n",hi-i].num,h[i].s);
//输出指定分数范围内的学生记录
printf("\n"):
}

找试题 · 找答案 · 找资料 在线模考