Printing out maximum and minimum numbers in a set of entered integers using while loop

I'm going to explain how you can create a program that prints out the largest(maximum) and the lowest(minimum) integers in a specified set of integers.
I used Borland C++ IDE but any C++ compiler can work.
Here is the code:
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
#include<iostream>
#include<conio>
void main()
{
int a,max,n,min,i=1;
cout << "This program will arrange your numbers in ascending and";
cout << " descending order" << endl<< endl;
cout << "How many numbers do you want to enter? ";
cin >>n;
cout << endl;
cout << "Enter your numbers: " << endl;
cin >>a;
max = min =a; //initializing max and min.
while(i<n)
{
cin>>a;
if(min>a)
min=a;
else if(max<a)
max=a;
i++;
}
cout<<"The Largest number is " << max << endl;
cout<<"The lowest number is " << min << endl;
getch();

} 

I think hope this will help you guys who have tried to google on how to print out maximum and minimum integers using loops.
but any C++ compiler can work

<conio.h> is not a standard header file, meaning it is up to the discretion of the makers of the compiler to decide to include it. So this is not correct.

Also, most compilers (should) have a problem with void main(). You should be using int main().
Last edited on
Also, the code above does not make use of namespaces, so the cout, cin, endl etc will not be recognised in a modern compiler.
What I meant is that, you can use any compiler to write that program as long as you know what header files are recognized by your compiler. For example, if I were to use Code Blocks, then I would not put #include<conio> because it would not be recognized by code blocks.
Apart from the differences in the header files, the code in the main function remains the same!!!
Moschops it is not necessary to use namespace with Borland because it already recognizes the standard c++ insertion and extraction operators.
Namespace inclusion is done mostly to modern compilers like how I have done it in the code below using Code Blocks.
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
#include<iostream>

using namespace std;

int main()
{
float a,max,min;//float has been used to allow entering of decimal numbers
int n,i=1;
cout << "This program will arrange your numbers in ascending and";
cout << " descending order" << endl<< endl;
cout << "How many numbers do you want to enter? ";
cin >>n;
cout << endl;
cout << "Enter your numbers: " << endl;
cin >>a;
max = min =a; //initializing max and min.
while(i<n)
{
cin>>a;
if(min>a)
min=a;
else if(max<a)
max=a;
i++;
}
cout<<"The Largest number is " << max << endl;
cout<<"The lowest number is " << min << endl;
cin.get();
cin.get();

return 0;
}
Thanks. Now I know that apart from the standard algorithm std::minmax_element I can use your code when an input iterator is used.
Only I have one remark that your algorithm does not work for an empty sequence for example when the user pressed Ctrl+Z in Windows without entering any value.
Moschops it is not necessary to use namespace with Borland because it already recognizes the standard c++ insertion and extraction operators.


It is not necessary to use namespace with Borland because Borland is a 15 year old compiler from before C++ was formalised. What you're coding there is not C++ and to present it here, to people hoping to learn, is the kind of thing that does more harm than good. Next thing we know you'll be recommending people use Turbo C++ or Dev-Cpp.
Topic archived. No new replies allowed.