// A program that will store five students' marks in an array
// It will output all the marks for verification
// It will also calculate and report the average mark
// Marc Chee 12/03/2019
#include <stdio.h>
int main (void) {
int marks[5] = {0};
int counter;
// commented out the manual entry to keep it as a reference
/* // manually input marks
marks[0] = 55;
marks[1] = 84;
marks[2] = 72;
marks[3] = 44;
marks[4] = 59; */
// Ask the user to input the 5 marks
printf("Please enter five separate marks.\n");
counter = 0;
while(counter < 5) {
scanf("%d", &marks[counter]);
counter++;
}
// loop through array and print out values
counter = 0;
while(counter < 5) {
printf("%d\n", marks[counter]);
counter++; // counter = counter + 1
}
// loop through the array and accumulate the values into a total
int total = 0;
counter = 0;
while(counter < 5) {
total += marks[counter];
counter++;
}
printf("Total of all marks is %d\n", total);
// calculate and output the average
double average = total/5.0;
printf("The average mark was: %lf\n", average);
return 0;
}
Resource created Tuesday 12 March 2019, 03:31:43 PM.
file: arraysLec.c