52.2k views
3 votes
Using a variable length array, write a C program that asks the user to enter test scores.Then, the program should calculate the average, determine the lowest test score, determine the letter grade, and display all three.

1 Answer

4 votes

Answer:

Here is the C program :

#include<stdio.h> // to use input output functions

int main(){ //start of main() function body

int n; //to store the number of tests taken

int test_scores[n], i; //a variable length array test_score

float sum=0,average; //to store the sum and average of test scores

int lowest; //to store the lowest test score

printf("Enter the number of tests taken :");//prompts user to enter number of test scores

scanf("%d",&n); / / reads the value of n from user

printf("Enter test scores: "); //prompts user to enter test scores

for(i=0; i<n; i++) {

scanf("%d",&test_scores[i]); //read the values of input test scores

sum = sum + test_scores[i]; } //calculates the sum of the test scores by adding the values of test scores

average=sum/n; // compute the average by dividing sum of all test scores with the total number of test scores

printf("Average is %.2f",average); //displays the average

printf("\\Grade is "); // prints Grade is

if (average >= 90) { //if value of average is greater than or equal to 90

printf("A"); } //print the grade letter A

else if(average >= 80 && average < 90) { //if value of average is greater than or equal to 80 and less than 90

printf("B"); } //print the grade letter B

else if(average>60 && average<80){ //if value of average is between 60 and 80

printf("C"); } //print the grade letter C

else if(average>40 && average<=60) { //if value of average is greater than 40 and less than or equals to 60

printf("D"); } //print the grade letter D

else { //if the value of average is below 40

printf("F"); } //print the grade letter F

lowest = test_scores[0]; //lowest points to the 1st element of test scores means the first input test score

for (int j = 1; j < n;j++) { //loop iterates through the scores

if (test_scores[j] < lowest) { // if the element at j-th index position of test_scores array is less than the element stored in the lowest variable

lowest = test_scores[j]; } } //then assign that element value of test_score to the lowest

printf("\\Lowest test score is: %d.\\", lowest); } //displays the lowest test score

Step-by-step explanation:

The program is well explained in the comments mentioned with each statement of the code.

The program prompts the user to enter the number of test scores as they are not already specified in the array because array test_scores is a variable length array which means its length is not fixed. The program then prompts the user to enter the test scores. The program then adds all the test scores and store the result in sum variable. Then it computes the average by dividing the value in sum variable to the number of test scores n. Then in order to determine the letter Grade the average value is used. The if else conditions are used to specify conditions in order to determine the Grade. Next the lowest score is determined by setting the value of lowest variable to the first element of the test_scores array. Then using for loop, the index variable j moves to each array element i.e. score and determines if the the value of element is less than that stored in the lowest variable. If the value positioned at j-th index of test_scores is less than that of lowest than this value is assigned to lowest. At last the lowest holds the minimum of the test scores.

Using a variable length array, write a C program that asks the user to enter test-example-1
Using a variable length array, write a C program that asks the user to enter test-example-2
User Yevhen Kuzmovych
by
4.7k points