第十三周项目三(函数版成绩处理)

素颜马尾好姑娘i 2022-08-14 02:46 203阅读 0赞
  1. 问题及代码:
  2. /*
  3. *Copyright (c) 2014,烟台大学计算机学院
  4. *ALL right reserved
  5. *文件名;seventyeight.cpp
  6. *作者;童宇
  7. *完成日期2014年11月23日
  8. *版本号v1.0
  9. *问题描述:函数版成绩处理
  10. *输入描述:
  11. *程序输出:
  12. */
  13. #include <iostream>
  14. #include <cmath>
  15. using namespace std;
  16. double ave;
  17. void input_score(int s[], int n);
  18. int get_max_score(int s[], int n);
  19. int get_min_score(int s[], int n);
  20. double get_avg_score(int s[], int n);
  21. double get_stdev_score(int s[], int n);
  22. int count(int x, int s[], int n); //返回在数组s中n名同学中有多少人得x分(实参给出最高/低时,可以求最高/低成绩的人数)
  23. void output_index(int x, int s[], int n); //在函数中输出数组s中n名同学中得x分的学号(下标)
  24. int main(void)
  25. {
  26. int score[50]; //将score设为局部变量,通过数组名作函数参数,传递数组首地址,在函数中操作数组
  27. int num;//小组人数也设为局部变量,将作为函数的实际参数
  28. int max_score,min_score;
  29. cout<<"小组共有多少名同学?";
  30. cin>>num;
  31. cout<<endl<<"请输入学生成绩:"<<endl;
  32. input_score(score, num);
  33. max_score=get_max_score(score, num);
  34. cout<<endl<<"最高成绩为:"<<max_score<<",共有 "<<count(max_score, score, num )<<" 人。";
  35. min_score=get_min_score(score, num);
  36. cout<<endl<<"最低成绩为:"<<min_score<<",共有 "<<count(min_score,score, num )<<" 人。";
  37. cout<<endl<<"平均成绩为:"<<get_avg_score(score, num);
  38. ave=get_avg_score(score, num);
  39. cout<<endl<<"标准偏差为:"<<get_stdev_score(score, num);
  40. cout<<endl<<"获最高成绩的学生(学号)有:";
  41. output_index(max_score,score, num);
  42. cout<<endl<<"获最低成绩的学生(学号)有:";
  43. output_index(min_score,score, num);
  44. cout<<endl;
  45. return 0;
  46. }
  47. void input_score(int s[], int n)
  48. {
  49. int i,c;
  50. for(i=0; i<n; i++)
  51. {
  52. cin>>c;
  53. if(c>=0&&c<=100)
  54. {
  55. s[i]=c;
  56. }
  57. else
  58. cout<<"请正确输入成绩!";
  59. }
  60. }
  61. int get_max_score(int s[], int n)
  62. {
  63. int max,i;
  64. for(i=0; i<n; i++)
  65. {
  66. if(max<s[i])
  67. max=s[i];
  68. }
  69. return max;
  70. }
  71. int get_min_score(int s[], int n)
  72. {
  73. int min,i;
  74. for(i=0; i<n; i++)
  75. {
  76. if(min>s[i])
  77. min=s[i];
  78. }
  79. return min;
  80. }
  81. double get_avg_score(int s[], int n)
  82. {
  83. int i,sum=0,ave;
  84. for(i=0; i<n; i++)
  85. {
  86. sum=sum+s[i];
  87. }
  88. ave=sum/n;
  89. return ave;
  90. }
  91. double get_stdev_score(int s[], int n)
  92. {
  93. int i;
  94. double s1,s2,s3,s4;
  95. for(i=0; i<n; i++)
  96. {
  97. s1=(s[i]-ave);
  98. s2=s1*s1;
  99. }
  100. s3=s2/n;
  101. s4=sqrt(s3);
  102. return s4;
  103. }
  104. int count(int x, int s[], int n)
  105. {
  106. int i,j=0;
  107. for(i=0; i<n; i++)
  108. {
  109. if(s[i]==x)
  110. j++;
  111. }
  112. return j;
  113. }
  114. void output_index(int x, int s[], int n)
  115. {
  116. int i;
  117. for(i=0; i<n; i++)
  118. {
  119. if(x==s[i])
  120. cout<<i<<" ";
  121. }
  122. }
  123. 运行结果:
  124. <img src="https://img-blog.csdn.net/20141123183828420?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTA2MDkwNg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

发表评论

表情:
评论列表 (有 0 条评论,203人围观)

还没有评论,来说两句吧...

相关阅读

    相关

    2019春第一次课程设计实验报告 一、 实验项目名称 飞机子弹射击敌机 二、 实验项目功能描述 由用户来对飞机的位置进行操作,通过控制飞机的位置来射击目标。