In C, Write A Function That Will Read A Collection Of Examination Scores

Problem Statement

Write a function that will read a collection of examination scores ranging in value from 1 to 100. Your subprogram should count and print the number of scores in the outstanding category ( 90 – 100), the number of scores in the satisfactory category ( 60-89), and the number of scores in the unsatisfactory category ( 1-59).

#include<stdio.h>

int read(int[], const int);
void stats(int scores[], const int);

int main(){
    const int MAX_LEN = 20;
    int scores[MAX_LEN];
    const int size = read(scores, MAX_LEN);
    stats(scores, size);
    return 0;
}

int read(int scores[], const int LEN){
    int i = 0, n;
    do{
        if(LEN <= i)break;
        printf("Enter score (-1 to stop): ");
        if(scanf("%d", &n)){
            if(-1 == n)break;
            if(1 > n || 100 < n){
                printf("Error: Invalid score!\n");
                continue;
            }
            scores[i++] = n;
        }
    }while(1);
    return i;
}

void stats(int scores[], const int SIZE){
    int out = 0, sat = 0, dis = 0;
    for(int i = 0; SIZE > i; i++){
        if(90 <= scores[i] && 100 >= scores[i])out++;
        else if(60 <= scores[i] && 89 >= scores[i])sat++;
        else dis++;
    }
    printf("\nCategory        No# of Scores\n");
    printf("------------------------------\n");
    printf("Outstanding     %d\n", out);
    printf("Satisfactory    %d\n", sat);
    printf("Un-Satisfactory %d\n", sat);
}
Download

Comments

Popular posts from this blog