【问题标题】:In c++ how do I go about using pointers to get the average of an array?在 C++ 中,我如何使用指针来获取数组的平均值?
【发布时间】:2017-10-18 00:40:03
【问题描述】:

我是编程新手,但在数组、指针和函数方面仍有问题。我想知道这有什么问题以及如何解决它。特别是为什么指针不能与函数一起使用。这是我正在尝试编写的程序:编写一个程序,该程序动态创建一个指向数组的指针,该数组大到足以容纳用户定义的测试分数数量。一旦输入了所有分数(在主函数中),该数组应该被传递给一个函数,该函数返回一个 DOUBLE 作为平均分数。在用户输出中,平均分数应采用两位小数的格式。使用指针表示法;不要使用数组表示法。

#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。一个被声明占用两个ints,但从未实现;这是您尝试从main 调用的那个,参数类型错误。另一个采用 int*int - 这个已实现,但从未调用。

标签: c++ arrays pointers


【解决方案1】:

首先,您的函数getAverage() 的原型与您定义的不同。其次,您尝试将std::unique_ptr&lt;int []&gt; 对象传递给一个需要int* 的函数。但是std::unique_ptr&lt;int []&gt; 是与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;
} 

【讨论】:

  • 谢谢你。小事情真的会扰乱程序,这令人沮丧。
【解决方案2】:

作业里写着

使用指针表示法;不要使用数组表示法

这意味着你不应该使用下标。

在 main 中,变量 ptr 被声明为具有 std::unique_ptr&lt;int[]&gt; 类型。

unique_ptr<int[]> ptr(new int[size]);

您正在尝试将其传递给函数getAverage

avg = getAverage(ptr, size);

具有int类型的相应参数。

double getAverage(int, int);

尽管您将函数定义为具有int * 类型的参数,但无论如何都没有从std::unique_ptr&lt;int[]&gt; 类型到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

【讨论】:

    【解决方案3】:

    你几乎拥有它。只需要改变这个块:

    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]);
    

    在您对标准指针有更好的理解之前,我不建议将智能指针和标准指针混用。编辑:我的意思是根据 same 有效指针混合它们。

    【讨论】:

    • std::unique_ptr::get() 分发原始指针并没有错。
    • 除非在使用原始指针时 unique_ptr 超出范围。但你肯定适合这个例子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多