#include <stdio.h>
int getIndex(int arr[5],int value)
{
int i;
int index;
for(i=0;i<5;i++)
{
/* 请完善数组查询功能 */
if(arr[i]==value)
{
index=i;
}
}
return index;
}
int main()
{
int arr[5]={3,12,9,8,6};
int value = 8;
int index = getIndex(arr , value); //这里应该传什么参数呢?
if(index!=-1)
{
printf("%d在数组中存在,下标为:%d\n",value,index);
}
else
{
printf("%d在数组中不存在。\n",value);
}
return 0;
}
你的index没有赋初始值的,如果不满足if的话,index不是-1,自然就满足index!=-1,程序就错了
#include <stdio.h>
#include <stdlib.h>
#define N 10
int cmp(const void *a,const void *b){
return *(int*)b - *(int*)a;
}
int Sum(int score[]){
int s = 0,i;
for(i = 0 ; i < N ; i++)
s += score[i];
return s;
}
int Max(int score[]){
return score[0];
}
int Min(int score[]){
return score[N-1];
}
double Avg(int s){
return (s + 0.0) / N;
}
int main()
{
int score[N]={67,98,75,63,82,79,81,91,66,84};
int i,sum;
sum = Sum(score);
qsort(score,N,sizeof(int),cmp);//sort it
printf("Total score : %d\n",sum);
printf("Max score : %d\n",Max(score));
printf("Min Score : %d\n",Min(score));
printf("Average score : %f\n",Avg(sum));
printf("\n成绩排序:\n");
for(i = 0 ; i < N ; i++)
{
printf("Rank %d's score : %d\n",i+1,score[i]);
}
return 0;
}