KSU CS3540 Assignment 2

Assignment 2

Modify the "grades" program so that the initially enters the number of grades and then enters the grades.  The program will output the average of the grades.  Please email your source code to your instructor by Noon on  Thursday, January  16th 

/* Name : Dalibor Labudovic
   Course: CS3540 Sec. 2
   Prof. : J. Higgins
*/

#include <stdio.h>
int main(){
        int grade;
        int num_grades;
                printf("Enter the number of grades: ");
                scanf("%d", &num_grades);
                        int i;
                        for(i = 0; i < num_grades; i++){
                                printf("Enter grade ");
                                scanf("%d", &grade);
                        }
                        //check to see if grade is between 0 and 100
                        if (grade < 0 || grade > 100) printf("Error: Grade should be between 0 and 100");

                if (num_grades == 0)
                        printf("No grades to average\n");
                else {
                int sum = 0;
                int j;
                for (j = 0; j < num_grades; j++)
                        sum += grade;
                        float ave = (float)sum/num_grades;
                        printf("average: %f\n",ave);
                }
        return 0;
}
 

Comments

Popular posts from this blog

CS3150 Assignment 1

CS4500 Test 4 Study Guide

CS4150 Assignment 2