c - 计算有多少学生通过考试的 C 程序
问题描述
我正在尝试为学校编写一个程序。因此,任务是编写一个程序来计算有多少学生通过了考试。学号未知 (n)。我写了你必须输入成绩和n值的部分,但似乎无法制作计算超过5的学生人数的部分 - 哦,成绩从0开始>> 10,其中 10 是最高分。
这是我到目前为止所拥有的:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i;
//introduecerea notelor
printf("introduceti numarul de studenti care au participat la examen: "); scanf("%d", &n);
int note[n];
for (i=0;i<n;i++){
printf("Studentul %d=", i); scanf("%d", ¬e[i]);
}
//afisarea tuturor notelor
for(i=0;i<n;i++){
printf(" %d", note[i]);
}
//calcularea numarului de studenti promovati
for(i=0;i<n;i++){
printf("%d ", note[i]);
}
getch();
return 0;
}
解决方案
你的意思是这样的:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i;
//introduecerea notelor
printf("introduceti numarul de studenti care au participat la examen: "); scanf("%d", &n);
int note[n];
for (i=0; i<n; i++) {
printf("Studentul %d=", i); scanf("%d", ¬e[i]);
}
//afisarea tuturor notelor
for(i=0; i<n; i++) {
printf("%d ", note[i]);
}
printf("\n");
//calcularea numarului de studenti promovati
for(i=0; i<n; i++) {
printf("%d ", note[i]);
}
printf("\n");
int passedGrades=0;
// We can get total number of students by dividing size of whole array with size of one cell, in this case one cell is 4 bytes
int totalStudents= sizeof(note)/sizeof(note[0]);
// Calculate sum of passed students here
for (size_t i = 0; i < totalStudents; i++) {
if(note[i]>5) {
passedGrades++;
}
}
// And finally print passed students
printf("%d of %d students had higher grade than 5",passedGrades,totalStudents );
//getch();
return 0;
}
推荐阅读
- html - SVG - 旋转时线条变粗
- azure-devops - 在共享配置 IIS 中自动部署机器密钥
- asp.net - 没有重置令牌的 ASP.net Identity 重置密码
- azure-devops - 如何查找 Azure DevOps Wiki 页面的短 URL?
- html - 使用 Bootstrap 4 垂直堆叠最后一个 DIV 以填充/适合容器的其余部分
- php - 插入正确数据时无法登录
- python - 在 PowerPoint 中编辑形状的位置
- python - 当同一行上有多个调用时,如何进入 PDB 中的特定可调用对象?
- c++ - C++ STL - 容器实现
- oracle - Oracle中的“过程名称IS”和“过程名称AS”有什么区别?