By applying for loop, your program should find and display the largest and the lowest of the numbers.

Q)Write a C++ program that receives the total number of integers (N). Then, the program will
ask for N real numbers. By applying for loop, your program should find and display the
largest and the lowest of the numbers. Give a proper message for invalid user input.If the input is below 0 print real numbers only and prompt again ,if enter 0 print wrong input all repetition until the user enters number greater than 1.


problem 1) Min and max values are same
problem 2) I also have to add some conditions on which

"NOT A HOMEWORK PROBLEM :D WE DONT GET SUCH COMPLICATED EXERCISES HERE JUST SOME PRACTICE PROBLEMS FOUND FROM IT DEPARTMENT"

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
  
#include<iostream>
using namespace std;

int main()
{

bool empty_ = true;

   int num=0;
   int min_ =0;
   int max_=0;
    int integ = 0;


        cout <<"ENTER" ;
        cin >>integ;

    for(int i=0;i<integ;i++)
    {      cout << "Enter a number: ";
            cin >>num;
                min_ = num;
                max_ = num;

        if (max_ < num)
            {
                max_ = num;
            }
            else if (num < min_)
            {
                min_ = num;
            }


    }
        cout << "MIN VALUE: "<<min_<<endl;
        cout << "MAX VALUE: "<<max_<<endl;
    return 0;
}
Last edited on
You reset the min and max every time on lines 22 and 23. Currently, lines 25-32 are pointless, because lines 22 and 23 already do what they do.

Think about this differently. Do you really want to set the min and max every time through the loop? What if you could set them to some initial value outside the loop, then perform checks inside the loop for every number?

Think about this and ask us questions.
Topic archived. No new replies allowed.