I'm new to programming and I'm still having trouble with arrays, pointers, and functions. I'd like to know whats wrong with this and how I can fix it. Specifically why the pointer isn't working with the function. Here is the program I'm trying to write: Write a program that DYNAMICALLY creates a pointer to an array large enough to hold a user-defined number of test scores. Once all the scores are entered (in the main function), the array should be passed to a function that RETURNS a DOUBLE for the average score. In the user output, the average score should be formatted with two decimals. Use pointer notation; do not use array notation .
#include <iostream>
#include <iomanip>
#include <memory>
using namespace std;
double getAverage(int, int);
int main()
{
int size = 0;
cout << "How many scores will you enter? ";
cin >> size;
unique_ptr<int[]> ptr(new int[size]);
cout << endl;
int count = 0;
//gets the test scores
for (count = 0; count < size; count++)
{
cout << "Enter the score for test " << (count + 1) << ": ";
cin >> ptr[count];
cout << endl;
}
//display test scores
cout << "The scores you entered are:";
for (count = 0; count < size; count++)
cout << " " << ptr[count];
cout << endl;
double avg;
avg = getAverage(ptr, size);
cout << setprecision(2) << fixed << showpoint << endl;
cout << "The average is " << avg << endl;
return 0;
}
double getAverage(int *ptr, int size)
{
double average1;
double total = 0;
for (int count = 0; count < size; count++)
{
total = total + *(ptr + count);
}
average1 = total / size;
return average1;
}