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

时间:2017-02-28 23:14:20

标签: c++ pointers

当我为每个问题输入以下值时,它要求输入用户,平均值不正确。我输入的值是测试核心量的3,每个测试分数的得分为64,90,52。我的调试器显示52是我的最低跌落测试分数,所以我不相信问题是LowestTestScore函数,但它是计算平均测试分数。下面的行应该只给出两个测试核心(64,90)的平均值。 average =(total/size-1); // Average with the drop lowest test score is wrong如果有人能引导我走向正确的方向,我会很高兴地欣赏。我发布了整个源代码,因为我不确定你是否能够通过代码片段找出它的错误。 sort函数应该工作,main函数也应该工作。我相信最低功能也可以工作,因为它给我正确的输出作为我的最低testgrade。

 #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;
    }

1 个答案:

答案 0 :(得分:2)

在降低最低测试分数后进行平均,请更改

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;
}

至(注意&#39; *最低= 0;&#39;在底部):

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;
}

然后在您的calculateAverage函数中,确保计算平均值,如:

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