c++ - (指针)在降低测试分数后计算平均函数的问题

标签 c++ pointers

当我为它要求用户输入的每个问题输入以下值时,平均值不正确。我输入的值是测试分数的数量 3,每个测试分数的分数是 64、90、52。我的调试器显示 52 作为我的最低跌落测试分数,因此我认为问题不是 LowestTestScore 函数,而是计算平均测试分数。下面的行应该只给我两个测试分数 (64,90) 的平均值。 平均值=(总数/大小-1);//最低测试分数下降的平均值是错误的 如果有人能指导我走向正确的方向,我将很感激。我发布了整个源代码,因为我不确定您是否可以仅通过代码片段找出问题所在。排序功能正常工作,主要功能也正常工作。我相信最低功能也能正常工作,因为它为我提供了正确的输出作为我的最低测试成绩。

 #include <iostream>
    void sortAscendingOrder(int*, int );
    void LowestTestScore(int*, int);
    void calculatesAverage(int*,int);
    int main()
    {
        int* array = nullptr;
        int input;

        std::cout << "Enter the number of testscores you want to enter." <<std::endl;
        std::cin >> input;

        array = new int[input];

        for(int count =0; count < input; count++)
        {
            std::cout << "Enter the test score" << (count +1) <<":" <<std::endl;
            std::cin >> *(array+count);
            while(*(array+count) < 0)
            {
                std::cout <<"You enter a negative number. Please enter a postive number." <<std::endl;
                std::cin >> *(array+count);
            }
        }

        sortAscendingOrder(array,input);

        for(int count =0; count < input;count++)
        {
            std::cout << "\n" << *(array+count);
            std::cout << std::endl;
        }
        LowestTestScore(array,input);
        calculatesAverage(array,input);



        return 0;
    }
    void sortAscendingOrder(int* input,int size)
    {
        int startScan,minIndex,minValue;

        for(startScan =0; startScan < (size-1);startScan++)
        {
            minIndex = startScan;
            minValue = *(input+startScan);
            for(int index = startScan+1;index<size;index++)
            {
                if(*(input+index) < minValue)
                {
                    minValue = *(input+index);
                    minIndex = index;
                }
            }
            *(input+minIndex)=*(input+startScan);
            *(input+startScan)=minValue;
        }
    }
    void LowestTestScore(int* input, int size)
    {
        int count =0;
        int* lowest = nullptr;
        lowest = input;
        for(count =1; count <size;count++)
        {
            if(*(input+count) < lowest[0])
            {
                lowest[0] = *(input+count);
            }
        }
        std::cout << "Lowest score" << *lowest;
    }
    void calculatesAverage(int* input, int size)
    {
        int total = 0;
        int average =0;
        for(int count = 0; count < size; count++)
        {
            total += *(input+count);

        }
        average =(total/size-1); // Average with the drop lowest test score is wrong.
        std::cout << "Your average is" << average;
    }

最佳答案

要在降低最低测试分数后取平均值,更改

void LowestTestScore(int* input, int size)
{
    int count =0;
    int* lowest = nullptr;
    lowest = input;
    for(count =1; count <size;count++)
    {
        if(*(input+count) < lowest[0])
        {
            lowest[0] = *(input+count);
        }
    }
    std::cout << "Lowest score" << *lowest;
}

至(注意底部的“*lowest = 0;”):

void LowestTestScore(int* input, int size)
{
    int count =0;
    int* lowest = nullptr;
    lowest = input;
    for(count =1; count <size;count++)
    {
        if(*(input+count) < lowest[0])
        {
            lowest[0] = *(input+count);
        }
    }
    std::cout << "Lowest score" << *lowest;
    *lowest = 0;
}

然后在你的 calculatesAverage 函数中,确保你像这样计算平均值:

average =(total/(size-1));

关于c++ - (指针)在降低测试分数后计算平均函数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42520507/

相关文章:

c++ - 取消引用无效指针,然后获取结果的地址

c 函数指针返回值

C++ 数组指针 [] 或++

C++ 带指针的动态二维数组(矩阵)

python - C++ 和 Python 实现之间的不同伪随机数

c++ - C++ 是否提供与 pair<T1, T2> 相当的 "triple"模板?

c++ - 将 3D 指针数组传递给函数

c++ - 构造函数的类型是什么?

c++ - 函数标题中的箭头运算符 (->)

c++ - Visual Studio 中包含的传递库