C++ 动态分配数组,冒泡排序 + 求平均值

标签 c++

我创建了冒泡排序,我想我也创建了平均排序。没有把握。但是我需要将它实现到我的主要功能中,但我不确定如何实现。现在有点卡住了。任何帮助都会很棒。谢谢!

Write a program that dynamically allocates an array large enough to hold a user-defined number of test scores.

Once all the scores are entered, the array should be passed to a function (you create) as a pointer that sorts it in ascending order. This function can be named sortArray and will take two parameters: (double* anArray, int size). The function will return true if the array is sorted, or false otherwise (for example if size <= 0)

Another function should be created that calculates the average score of the items in the array. This function will take in two parameters (double* anyArray, int size) and return the average as a double.

The program should display the sorted array of scores and averages with appropriate headings.

Use pointer notation rather than array notation whenever possible.

#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 并且它从不返回任何值所以让它无效并且你在排序数组中使用了 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);
}

关于C++ 动态分配数组,冒泡排序 + 求平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33426625/

相关文章:

C++ 有趣的函数定义

c++ - 移动后是否需要重置 std::list?

C++链表——析构函数实现

c++ - constexpr 和 inline 函数可以重新定义吗?

c++ - 如何在保持其值的同时将 C++ unsigned char 变量转换为 char 变量

C++ 预处理器宏选择参数

c++ - Qt 应用程序在退出时崩溃

c++ - 内存管理的 C++ 单例

c++ - 如果 : command not found, 窗口命令

原始对象的 C++ 指针和复制对象的指针