Write a program that reads 7 scores as input and outputs the average. The program should use an array of
floats. Note that you need to determine the highest and the lowest score and exclude them in the computation.
/*
*C program that reads 7 scores as input and outputs the average. The program uses an array of floats.
*It determines the highest and lowest score and excludes them in computation.
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
// declaring scores array
float scores[7];
float maxScore = 0;
float minScore = 0;
float totalScore = 0;
printf("Enter scores in order from the first to seventh\n");
int i; //For controlling the loops
for (int i = 0; i < 7; i++)
{
// Reading User Input scores value Based on index
printf("Enter score %d :", i + 1);
scanf("%f", &scores[i]);
}
for (int i = 0; i < 7; i++)
{
//Get max score in the array
maxScore = scores[0]; //Initialize maxScore with first element
if(scores[i] > maxScore)
{
maxScore = scores[i];
}
//Get minimum score in the array
minScore = scores[0]; //Initialize minScore score with first element
if(scores[i] < minScore)
{
minScore = scores[i];
}
totalScore = totalScore += scores[i];
}
//Get average of the 7 subjects excluding the biggest and smallest score element in the array
float average = (totalScore - (minScore + maxScore))/7;
printf("The average score excluding the first and seventh score is: %f", average);
return 0;
}
Comments
Leave a comment