Skip to content

Instantly share code, notes, and snippets.

@sankalpau3
Forked from simoncourtenage/grade5array.cpp
Created October 15, 2019 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sankalpau3/b0ed2ff67f6f4b93025a45b53fca11c0 to your computer and use it in GitHub Desktop.
Save sankalpau3/b0ed2ff67f6f4b93025a45b53fca11c0 to your computer and use it in GitHub Desktop.
Example Grade program using arrays
#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