Create a program that dynamically creates a vector whose size depends on the user's preference. Pass the vector to a CalculateAverage function that is responsible for computing the average GPA of the given vector of GPAs.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

Class Average GPA

Create a program that dynamically creates a vector whose size depends on the user's preference. Pass the vector to a CalculateAverage function that is responsible for computing the average GPA of the given vector of GPAs.

CalculateAverage

Implement a function called CalculateAverage in calculate_avg.cc that calculates the average of a double vector and returns that average.

CalculateAverage() will have one parameter, a std::vector<double>& referring to the vector.

When the vector size is greater than 0, the function should calculate the average GPA from the given vector of grades.

However, when the size of the vector is 0, then the function should return 0.

main

The main function has mostly been built for you. It is your task to:

  1. Construct a double vector based on the inputted class size.
  2. Store each student's GPAs into the vector.
  3. Pass the vector as input to CalculateAverage to compute the average GPA of the class.
  4. Display the students' average GPA. Read the instructions in main.cc for more details.

If the user happens to provide a class size of 0, then the program should output "You have no students!" and then end the execution of the program without attempting to calculate the average.

Place the CalculateAverage's function's' implementation in calculate_avg.cc. We have provided the prototype in calculate_avg.h.

Sample Output

How many students are in your class? 5 Enter the GPA for the students in your class (0.0 - 4.0) Enter the GPA for student #1: 3.8 Enter the GPA for student #2: 2.5 Enter the GPA for student #3: 4.0 Enter the GPA for student #4: 1.9 Enter the GPA for student #5: 3.6 Class average: 3.16 How many students are in your class? 0 You have no students!

main.cc file

#include <iomanip>
  #include <iostream>
   
  int main() {
  int num_students = 0;
  std::cout << "How many students are in your class? ";
  std::cin >> num_students;
   
  if (num_students <= 0) {
  std::cout << "You have no students!\n";
  } else {
  // ===================== YOUR CODE HERE =====================
  // Construct a vector whose size is the same as the
  // given number of students in the class, based on the
  // `num_students` variable. Don't forget to #include <vector>.
  // ==========================================================
   
  std::cout << "Enter the GPA for the students in your class (0.0 - 4.0)\n";
  for (int i = 0; i < num_students; i++) {
  // ============ YOUR CODE HERE ============
  // Store the input GPA from cin into
  // the corresponding index in the vector.
  // ========================================
  std::cout << "Enter the GPA for student #" << i + 1 << ": ";
  }
  double average = 0;
   
  // ===================== YOUR CODE HERE =====================
  // Call your CalculateAverage function and provide any
  // necessary parameters. Then, store the results in the average
  // variable. Make sure to #include "calculate_avg.h" before you
  // use the CalculateAverage function.
  // ==========================================================
   
  std::cout << "Class average: " << std::setprecision(2) << std::fixed
  << average << "\n";
  }
   
  return0;
  }

 

calculate_avg.cc file

#include <vector>
   
  double CalculateAverage(const std::vector<double> &student_grades) {
  // ================= YOUR CODE HERE =================
  // Compute and return the average grade
  // based on the input vector containing student GPAs.
  // Hint: you may want to use a range based loop!
  // ==================================================
  return0.0;
  }

 

calculate_avg.h file

#include <vector>
   
  // This is the function declaration for the function you
  // will implement in the implementation file, calculate_avg.cc
  // You do not need to edit this file.
  double CalculateAverage(const std::vector<double> &student_grades);
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 6 steps with 2 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education