Thread: Calculate and print the average of several grades entered by the user.

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    2

    Calculate and print the average of several grades entered by the user.

    I am trying to write a C program that calculates and prints the average of several grades entered by the user.The program should first prompt the user to enter the number of grades to process (at least 2grades must be entered by user!) .It should then prompt the user for each grade. As each grade is entered, the program should ensurethat the grades entered are in the grade range of zero to 100. If the grade is not in that range, anerror message should be displayed (as shown below), and the user should be re-prompted for agrade. Any bad grades entered should not be included in the average calculation.Once all values are entered, the program should calculate and print the average of all of the positivevalues entered, rounded to the nearest whole number.At the end display a message for the appropriate letter grade as shown here:90 - 100: A80 - 89: B70 - 79: C60 - 69: D 0 - 59: F

    Here is my code but the problem is that it keeps asking for more than 4 grades, I dont know how to make it stop at 4.

    Code:
    #include <stdio.h>
     
    
    
    int main()
    {
    
    
    int grade, num, i; 
    float sum=0,avg;
    char letter[2];
    do{ 
    printf ("This program caluculates the average of as many grades you wish to enter.\n\n");
    printf("\nFirst, enter the number of grades to process: ");
    scanf ("%i", &grade);
    printf ("Now enter the %i grades to be averaged.\n", grade);
    
    
    if(num<2)
    printf("\nEnter atleast 2 grades Enter again: ");
    }while(num<2);
    
    
    for(i=0;i<=num;i++)
    { 
    do{
    printf("\nEnter grade #%i:",i+1); 
    scanf("%d",&grade);
    if(grade <0 || grade >100)
    {
    printf("\n*** Invalid entry. grade must be 0 to 100. ***:");
    i--;
    } 
    }while(grade <0 && grade >100);
    
    
    }
    for(i=0;i<=num;i++)
    sum=sum+grade;
    avg=sum/num;
    printf("\nThe average of the %d letter entered is %.2f",num,avg);
    
    
    if(avg>=90 && avg<=100)
    printf(letter,"A");
    else if(avg >=80 && avg<=89)
    printf(letter,"B");
    else if(avg >=70 && avg<=79)
    printf(letter,"C");
    else if(avg >=60 && avg<=69)
    printf(letter,"D");
    else if(avg >=0 && avg<=59)
    printf(letter,"F");
    printf("\nYou have a letter grade of %s\n",letter); 
    return 0;
    }

  2. #2
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Code:
    do{
    printf ("This program caluculates the average of as many grades you wish to enter.\n\n");
    printf("\nFirst, enter the number of grades to process: ");
    scanf ("%i", &grade);
    printf ("Now enter the %i grades to be averaged.\n", grade);
     
     
    if(num<2)
    printf("\nEnter atleast 2 grades Enter again: ");
    }while(num<2);
    In your if and while statements - what value does the num variable have?

  3. #3
    Registered User
    Join Date
    Oct 2016
    Posts
    2
    I am almost done with my code, I need only a selection structure (if, or if else) statement to check for baddata values.
    this is my code so far:
    Code:
    #include <stdio.h>int main(){
        int n, i;
        float num[100], sum=0.0, average;
        printf ("This program caluculates the average of as many grades you wish to enter.\n");
        printf("\nFirst, enter the number of grades to process: ");
        scanf("%d",&n);
        printf ("\nNow enter the %d grades to be averaged.\n\n", n);
    for(i=0; i<n; ++i)
       {
          printf("Enter grade #%i:",i+1);
          scanf("%f",&num[i]);
          sum+=num[i];
       }
      
       average=sum/n;
       printf("\nThe average of the 4 grades entered is %.2f\n\n",average);
       
    if(average>=90 && average<=100)
    printf("\nYou have a letter grade of A ");
    else if(average >=80 && average<=89)
    printf("\nYou have a letter grade of B ");
    else if(average >=70 && average<=79)
    printf("\nYou have a letter grade of C ");
    else if(average >=60 && average<=69)
    printf("\nYou have a letter grade of D ");
    else if(average >=0 && average<=59)
    printf("\nYou have a letter grade of F ");
     
    getch ();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-09-2012, 12:31 PM
  2. How to calculate grades using Linked Lists
    By TampaTrinDM88 in forum C Programming
    Replies: 8
    Last Post: 07-13-2009, 09:08 AM
  3. C program using structs to calculate grades
    By TampaTrinDM88 in forum C Programming
    Replies: 4
    Last Post: 07-06-2009, 12:33 PM
  4. use user entered formula to calculate something
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 12-07-2001, 02:21 AM

Tags for this Thread