Intro to programming: (I am new to programming)!! Using an array that holds a mi
ID: 3862470 • Letter: I
Question
Intro to programming:
(I am new to programming)!!
Using an array that holds a minimum of 10 grades, write a program that accepts letter grades (A-thru-F) until the array is full or the user enters an “escape value” (e.g., Q, X, etc.). The program will then calculate an overall GPA. The program will need to convert letter grades into “grade scores”.
‘A’ 4
‘B’ 3
‘C’ 2
‘D’ 1
‘F’ 0
If the entry is not a valid grade, the program should print an error message and the grade should not be included in the GPA (i.e., only use valid letter grades). Print out the letter grades, their associated “grade score”, and the GPA (to two decimal places).
Example:
A – 4
A – 4
B – 3
A – 4
C – 2
Your GPA, based on 5 letter grades, is 3.40 Implementation Constraints
1. You need to use a loop to accept the input.
2. You need to use a loop to calculate the GPA
Explanation / Answer
// C code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
char grades[10], grade;
int scores[10];
int size= 0, i;
double gpa = 0;
// loop to accept the input.
for (i = 0; i < 10; ++i)
{
printf("Enter letter grade: ");
scanf(" %c",&grade);
if( (grade < 'A' || grade > 'F') || grade == 'E' )
{
printf("Invalid grade ");
break;
}
grades[i] = grade;
if(grades[i] == 'A')
scores[i] = 4;
else if(grades[i] == 'B')
scores[i] = 3;
else if(grades[i] == 'C')
scores[i] = 2;
else if(grades[i] == 'D')
scores[i] = 1;
else
scores[i] = 0;
size++;
}
// loop to calculate the GPA
for (i = 0; i < size; ++i)
{
gpa = gpa + scores[i];
}
gpa = gpa/size;
for (int i = 0; i < size; ++i)
{
printf("%c - %d ",grades[i], scores[i]);
}
printf("GPA: %0.2lf ",gpa);
return 0;
}
/*
output:
Enter letter grade: A
Enter letter grade: B
Enter letter grade: A
Enter letter grade: C
Enter letter grade: D
Enter letter grade: F
Enter letter grade: C
Enter letter grade: C
Enter letter grade: G
Invalid grade
A - 4
B - 3
A - 4
C - 2
D - 1
F - 0
C - 2
C - 2
GPA: 2.25
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.