124k views
1 vote
C-PROGRAMING

You are to create 3 Random numbers from 0-100 for the first person.
You are then to ask the user to input 3 numbers from 0-100 for the second person.
Tell me who has the bigger "average".
DISPLAY the GRADES, AVERAGES and the LETTER GRADE of each person.
You are to display the averages with only 1 place after the decimal point.

User DWolf
by
8.5k points

1 Answer

6 votes

int main() {

int p1[3], p2[3]; // arrays to store the 3 numbers for each person

float p1_avg, p2_avg; // variables to store the average of each person

char p1_grade, p2_grade; // variables to store the letter grade of each person

// generate 3 random numbers from 0-100 for the first person

srand(time(NULL)); // seed the random number generator with current time

for (int i = 0; i < 3; i++) {

p1[i] = rand() % 101; // generate a random number from 0-100

}

// ask the user to input 3 numbers from 0-100 for the second person

printf("Enter 3 numbers from 0-100 for the second person:\\");

for (int i = 0; i < 3; i++) {

scanf("%d", &p2[i]); // read the input number

}

// calculate the average of each person

p1_avg = (p1[0] + p1[1] + p1[2]) / 3.0;

p2_avg = (p2[0] + p2[1] + p2[2]) / 3.0;

// determine the letter grade of each person based on their average

if (p1_avg >= 90) {

p1_grade = 'A';

} else if (p1_avg >= 80) {

p1_grade = 'B';

} else if (p1_avg >= 70) {

p1_grade = 'C';

} else if (p1_avg >= 60) {

p1_grade = 'D';

} else {

p1_grade = 'F';

}

if (p2_avg >= 90) {

p2_grade = 'A';

} else if (p2_avg >= 80) {

p2_grade = 'B';

} else if (p2_avg >= 70) {

p2_grade = 'C';

} else if (p2_avg >= 60) {

p2_grade = 'D';

} else {

p2_grade = 'F';

}

// display the grades, averages, and letter grades of each person

printf("Person 1 Grades: %d %d %d\\", p1[0], p1[1], p1[2]);

printf("Person 1 Average: %.1f\\", p1_avg);

printf("Person 1 Letter Grade: %c\\", p1_grade);

printf("Person 2 Grades: %d %d %d\\", p2[0], p2[1], p2[2]);

printf("Person 2 Average: %.1f\\", p2_avg);

printf("Person 2 Letter Grade: %c\\", p2_grade);

}

Hope I helped :)

User Kristian Roebuck
by
8.6k points