### Find maximum number!

I am trying to find the maximum number from the numbers entered from a keyboard.Anyone with a short approach?

Thats is my code

// Code to find the maximum number
# include<iostream>
using namespace std;
const int values = 30;
int main ()
{
int num[values];
int max , i;

cout << "Please enter 5 numbers, so that i show you the maximum \n";
cin >> num[0] >>num[1] >>num[2] >> num[3] >>num[4];

max = num[0];

for (int i=0; i<5; i++)
if (max < num[i])
max = num[i];
cout << "The maxmum value is:" << max << endl ;

system ("pause");
return 0;
}
You should firstly use a for loop to streamline that entry code.
 ``12`` ``````for (int x = 0; x < 5; x++) cin >> num;``````

You know how that works, right? You don't need an array because there is only one number to keep - the maximum.
Then, set max to the first number entered. So on the first iteration, set max to the current number.
After getting another number, check if it's higher than the current maximum. If it is, set max to the current number. If not, leave max as is.
That's it.
Of course you could put each number in an array or vector, sort the container in ascending order
and then print the last element which will be the max value.
Alternatively if you use the above method you could include a line static max=0 immediately below the for loop and another line: if ( num>max)num=max immediately after cin>>num
Then num is max so print num is max.
 ``123456789`` ``````int max(0); for (int x = 0; x < 5; x++) { while(!(cin >> num)) { std::cout << "That wasn't a valid number. Please enter another. " << std::endl; } max = (0 == x) ? num : std::max(max, num) }``````

I haven't tested that but I think it will work. Doesn't hurt to add some error checking.
Am still lost in it, where are my going wrong?

// Code to find the maximum number
# include<iostream>
using namespace std;

int main ()
{
int num;
int max = num , i;

cout << "Please enter 5 numbers, so that i show you the maximum \n";
cin >> num;

for (int i=0; i<5; i++)

if ( num>max)num=max;

cout << "The maxmum value is:" << max << endl ;

system ("pause");
return 0;
}
closed account (z05DSL3A)
 ``1234567891011121314151617181920212223`` ``````// Code to find the maximum number # include using namespace std; int main () { int num(0); // semantically equal to: int num = 0; int max(0); cout << "Please enter 5 numbers, so that i show you the maximum \n"; for (int i(0); i<5; i++) { cin >> num; if ( num > max) { max = num; } } cout << "The maxmum value is: " << max << endl ; system ("pause"); return 0; }``````
Last edited on
I tried the above code but no output!!
closed account (z05DSL3A)
What, no output at all? I would suggest you go back and look at it again, you must have done something wrong.
#include<iostream.h>
main()
{
int numb[1000];
int input,i,max;

cout<<"how many number u want to input: ";
cin>>input; //to know how many number that u want to input

for(i=1;i<=input;i++) //input data
{
cout<<"numb"<<i<<": ";
cin>>numb[i];
}

max=numb[1];

for(i=1;i<=input;i++) //compare data with max
{
if(numb[i]>max)
max=numb[i];
}
cout<<"Max number is: "<<max;
}
Grey Wolf,

if you init max value by 0, and an user will not input positive numbers(he will input only negative) then the user will be suprised, because the biggest value is 0, but he did not enter it.
closed account (z05DSL3A)
Denis,

I was 'fixing up' the code posted by gnwillix88. I figured that if he can't get his code working, initializing max to the largest negative number would probably blow his mind so I left it at zero (better that than initializing to an uninitialized variable). There is also no error handling incase the user enters a letter or other erroneous input.

edit:
 ``1234567891011121314151617181920212223242526272829303132`` ``````#include #include #include #include #include int main() { double number(0); // semantically equal to: int num = 0; double maxNumber(std::numeric_limits::min()); std::string input(""); std::cout << "Please enter 5 numbers, so that i show you the maximum \n"; for (int i(0); i<5; i++) { while(true) { std::cout << i+1 << ": "; getline(std::cin, input); std::stringstream ss(input); if (ss >> number) break; std::cout << "Invalid number, please try again" << std::endl; } maxNumber = std::max(number, maxNumber); } std::cout << "The maxmum value is: " << maxNumber << std::endl ; system ("pause"); return 0; }``````
Last edited on
This is the shortest and simplyest solution:

 ``123456789101112131415`` ``````#include using namespace std; int main () { int a[5],i,max; for (i=0;i<5;i++) { cin>>a[i]; if (i==0) max=a[i]; if (a[i]>max) max=a[i]; } cout<<"max numb is:"<

Here he will have all inputs in array (if its needed for other operations) and he will have a maximum of all of them. And this works for negative numbers also.

Here is code for finding max of 2 numbers without using if/else, while for or anything like that

 ``1234567891011121314`` ``````#include using namespace std; int main () { int a,b; cout<<"a:"; cin>>a; cout<<"b:"; cin>>b; cout<< ( (a+b) + abs(a-b) ) / 2; //for maximum cout<< ( (a+b) - abs(a-b) ) / 2; //for minimum return 0; }``````
Last edited on
Topic archived. No new replies allowed.