首页 > 解决方案 > 计算有多少学生通过考试的 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;
}

标签: c

解决方案


你的意思是这样的:

#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;
}

推荐阅读