慕后端1351523
2020-07-01 20:37
#include <stdio.h> int total(int score[]) { int sum; int size = sizeof(score); for(int i=0; i<size; i++) { sum += score[i]; } return sum; } int max(int score[]) { int max, item; int size = sizeof(score); for(int i=0; i<size; i++) { item = score[i]; if(item > max) { max = item; } } } int min(int score[]) { int min, item; int size = sizeof(score); for(int i=0; i<size; i++) { item = score[i]; if(item < min) { min = item; } } } int average(int score[]) { return (float)total(score) / sizeof(score); } void sort(int score[]) { int i, temp, flag; int size = sizeof(score); while(flag) { flag = 0; for(i=0; i<size-1; i++) { if(score[i] > score[i+1]) { // 交换两元素 temp = score[i]; score[i] = score[i+1]; score[i+1] = temp; flag = 1; } } } } void printScore(int score[]) { int size = sizeof(score); for(int i=0; i<size; i++) { printf(" %d", score[i]); } printf("\n") } int main() { int score[N]={67,98,75,63,82,79,81,91,66,84}; printf("Test function total(): total(score)=%d\n", total(score)); printf("Test function max(): max(score)=%d\n", max(score)); printf("Test function min(): min(score)=%d\n", min(score)); printf("Test function average(): average(score)=%d\n", average(score)); printf("Test function sort(): sort(score)="); sort(score); printScore(score); return 0; }
hello.c: In function 'total': hello.c:5:22: warning: 'sizeof' on array function parameter 'score' will return size of 'int *' [-Wsizeof-array-argument] int size = sizeof(score); ^ hello.c:2:15: note: declared here int total(int score[]) ^~~~~ hello.c: In function 'max': hello.c:15:22: warning: 'sizeof' on array function parameter 'score' will return size of 'int *' [-Wsizeof-array-argument] int size = sizeof(score); ^ hello.c:12:13: note: declared here int max(int score[]) ^~~~~ hello.c: In function 'min': hello.c:28:22: warning: 'sizeof' on array function parameter 'score' will return size of 'int *' [-Wsizeof-array-argument] int size = sizeof(score); ^ hello.c:25:13: note: declared here int min(int score[]) ^~~~~ hello.c: In function 'average': hello.c:40:40: warning: 'sizeof' on array function parameter 'score' will return size of 'int *' [-Wsizeof-array-argument] return (float)total(score) / sizeof(score); ^ hello.c:38:17: note: declared here int average(int score[]) ^~~~~ hello.c: In function 'sort': hello.c:45:22: warning: 'sizeof' on array function parameter 'score' will return size of 'int *' [-Wsizeof-array-argument] int size = sizeof(score); ^ hello.c:42:15: note: declared here void sort(int score[]) ^~~~~ hello.c: In function 'printScore': hello.c:64:22: warning: 'sizeof' on array function parameter 'score' will return size of 'int *' [-Wsizeof-array-argument] int size = sizeof(score); ^ hello.c:62:21: note: declared here void printScore(int score[]) ^~~~~ hello.c:70:1: error: expected ';' before '}' token } ^ hello.c: In function 'main': hello.c:73:15: error: 'N' undeclared (first use in this function) int score[N]={67,98,75,63,82,79,81,91,66,84}; ^ hello.c:73:15: note: each undeclared identifier is reported only once for each function it appears in
#include <stdio.h> #include <stdlib.h> #define N 10int 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;}
C语言入门
926025 学习 · 20793 问题
相似问题