0

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;
    }
Pierre
  • 41
  • 1
  • 6
  • Forgot to mention the average the program its outputting. It's giving me an average of 67, instead of the correct average 77. – Pierre Feb 28 '17 at 23:23
  • usual comments about using vector instead of naked arrays etc. And please do `array[count]` not `*(array+count)` – pm100 Mar 01 '17 at 00:06
  • 1
    to clarify - use std::vector instead of naked arrays, life is much simpler – pm100 Mar 01 '17 at 00:10

1 Answers1

2

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

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

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

average =(total/(size-1));
vincent
  • 1,370
  • 2
  • 13
  • 29
  • If I divide the average by just the size of it. Then the lowest test score will also be included, and I don't want that. I want just average test score of the two test after the lowest testscore has been dropped. That is why I'm dividing total by size-1. Because it's just taking the average of two test which gives me 67. – Pierre Feb 28 '17 at 23:31
  • Ah, ok. You could set the lowest test score to 0 in your LowestTestScore function, then divide by size-1 as you did. – vincent Feb 28 '17 at 23:33
  • An alternative to blatting over the lower test score with 0 (which is a bit destructive) is subtracting that score from the total before dividing, or not adding it into the total in the first place (which is easy to do given the inputs are sorted beforehand), but no big deal... – Tony Delroy Feb 28 '17 at 23:38
  • @vincent why does this work `*lowest =0;` What I noticed is if I have `*lowest = input;` in the average function it would take the total and divide by just 2. For example if I enter 3 as my amount of test. 89,91,70. The way you suggested my debugger shows the correct output, but if I write differently by just having it as `*lowest = input` it does not give me the correct output? – Pierre Mar 01 '17 at 04:32
  • When you loop through your data, you store a pointer to the memory address of the lowest score. If you then set *lowest = 0, you are clearing the lowest score in your array to 0. So then scores are 89, 91, 0. Later, when you calculate your average, this works out because you account for the dropped score with size-1. I'm not sure where you're setting *lowest = input in the code you're referencing, but input is a pointer and not 0. I'm not really sure what you're asking. I'd also recommend using vectors and normal 'for' loops or interators, rather than pointer arithmetic. That can cause issues. – vincent Mar 01 '17 at 06:32