用于计算通过考试人数的C程序

时间:2018-12-06 10:20:00

标签: 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", &note[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;
}

2 个答案:

答案 0 :(得分:0)

您只需要在第三个循环中添加一个支票

int numPassing = 0;
for(i = 0; i < n; i++) {
    if(note[i]>=5) {
        numPassing++;
    } /* if */
} /* for */
printf("%d passed the exam!\n", numPassing);

答案 1 :(得分: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", &note[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;
}