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:
#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;
elseif(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.
<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().
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.
#include<iostream>
usingnamespace 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;
elseif(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.