LAB WORK 4: PRACTICE LOOP, SELECTION, and ARRAY (if any)
Ngunguto, a high school student with a keen interest in programming, found himself inspired during a computer science class project. His teacher challenged the students to create a tool that could help with academic performance tracking. Motivated by this challenge, Ngunguto decided to develop a program that would calculate and display the average scores and corresponding grades for five of his classmates based on their performance in four subjects: Mathematics, Science, English, and History.
To begin, Ngunguto gathered the scores of his classmates, ensuring he had accurate data for each subject. He structured the data in a clear format, creating a list that included the names of the students and their respective scores. This organization was crucial, as it would allow him to efficiently process the information later.
Next, Ngunguto set about writing the program. He started by defining a function that would calculate the average score for each student. To make the program user-friendly, he included comments to explain each part of the code. He implemented a grading scale that would categorize the average scores: an A for scores between 90 and 100, a B for 80 to 89, a C for 70 to 79, a D for 60 to 69, and an F for scores below 60.
After coding the logic, Ngunguto ran several tests with sample data to ensure the program worked correctly. He was thrilled to see that it not only calculated the averages accurately but also determined the correct grades for each student. To enhance the output, he formatted the results to display each student’s name, average score, and corresponding grade neatly.
Encouraged by his success, Ngunguto decided to present his project to the class. During his presentation, he explained the process he followed, the challenges he faced, and how he overcame them. His classmates were impressed with the program’s functionality, and many expressed interests in using it for their own academic tracking.
This project not only solidified Ngunguto’s programming skills but also fostered a sense of collaboration among his peers. They discussed potential improvements, such as adding features for tracking progress over time or incorporating a graphical user interface. Inspired by the feedback, Ngunguto felt motivated to continue enhancing the program, turning it into a valuable tool for academic success. Through this experience, he learned the importance of data management, problem-solving, and the power of technology in education.
Solution Implementation
Write a complete C++ program that solves Ngunguto’s problem.
Solution Code (Click to show)
#include <iostream>#include <string>#include <iomanip>using namespace std;int main() { // Student data: names and scores for 4 subjects string names[5] = {"Alice", "Bob", "Charlie", "Diana", "Ethan"}; int scores[5][4] = { {85, 92, 78, 88}, // Alice's scores {72, 68, 75, 80}, // Bob's scores {95, 89, 92, 96}, // Charlie's scores {58, 62, 65, 60}, // Diana's scores {45, 52, 48, 55} // Ethan's scores }; string subjects[4] = {"Mathematics", "Science", "English", "History"}; cout << "STUDENT ACADEMIC PERFORMANCE REPORT" << endl; cout << "================================================" << endl; // Process each student using a loop for (int i = 0; i < 5; i++) { int total = 0; // Calculate total score using a loop for (int j = 0; j < 4; j++) { total += scores[i][j]; } // Calculate average score double average = total / 4.0; // Determine grade using selection statements char grade; if (average >= 90) { grade = 'A'; } else if (average >= 80) { grade = 'B'; } else if (average >= 70) { grade = 'C'; } else if (average >= 60) { grade = 'D'; } else { grade = 'F'; } // Display results cout << "Student: " << names[i] << endl; cout << " Subjects: "; for (int j = 0; j < 4; j++) { cout << subjects[j] << "=" << scores[i][j]; if (j < 3) cout << ", "; } cout << endl; cout << fixed << setprecision(2); cout << " Average Score: " << average << endl; cout << " Grade: " << grade << endl; cout << "--------------------------------" << endl; } return 0;}
Explain how loops are used in this C++ program.
Answer (Click to show)
Loops are used in this C++ program in three ways:
Outer for-loop: for (int i = 0; i < 5; i++) - iterates through each student
Inner for-loop: for (int j = 0; j < 4; j++) - calculates total score by summing all subjects