簡體   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