Calculate grade average with weights

Calculate grade average with weights
This C++ program will calculate the average of 4 grades by using weight calculation. It will then display the average and give the user a chance to review the grades.
  1. #include <cstdlib>
  2. #include <iostream>
  3. using namespace std;
  4. bool isitvalid(double[]);
  5. bool printarray(double[], int);
  6. int main()
  7. {
  8.  // To hold the weights and the grades
  9.  double weights[4];
  10.  double grades[4];
  11.  // To hold the averages
  12.  double avrGrade = 0;
  13.  double avrClassGrade = 0;
  14.  int numOfStudents = 0;
  15.  char respChar = ‘N’;
  16.  int revGrade;
  17.  bool anotherStudent = false;
  18.  // Do until the user decides not to enter anymore students
  19.  do
  20.  {
  21.  // Loop for the user to enter the weight
  22.  for(int i = 0; i < 4; i++)
  23.  {
  24.  cout << “Please enter weight for grade (e.g. 0.3) #” << i + 1 << “: “;
  25.  cin >> weights[i];
  26.  }
  27.  // Loop for the user to enter the grades (until all are valid)
  28.  do
  29.  {
  30.  for(int i = 0; i < 4; i++)
  31.  {
  32.  cout << “Please enter grade #” << i + 1 << “: “;
  33.  cin >> grades[i];
  34.  }
  35.  }
  36.  while(isitvalid(grades) == false);
  37.  // Loop to calculate the average
  38.  for(int i = 0; i < 4; i++)
  39.  {
  40.                 avrGrade += weights[i] * grades[i];
  41.  }
  42.  // Show the final grade
  43.  cout << “The final grade is: ” << avrGrade << “\n“;
  44.  // If the user wants to review the grades, call the printarray() function
  45.  cout << “Enter the number of grades you want to review (0 to continue): “;
  46.  cin >> revGrade;
  47.  if(revGrade > 0)
  48.  {
  49.                     printarray(grades, revGrade);
  50.  }
  51.  cout << “Would you like to enter another student? Type Y for Yes anything else for No: “;
  52.  cin >> respChar;
  53.  // Holds the average class grade
  54.         avrClassGrade += avrGrade;
  55.         numOfStudents++;
  56.  }
  57.  while(respChar == ‘Y’);
  58.  // When the loops and conditions are all complete show the class average
  59.  cout << “\nThe class average is ” << avrClassGrade / numOfStudents << “\n“;
  60.  system(“PAUSE”);
  61.  return 0;
  62. }
  63. bool isitvalid(double grades[])
  64. {
  65. // Will be set to true if a bad grade is found
  66. bool badGrade = false;
  67. for(int i = 0; i < 4; i++)
  68. {
  69. if(grades[i] < || grades[i] > 100)
  70. {
  71.                   badGrade = true;
  72. }
  73. }
  74. if(badGrade == true)
  75. {
  76. return false;
  77. }
  78. else
  79. {
  80. return true;
  81. }
  82. }
  83. bool printarray(double grades[], int numOfGrades)
  84. {
  85. // Loop through the number of grades the user has decided to review and show them
  86. for(int i = 0; i < numOfGrades; i++)
  87. {
  88. cout << “Grade #” << i + << ” is ” << grades[i] << “\n“;
  89. }
  90. }
Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top