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;
}
First of all your function getAverage()
has different prototype than that you defined. And secondly you try to pass std::unique_ptr<int []>
object into a function that instead expects a int*
. But std::unique_ptr<int []>
is a different type than int*
and not implicitly convertible. So to pass int *
use std::unique_ptr::get
function. like
#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.get(), 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;
}
In the assignment there is written
Use pointer notation; do not use array notation
This means that you should not use subscripting.
In main the variable ptr
is declared as having the type std::unique_ptr<int[]>
.
unique_ptr<int[]> ptr(new int[size]);
You are trying to pass it to the function getAverage
avg = getAverage(ptr, size);
that has the corresponding parameter of the type int
.
double getAverage(int, int);
Though then you defined the function as having the parameter of the type int *
in any case there is no explicit conversion from the type std::unique_ptr<int[]>
to the type int *
. You should use method get
of the smart pointer.
Also the function parameter should be declared like const int *
because the array is not changed in the function.
The program can look the following way
#include <iostream>
#include <iomanip>
#include <memory>
double getAverage(const int *a, size_t n)
{
double total = 0.0;
for (const int *p = a; p != a + n; ++p) total += *p;
return n == 0 ? total : total / n;
}
int main()
{
size_t n = 0;
std::cout << "How many scores will you enter? ";
std::cin >> n;
std::unique_ptr<int[]> ptr(new int[n]);
std::cout << std::endl;
size_t i = 0;
for ( int *current = ptr.get(); current != ptr.get() + n; ++current )
{
std::cout << "Enter the score for test " << ++i << ": ";
std::cin >> *current;
}
std::cout << std::endl;
std::cout << "The scores you entered are:";
for (const int *current = ptr.get(); current != ptr.get() + n; ++current)
{
std::cout << " " << *current;
}
std::cout << std::endl;
double avg = getAverage( ptr.get(), n );
std::cout << std::setprecision(2) << std::fixed << std::showpoint;
std::cout << "\nThe average is " << avg << std::endl;
return 0;
}
The program output might look like
How many scores will you enter? 10
Enter the score for test 1: 1
Enter the score for test 2: 2
Enter the score for test 3: 3
Enter the score for test 4: 4
Enter the score for test 5: 5
Enter the score for test 6: 6
Enter the score for test 7: 7
Enter the score for test 8: 8
Enter the score for test 9: 9
Enter the score for test 10: 10
The scores you entered are: 1 2 3 4 5 6 7 8 9 10
The average is 5.50
You almost had it. Just need to change this chunk:
using namespace std;
double getAverage(int*, int); // <--here
int main()
{
int size = 0;
cout << "How many scores will you enter? ";
cin >> size;
int *ptr = new int[size]; //<--and here (but you'll need to delete it later)
//unique_ptr<int[]> ptr(new int[size]);
I don't recommend mixing smart pointers and standard pointers until you have a better understanding of standard pointers. edit: I mean mixing them in terms of the same effective pointer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.