fork download
  1. #include <stdio.h>
  2. #define N 40
  3. int ReadScore(int score[]); /* ReadScore()函数原型 */
  4. int FindMax(int score[], int n); /* FindMax()函数原型 */
  5. int main(void)
  6. {
  7. int score[N], max, n;
  8. n = ReadScore(score); /*调用函数ReadScore()输入成绩,返回学生人数*/
  9. printf("Total students are %d\n",n);
  10. max = FindMax(score, n); /*调用函数FindMax()计算最高分,返回最高分*/
  11. printf("The highest score is %d\n",max);
  12. return 0;
  13. }
  14. /* 函数功能:输入学生某门课的成绩,当输入负值时,结束输入,返回学生人数 */
  15. int ReadScore(int score[]) /* ReadScore()函数定义 */
  16. {
  17. int i = -1; /*i初始化为-1,循环体内增1后可保证数组下标从0开始*/
  18. do{
  19. i++;
  20. printf("Input score:");
  21. scanf("%d", &score[i]);
  22. } while (score[i] >= 0); /* 输入负值时结束成绩输入 */
  23. return i; /* 返回学生人数 */
  24. }
  25. /* 函数功能:计算最高分 */
  26. int FindMax(int score[], int n)/* FindMax()函数定义 */
  27. {
  28. int max, i;
  29. max = score[0]; /* 假设score[0]值为当前最大值 */
  30. for (i=1; i<n; i++)
  31. {
  32. if (score[i] > max) /* 若score[i]值较大 */
  33. {
  34. max = score[i]; /* 则用score[i]值替换当前最大值 */
  35. }
  36. }
  37. return max; /* 返回最高分 */
  38. }
  39.  
Success #stdin #stdout 0s 544KB
stdin
10 10 -10 10
stdout
Input score:Input score:Input score:Total students are 2
The highest score is 10