在C++中,我该如何去使用指针来获取数组的平均值?
我是编程新手,我仍然遇到数组,指针和函数的麻烦。我想知道这有什么问题,以及我如何解决这个问题。具体为什么指针不能与该函数一起工作。这里是我试图编写的程序:编写一个程序,DYNAMICALLY创建一个指向数组足够大的数组的指针,以保存用户定义的测试分数。一旦输入了所有分数(在主函数中),该数组就应该被传递给一个返回平均分数的DOUBLE的函数。在用户输出中,平均得分应格式化为两位小数。使用指针表示法;不要使用数组表示法。在C++中,我该如何去使用指针来获取数组的平均值?
#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;
}
首先,您的功能getAverage()
与您定义的原型不同。其次,您尝试将std::unique_ptr<int []>
对象转换为函数,而不是期望int*
。但是std::unique_ptr<int []>
是与int*
不同的类型,不能隐式转换。 所以要通过int *
使用std::unique_ptr::get
函数。 像
#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;
}
谢谢你。令人沮丧的是,这些小事可能会让一个程序感到不安。 –
你已经差不多了。只需要改变这个块:
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]);
我不建议混合智能指针和标准指针,直到你有一个更好的理解标准指针。编辑:我的意思是混合它们的相同的有效指针。
在分配有书面
使用指针符号;不要使用数组符号
这意味着你不应该使用下标。
主要变量ptr
被声明为具有类型std::unique_ptr<int[]>
。
unique_ptr<int[]> ptr(new int[size]);
您正在试图将它传递给函数getAverage
avg = getAverage(ptr, size);
具有类型int
的相应参数。
double getAverage(int, int);
虽然那么你定义的函数具有在任何情况下的类型int *
的参数存在从类型std::unique_ptr<int[]>
到类型int *
没有显式转换。您应该使用智能指针的方法get
。
此外,函数参数应声明为const int *
,因为数组在函数中没有更改。
程序可以看看下面的方式
#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;
}
程序输出可能看起来像
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
有在你的程序名为'getAverage'两个不同的功能。一个被宣布采用两个“int”,但从未实现;这是你试图从'main'调用的那个,它有错误的参数类型。另一个采用'int *'和'int' - 这个实现了,但从来没有调用过。 –
[我如何将unique_ptr传递给函数](https://*.com/q/30905487/669576) –