do while for if statements CODEBLOCKS C++

Write a program that will ask the user for a set of ten numbers. After all ten numbers have been entered, the program will display the largest and smallest numbers of the data set. The program will then prompt the user whether he/she would like to enter another set of data. When the user indicates he/she is finished entering data sets, the program will finally display the average largest number and the average smallest number for all sets entered.

Numbers entered: (Need not to be displayed)
1st set: 1, 2, 4, 7, 8, 4.6, 3, 5, 7, 10
2nd set: 2, 5, 6, 7, 7, 7, 7, 7, 7, 9
3rd set: 3, 5, 5, 5, 5, 5, 5, 5, 5, 8

To be displayed:

Low average = 2
High average = 9

Here is my code: (PLEASE DISREGARD MY COMMENTS)
float input;
int count;
float starter;
float high;
float low;


//Get input

cout << "Enter number: ";
cin >> starter;

for (count = 1; count <= 9; count ++)
{
cout << "Enter number: ";
cin >> input;

if (input <= starter)
low = input;
else
high = input;

}
//Calculations


//Clear the screen
system("cls");

//Display
cout << "High is " << high << endl;
cout << "Low is " << low << endl;
//Hold execution on screen
system("pause");

//Indicate to OS successful termination of program
return 0;

When pasting code, please use the code tags. In the format box they show up as <>. Just put your code between the tags that show up.

You didn't ask a question. What do you want us to help you with?

Also, there is no reason to disregard your comments. Other than one being after what it describes rather than before, I think they are good comments for a beginning programmer.
Im sorry about pasting the wrong way. This is my first time asking for professional help.

My logic just wont work with this project. Am I doing this right? My plan was to start off with whatever the 1st number was and if the 2nd number is lower, ill name it low, if the 3rd number is even lower, ill replace low with the 3rd number. If it is higher than the 2nd and the 1st number, ill name it high.

How do I go about this? Please help me.
1
2
3
4
5
6
7
1. Read in number
2. Set low <- number
3. Set high <- number
4. Loop 9 times
    a. Read in number
    b. If number < low : set low <- number
    c. Else If number > high : set high <- number
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Get input

cout << "Enter number: ";
cin >> starter;

low = starter; /// assume first value is lowest
high = starter; /// assume first value is highest

for (count = 1; count <= 9; count ++)
{
    cout << "Enter number: ";
    cin >> input;

    if (input < low) /// compare input against lowest value and not starter which is first value
        low = input;
    if (input > high) /// 
        high = input; 
}
You have to have two nested loops. One externl loop is for number of sequences. And internal loop for entered values of a sequence. Something as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
int total_min, total_max;
int count;
char c;

total_min = total_max = 0;
count = 0;


do
{
   const int LENGTH = 10;
   bool empty = true;
   int min. max;

   cout << "Enter a sequence of " << LENGTH << " integer numbers\n";

   for ( int i = 0; i < LENGTH; i++ )
   {
      int number;

      cout << "number " << i + 1 << ": ";
      cin >> number;

      if ( empty )
      { 
         min = max = number;
         empty = false;
      }
      else
      {
         if ( number < min ) min = number;
         if ( max < number ) max = number;
      }
   }
 
   total_min += min;
   total_max += max;
   ++count;

   cout << "Minimum value is " << min
         << ", and maximum value is " << max
         << end;

   cout << "Continue (y/n)? "
   cin >> c;

} while ( c == 'y' || c == 'Y' );

cout << The average minimum is " << total_min / count
      << ", and the average maximum is " << total_max / count
      << endl;
 

Last edited on
Thank you Everyone!!!!! I used vlad from moscow's code since it is more complete.
However, when displaying "min", if i put a float, it shows a weird number. When I put 1.5 for the lowest, it displays "The lowest number is 5.98839e-039."
Why? How do I fix it? Here is my modified code. Oh, and how important is the
if ( empty ) block?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

int main ()
{
    float TotalMin = 0;
    float TotalMax = 0;
    int count;
    char c;
    float number;
    const int LENGTH = 10;
    float min;
    float max;
    int i;

    do
    {
        cout << "Enter a set of 10 numbers\n";

    for (i = 0; i < LENGTH; i++ )
    {
        cout << "number " << i + 1 << ": ";
        cin >> number;

        if (number < min)
            min = number;
        else if (max < number)
            max = number;
    }

    TotalMin += min;
    TotalMax += max;
    count++;

    cout << "The lowest number is " << min << ", and highest number is " << max
         << endl << endl;

    cout << "Continue (y/n)? ";
    cin >> c;

    }while( c == 'y' || c == 'Y' );

    //Set Formatting to 2 Decimal Places
    cout << fixed << showpoint << setprecision(2);

    //Display Averages
    cout << "The low average is  "<< TotalMin / count << ", and the high"
         << " average is " << TotalMax / count << endl << endl;
}
You get that value because in your code min and max are not initialised.

The if (empty) block in vlad's code is vital since it initialises min and max to the first number of the sequence.
Topic archived. No new replies allowed.