Need help in basic C++ regarding how to properly loop through part and finding smallest value

StackOverflow https://stackoverflow.com/questions/13260908

  •  27-11-2021
  •  | 
  •  

Question

Hi I'm needing some help. I'm in a intro to programming class and we are using c++. I am hoping someone can help me with an assignment that was due yesterday (I understand not to expect miracle responses but a girl can always try).

I'm having two problems that I know of. The first is regarding the smallest value. The big one is in trying to make it loop for requirements of three times but not lose out on my total count. I cannot use arrays or anything I haven't learned yet which is why I've posted this. I've seen similar problems and questions but they have ended up with answers too complex for current progress in class. So here is the problems instructions:

Instructions 1) Write a program to find the average value, the largest value, and the smallest value of a set of numbers supplied as input from the keyboard. The number of values in the data set must be in the range 0 to 20, inclusive. The user will first enter the number of values in the data set(use variable int Number). Give the user 3 attempts at entering Number in the range given. If the value for Number entered is out of this range, write an error message but continue. If the user does not enter a valid value for Number within the 3 attempts print an error message and terminate the program.

2) Format only the output for the Average value to 3 decimal places when printed.

3) The values in the data set entered as input can be any value positive, negative, or zero.

4) Make the program output readable(see the example below). (Note: that you will notprint out the input values that were entered in this program like you normally are required to do. This is because we have not covered the “tool” needed to do so yet in our studies).

Below will be the output from the execution of your program: (using these values in order for the data set --> 19.0 53.4 704.0 -15.2 0 100.0)

The largest number:  704
The smallest number:  -15.2
The average of the 6 numbers entered: 143.533
yourName L4p2XX.cpp
Lab#4 prob 2 XX-XX-12

Here is my poor excuse at the solution:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    double Number = 0, minValue, maxValue, average, total = 0;
    int ct = 0, numCount;
    cout << "How many numbers would you like to enter? ";
    cin >> numCount;

    for(ct = 1; ct <= numCount; ct += 1)
    {    
        cout << "Enter Value from 0 to 20, inclusive: ";
        cin >> Number;

        if(Number > 20|| Number < 0)
            for(int errorCt = 1; errorCt <= 4; errorCt += 1)
            { 
                if(errorCt == 4)
                {  
                    cout << "You have had 3 attempts to enter a valid" <<
                            "number. \nPlease try this program again when you" <<
                            "are able to follow directions.";
                    cout <<"\nLBn\n"<<"L4P2LB.cpp\n"<<"11-05-12\n";
                    return 0;
                }
                cout << Number << "is not within range.\n" << 
                        "Please enter a number from 0 to 20: ";
                cin >> Number;
            } //end for loop

        total += Number;
        if(maxValue <= Number)
            maxValue = Number;
        if(Number <= minValue)
            minValue = Number;
    } //end for loop

    cout << "The smallest number entered was " << minValue << endl;
    cout << "The largest number you entered was " << maxValue << endl;
    average = total/numCount;
    cout << setprecision(3) << fixed << showpoint << "You entered " <<
            numCount << " numbers. The average of these is " << average;

    //Program ID
    cout <<"\n" << "L4P2LB.cpp\n" << "11-05-12\n";
    system ("pause");
    return 0;
} // End main

Thank you in advance to anyone who can steer me in the right direction. Not looking for anyone to do my work I just need help in direction if nothing else or any suggestions as to what to do. Thanks again. Lynda

Also I need somehow to pause after the third time and exit properly. If I put the second pause in it won't work so am I missing something obvious there too!

Was it helpful?

Solution

The first problem I see is that you didn't initialize a couple of variables.

You should either initialize both minValue and maxValue variables with something which will overwritten in every case in the first loop (typically "positive/negative infinity", as provided by <limits>), or just set both to Number in the first iteration, regardless of their current value. So I'd suggest to fix this by replacing

    if(maxValue <= Number)
        maxValue = Number;
    if(Number <= minValue)
        minValue = Number;

with

    if(maxValue <= Number || ct == 1)
        maxValue = Number;
    if(Number <= minValue || ct == 1)
        minValue = Number;

as ct == 1 will be true in the first iteration.

That said, you check the 0..20 range condition on the wrong variable. You check it on the Number variable, but you should check the numCount variable. But you also didn't respect the requirement that the variable to store the "number of numbers" should be Number, so you did check the correct variable, but used the wrong to read the input into. This should fix this issue (I changed the variable name in the cin >>... line + moved the check outside your main loop):

cout << "How many numbers would you like to enter? ";
cin >> Number;
if(Number > 20|| Number < 0)
{
    for(int errorCt = 1; errorCt <= 4; errorCt += 1)
    ...
        if(errorCt == 4)
        {  
            cout << "You have had 3 attempts to enter a valid" <<
                    "number. \nPlease try this program again when you" <<
                    "are able to follow directions.";
            cout <<"\nLBn\n"<<"L4P2LB.cpp\n"<<"11-05-12\n";
            return 0;
        }
        cout << Number << "is not within range.\n" << 
                "Please enter a number from 0 to 20: ";
        cin >> Number;
    } //end for loop
}

for(ct = 1; ct <= Number; ct += 1)
{    
    ...
}
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top