Question

Class Average Create a program that dynamically creates an array whose size depends on the user's...

Class Average

Create a program that dynamically creates an array whose size depends on the user's preference. Pass the array to a calculate_avg function that is responsible for computing the average GPA of the given array. Use pointer arithmetic throughout your program.

calculate_avg

Create a function called calculate_avg that calculates the average of a double array and returns that average.

calculate_avg() will have two parameters:

  1. a double* referring to the array
  2. an int that contains the size of the given array.

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

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

main

The main function has mostly been built for you. It is your task to dynamically create a double array, store users' grades into the array, and pass the array to calculate_avg to compute and then display the students' average GPA. Read the instructions in main.cpp for more details.

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

Do not forget to deallocate memory that your code dynamically created.

Place the calculate_avg's function prototype in calculate_avg.hpp and it's implementation in calculate_avg.cpp.

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 class!

c++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Executable Code:



calculate_avg.hpp

#include <iostream>

#include <iomanip>

using namespace std;

double calculate_avg(double *, int);

calculate_avg.cpp

#include "calculate_avg.hpp"

double calculate_avg(double *grades, int n)

{

double sum = 0;

for(int i = 0; i < n; i++)

{

sum += *(grades + i);

}

return (sum / n);

}

main.cpp

#include "calculate_avg.hpp"

int main()

{

int n;

cout << "How many students are in your class? ";

cin >> n;

if(n <= 0)

{

cout << "You have no class!" << endl;

exit(0);

}

double grades[n];

cout << "Enter the GPA for the students in your class (0.0 - 4.0)" << endl;

for(int i = 0; i < n; i++)

{

double inp;

cout << "Enter the GPA for the student #" << (i + 1) << ": ";

cin >> inp;

while(inp < 0.0 || inp > 4.0)

{

cout << "GPA must be between 0.0 - 4.0!\n\n";

cout << "Enter the GPA for the student #" << (i + 1) << ": ";

cin >> inp;

}

grades[i] = inp;

}

double average = calculate_avg(grades, n);

cout << setprecision(2) << fixed << "Class average: " << average << endl;

return 0;

}

Sample Output:



Add a comment
Know the answer?
Add Answer to:
Class Average Create a program that dynamically creates an array whose size depends on the user's...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that dynamically allocates an integer array of size of 20 to hold a...

    Write a program that dynamically allocates an integer array of size of 20 to hold a user-defined number of test scores. Once all the scores are entered, the array should be passed to • a function that calculates the average score, the minimal score, and the maximal score in the array, and returns these scores to the main function (hint: use pass by reference, I gave an example in the class) • a function that searches a specific number. The...

  • Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds value...

    Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds values of double data type. 1.) Create a program that produces the following output OUTPUT: Q Quit Enter your choice: e Enter an item: 1.1 E Enqueue D Dequeue s-show queue ← showMenuO function called in main) OQuit // screen clears-.. continue enqueuing.screen clearing with each iteration Enter your choice: e Queue is full. E Enqueue D Dequeue s Show queue 0...

  • Help with C++ please Create a class called ClassQuiz. ClassQuiz will maintain quiz grades for students...

    Help with C++ please Create a class called ClassQuiz. ClassQuiz will maintain quiz grades for students in a class. The class has the following member variables: int numStudents // holds the number of students in the class double* grades // to point to a dynamically allocated array of grades The class has the following public functions: +default constructors //set numStudents = 0; and grades = nullptr; +parameterized constructors //accepts numStudents and creates an array with size = numStudents; +AcceptGrades //...

  • Update your first program to dynamically allocate the item ID and GPA arrays. The number of...

    Update your first program to dynamically allocate the item ID and GPA arrays. The number of items will be the first number in the updated “student2.txt” data file. A sample file is shown below: 3 1827356 3.75 9271837 2.93 3829174 3.14 Your program should read the first number in the file, then dynamically allocate the arrays, then read the data from the file and process it as before. You’ll need to define the array pointers in main and pass them...

  • You will create a Grade Program that will calculate students’ weighted averages. You are going to...

    You will create a Grade Program that will calculate students’ weighted averages. You are going to have the user enter lab grades, test grades, and project grades. You will then display a grade report of each individual average along with their overall average and letter grade. Ask the user to enter their lab grades. Call the averageGrade function that will allow the user to enter their grades and calculate their average. averageGrade Function: This will pass the average back to...

  • Write a program that dynamically allocates an array in the freestore large enough to hold a...

    Write a program that dynamically allocates an array in the freestore large enough to hold a user defined number of test scores as doubles. Once all the scores are entered, the array should be passed to a function that finds the highest score. Another function the array should be passed to a function that finds the lowest score. Another function should be called that calculates the average score. The program should show the highest, lowest and average scores. Must use...

  • Write a program that dynamically allocates an array in the freestore large enough to hold a...

    Write a program that dynamically allocates an array in the freestore large enough to hold a user defined number of test scores as doubles. Once all the scores are entered, the array should be passed to a function that finds the highest score. Another function the array should be passed to a function that finds the lowest score. Another function should be called that calculates the average score. The program should show the highest, lowest and average scores. Must use...

  • Write a C++ console program that defines a class named Course that utilizes a dynamically allocated...

    Write a C++ console program that defines a class named Course that utilizes a dynamically allocated array. Do not use the vector class for this assignment. The Course class should define private data members for the name of the course, the number of students in the course, an array of student names (string*), and the capacity of the course (the array may not be full of students). Use pointer notation when dealing with the array. A 2-arg constructor should initialize...

  • using C# Write a program which calculates student grades for multiple assignments as well as the...

    using C# Write a program which calculates student grades for multiple assignments as well as the per-assignment average. The user should first input the total number of assignments. Then, the user should enter the name of a student as well as a grade for each assignment. After entering the student the user should be asked to enter "Y" if there are more students to enter or "N" if there is not ("N" by default). The user should be able to...

  • Create a function that takes in an integer array called “input” and size. The function should...

    Create a function that takes in an integer array called “input” and size. The function should return a pointer to a dynamically created array the has two locations. The first is the minimum number in the input array and the second is the maximum number in input. Demonstrate your work by calling this function in main. The program should be c++

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT