简体   繁体   English

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

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

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. 一旦输入了所有分数(在主函数中),就应将数组传递给一个函数,该函数对平均分数返回DOUBLE。 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. 首先,您的函数getAverage()具有与您定义的原型不同的原型。 And secondly you try to pass std::unique_ptr<int []> object into a function that instead expects a int* . 其次,您尝试将std::unique_ptr<int []>对象传递给一个需要int*的函数。 But std::unique_ptr<int []> is a different type than int* and not implicitly convertible. 但是std::unique_ptr<int []>是与int*不同的类型,并且不能隐式转换。 So to pass int * use std::unique_ptr::get function. 因此,要传递int *使用std::unique_ptr::get函数。 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[]> . 在主变量ptr被声明为std::unique_ptr<int[]>

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

You are trying to pass it to the function getAverage 您正在尝试将其传递给函数getAverage

avg = getAverage(ptr, size);

that has the corresponding parameter of the type int . 具有相应类型为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 * . 尽管随后您将函数定义为具有int *类型的参数,但是在任何情况下都没有从std::unique_ptr<int[]>类型到int *类型的显式转换。 You should use method get of the smart pointer. 您应该使用智能指针的get方法。

Also the function parameter should be declared like const int * because the array is not changed in the function. 另外,应将函数参数声明为const int *因为const int *的数组未更改。

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. 编辑:我的意思是根据相同的有效指针混合它们。

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

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