Intro Resources Bib Address NQ
 

Averager 1.0

 

Here is the C source code for a program I call "Averager." It's very simple: not only does it average your grades (which any calculator can do), but it lists the number of A's, B's, C's, etc. To modify the values for A, B, C, etc., change the if values in the "gr" test.


/* Averager 1.0
Finds class average and number of A's, B's, etc.
©Steve Harris 1998-99
*/

/************************* PROTOTYPES */

void GetTheAverage(void);
char GetCommand(void);

/************************* MAIN */

#include <stdio.h>
char command;

int main(void)
{
    printf(" Welcome to Averager.\n\n ©Steve Harris 1998-99\n\n");

    while ( (command = GetCommand()) != 'q')
    {
       switch( command)
       {
       case 'q':
       break;
       case 'n':
       GetTheAverage();
       break;
       }
    }

    return 0;
}

/************************* char GetCommand(void)*/

char GetCommand(void)
{
    char command;

    do
    {
       printf("\nEnter command (Q=quit, N=new): ");
       scanf("%c", &command);
    }
    while ( (command != 'q') && (command != 'n'));

    return( command);
}

/*************************** getTheAverage() */

void GetTheAverage(void)
{
    float averg,tot,gr[50];
    int a,b,c,d,f,i,numStu;

    printf("\nHow many students? ");
    scanf("%d",&numStu);
    printf("\n");

    averg=0.00;    /*initialize variables*/
    a=b=c=d=f=tot=0;
   
    for (i=1; i<=numStu; i++)   /*main loop*/
    { printf("Grade of student %d? ", i);
    scanf("%f",&gr[i]);
    tot=tot+gr[i];

    if (gr[i] > 89) a=a+1;     /*checks the grade*/
    if (gr[i] < 60) f=f+1;
    if (gr[i] > 79)
       if (gr[i] < 90) b=b+1;
    if (gr[i] > 69)
       if (gr[i] < 80) c=c+1;
    if (gr[i] > 59)
       if (gr[i] < 70) d=d+1;
    }

    averg=(tot/numStu);

    printf("\n\nClass average is %f", averg);
    printf("\n\tAs \tBs \tCs \tDs \tFs");
    printf("\n\t%d \t%d \t%d \t%d \t%d", a,b,c,d,f);

}