C++动态分配数组,Bubblesort+Finding Average

C++ Dynamically Allocate an Array, Bubblesort + Finding Average

本文关键字:Bubblesort+Finding Average 数组 动态分配 C++      更新时间:2023-10-16

我创建了bubblesort,我想我也有一个平均值。不确定。但我需要把它实现到我的主要功能中,我不知道如何实现。现在有点卡住了。任何帮助都会很棒。谢谢

编写一个程序,动态分配一个足够大的数组保存用户定义数量的测试分数。

输入所有分数后,数组应传递给函数(您创建的)作为一个指针,按升序对其进行排序。此函数可以命名为sortArray,并将采用两个参数:(双*anArray,int大小)。如果数组被排序,否则为false(例如,如果大小<=0)

应创建另一个计算平均分数的函数数组中项目的。此函数将接受两个参数(double*anyArray,int size),并以double形式返回平均值。

程序应显示分数和平均值的排序数组带有适当标题。

尽可能使用指针表示法而不是数组表示法。

#include <iostream>
#include <iomanip>
using namespace std;
bool sortArray(double* anArray, int size);
double averageArray(double* anyArray, int size);

int main()
{
    double* anArray;
    double total = 0.0;
    double average;
    int scores;
    int count;
    cout << "How many test scores are you entering?: ";
    cin >> scores;
    anArray = new double[scores];
    cout << "Enter test scores: ";
    for (count = 0; count < scores; count++)
    {
        cout << "Test Score " << count + 1 << ": ";
        cin >> anArray[count];
    }

    system("pause");
    return 0;
}
bool sortArray(double* anArray, int size)
{
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size - 1; j++)
        {
            if (anArray[j] > anArray[j + 1])
            {
                int temp = anArray[j];
                anArray[j] = anArray[j + 1];
                anArray[j + 1] = temp;
            }
        }
    }
}
double averageArray(double* anyArray, int size)
{
    double count = 0.0;
    for (int i = 0; i < size; i++)
    {
        count += anyArray[i];
    }
    return (count / size);
}

您在sortarray函数中使用了bool,它永远不会返回任何值,所以将其设为void,并且您在sort array中使用了int temp,它必须是double。我为您使用此代码修复了它。顺便说一下,您没有调用函数,并且在排序后忘记打印数组的值。

#include <iostream>
#include <iomanip>
using namespace std;
bool sortArray(double* anArray, int size);
double averageArray(double* anyArray, int size);

int main()
{
    double* anArray;
    double total = 0.0;
    double average;
    int scores;
    int count;
    cout << "How many test scores are you entering?: ";
    cin >> scores;
    anArray = new double[scores];
    cout << "Enter test scores: ";
    for (count = 0; count < scores; count++)
    {
        cout << "Test Score " << count + 1 << ": ";
        cin >> anArray[count];
    }
    sortArray(anArray, scores);
    for (count = 0; count < scores; count++)
    {
        cout << anArray[count] << "n";
    }
    cout << averageArray(anArray, scores);
    system("pause");
    return 0;
}
bool sortArray(double* anArray, int size)
{
    bool sort = false;
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size - 1; j++)
        {
            if (anArray[j] > anArray[j + 1])
            {
                sort = true;
                double temp = anArray[j];
                anArray[j] = anArray[j + 1];
                anArray[j + 1] = temp;
            }
        }
    }
    return sort;
}
double averageArray(double* anyArray, int size)
{
    double count = 0.0;
    for (int i = 0; i < size; i++)
    {
        count += anyArray[i];
    }
    return (count / (double) size);
}