本文介绍了(指标)在降低测试分数后计算平均值函数的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我为要求用户输入的每个问题输入以下值时,平均值不正确.我输入的值是3,代表每个测试分数的分数分别为64、90、52和39.我的调试器显示52是我的最低掉落测试分数,所以我不认为问题出在LowestTestScore函数上,但是它是计算平均测试分数.下面的行应该只给我两个测试分数的平均值(64,90). average =(total/size-1); // Average with the drop lowest test score is wrong如果有人可以引导我朝正确的方向前进,我将不胜感激.我发布了整个源代码,因为我不确定您是否能够仅通过代码片段来找出问题所在.排序功能按其应有的方式工作,主要功能也应按其应有的方式工作.我认为最低的功能也可以工作,因为它可以为我的最低测试成绩提供正确的输出.

When I enter the following value for each question it ask for the user input, the average is not correct. Values I enter are 3 for the amount of testscores, 64,90,52 for the score for each test score. My debugger shows 52 as my lowest drop test score so I do not believe the issue is the LowestTestScore function, but it's the calculate the average test score. The line below should give me the average of just only the two testscores (64,90). average =(total/size-1); // Average with the drop lowest test score is wrong If someone could guide me to the right direction I would gladly appreciated. I posted the entire source code because I was not sure if you could be able to figure out what is wrong with it with just snippets of the code. The sort function works as it should work, The main function works as it should as well. I believe the lowest function works too since it's giving me the correct output as my lowest 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;
    }

推荐答案

要求出最低分数后的平均值,请更改

To average after dropping the lowest test score, change

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;"位于底部):

To (notice '*lowest = 0;' at the bottom):

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函数中,确保按以下方式计算平均值:

Then in your calculatesAverage function, make sure you calculate the average like:

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

这篇关于(指标)在降低测试分数后计算平均值函数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 13:48