Created
September 9, 2019 10:42
-
-
Save simoncourtenage/bb51f38e186aa1fbd9b1ac3e3ab7a1cb to your computer and use it in GitHub Desktop.
Example Grade program using arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
int main() | |
{ | |
std::string progName = "Grade Analysis"; | |
std::cout << "Welcome to the " << progName << " program\n" | |
<< "How many students to be entered (max 10)? " << std::endl; | |
int studentNum; | |
float averageGrade; | |
std::cin >> studentNum; | |
if (studentNum > 10) { | |
std::cout << "Too many students - up to 10 only allowed" << std::endl; | |
exit(0); | |
} | |
std::string names[10]; // allows up to 10 names t be stored | |
int grades[10]; | |
for (int i=0;i<studentNum;i++) { | |
std::cout << "Enter name for student " << (i+1) << " : "; | |
std::string studentName; | |
std::cin >> studentName; | |
names[i] = studentName; | |
std::cout << "Enter " << studentName << "'s grade: "; | |
int grade; | |
std::cin >> grade; | |
grades[i] = grade; | |
} | |
// calculate average | |
int sum = 0; | |
for (int i = 0; i < studentNum; i++) { | |
sum = sum + grades[i]; | |
} | |
float average = static_cast<float>(sum) / studentNum; | |
std::cout << "Average grade is " << average << std::endl; | |
std::cout << progName << " completed" << std::endl; | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment