In: Computer Science
Write a program that dynamically allocates a built-in array large enough to hold a user-defined number of test scores. (Ask the user how many grades will be entered and use a dynamic array to store the numbers.) Once all the scores are entered, the array should be passed to a function that calculates the average score. The program should display the scores and average. Use pointer notation rather than array notation whenever possible. (Input Validation: Do not accept negative numbers for test scores.) Make it a class, call it something like gradeholder. You will need a destructor because you are using dynamic arrays.
please help!! this is C++ programming fundamental 2 and this is visual studio, thank you!
CODE IN JAVA-
#include <stdio.h>
#include<iostream>
using namespace std;
class gradeholder {
private: int n;
int *arr; //Dynamic array
float sum=0;
public: gradeholder()
{
cout << "Enter the number of grades";
cin >> n;
arr = new int[n];
cout << "Enter the " << n << " grades. \n";
for (int i = 0; i < n; i++)
{
cin >> *(arr + i);
if ((arr[i]) < 0) {
cout << "Do not enter negative scores.\n"; //Not accepting
negative scores.
i--;
}
}
}
void avg_score()
{
for (int i = 0; i < n; i++)
sum+= *(arr + i);
}
void display()
{
cout << "The scores are : ";
for (int i = 0; i < n; i++) {
cout << *(arr + i) << " ";
}
cout << "\nThe average score is : " << sum / n;
}
~gradeholder()
{
delete [] arr; //Deleting dynamic array
}
};
int main()
{
gradeholder obj1;
obj1.avg_score();
obj1.display();
return 0;
}
CODE WINDOW-
OUTPUT WINDOW-