繁体   English   中英

在C ++中,我如何使用指针获取数组的平均值?

[英]In c++ how do I go about using pointers to get the average of an array?

我是编程新手,但数组,指针和函数仍然有问题。 我想知道这有什么问题以及如何解决。 具体说明为什么指针不能与该函数一起使用。 这是我要编写的程序:编写一个程序,以动态方式创建一个指向数组的指针,该数组的大小足以容纳用户定义的测试分数。 一旦输入了所有分数(在主函数中),就应将数组传递给一个函数,该函数对平均分数返回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()具有与您定义的原型不同的原型。 其次,您尝试将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;
} 

在作业中写着

使用指针符号; 不要使用数组符号

这意味着您不应使用下标。

在主变量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 *因为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

你差点就吃了。 只需更改此块即可:

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

我不建议您混合使用智能指针和标准指针,除非您对标准指针有更好的了解。 编辑:我的意思是根据相同的有效指针混合它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM