+ 2

How can i fix this coed to show minimum?

#include <iostream> using namespace std; int main() { int num,max,min; cout<<"enter the number :"<<endl; for(int i=0;i<10;i++) { cout<<i+1<<"-"; cin>>num; if (num>max) max=num; if (num<min) min=num; } cout<<"max="<<max<<endl; cout<<"min="<<min<<endl; }

27th Nov 2019, 1:15 PM
Muhammad Heloa Hannan
Muhammad Heloa Hannan - avatar
51 Réponses
+ 6
Yes it works but it should be like max=min=num; not num=min=max
27th Nov 2019, 4:22 PM
Jayakrishna 🇮🇳
+ 6
Muhammad Heloa Hannan Then you have to do something like this. int arr[10]; for (int i=0; i<10; i++){ cin>>arr[i]; } This is how you take inputs.
27th Nov 2019, 2:36 PM
Avinesh
Avinesh - avatar
+ 3
Muhammad Heloa Hannan First of all there is no need of for loop if you just need to compare the two variables for max and min. A simple if condition will do.
27th Nov 2019, 1:29 PM
Avinesh
Avinesh - avatar
+ 3
Muhammad Heloa Hannan I can understand that you cannot understand what I'm saying. Isn't that a for loop? You need to write one more for loop with if conditions to get your answer. Edit: Anyways ignore it.
27th Nov 2019, 2:53 PM
Avinesh
Avinesh - avatar
+ 2
Muhammad Heloa Hannan Kindly see this code and rectify your errors. #include <iostream> using namespace std; int main(){ int num1,num2,max,min; cout<<"enter two numbers"<<endl; cin>>num1>>num2; if (num1>num2){ max=num1; min=num2; } else{ max=num2; min=num1; } cout<<"max = "<<max<<endl; cout<<"min = "<<min<<endl; }
27th Nov 2019, 2:26 PM
Avinesh
Avinesh - avatar
+ 2
Muhammad Heloa Hannan , Can you give the ten number examples for testing? I don't understand what's the problem with min become negative. If one of the input was a negative number it is expected. Also, min can equal max if all the ten numbers entered are the same. About the code, it is pretty close to what I meant to say. I would do: cin >> num; min = max = num; // assume 1st input as min & max Just before the for loop block.
27th Nov 2019, 4:12 PM
Ipang
+ 2
cin >> num; min = max = num; // write like this exactly num = min = max; // but not like this, this will // copy garbage value from // <max> to <min> and <num>
27th Nov 2019, 4:23 PM
Ipang
+ 2
Ipang Thank you alot❤️❤️❤️ the coed work very well
27th Nov 2019, 4:37 PM
Muhammad Heloa Hannan
Muhammad Heloa Hannan - avatar
+ 2
Ipang thank you about your advice
27th Nov 2019, 4:47 PM
Muhammad Heloa Hannan
Muhammad Heloa Hannan - avatar
+ 2
Muhammad Heloa Hannan I understand that. That's why telling you... And copy paste from what Ipang said to you in last reply..
27th Nov 2019, 4:56 PM
Jayakrishna 🇮🇳
+ 2
Hehe.. sorry
28th Nov 2019, 12:58 PM
Nerb
Nerb - avatar
+ 2
embed your code in the code playground and send the infused links to help people edit it and resend you 😊
28th Nov 2019, 4:40 PM
Aditya
Aditya - avatar
+ 1
Take inputs; Compare in if clause for min, max then print... do it then show your code or error if any there..
27th Nov 2019, 1:23 PM
Jayakrishna 🇮🇳
+ 1
I knew the comparative between two number i look like a way for 10 number to show me max and min by using for loop
27th Nov 2019, 2:30 PM
Muhammad Heloa Hannan
Muhammad Heloa Hannan - avatar
+ 1
Take the first number before the loop. This number will be assumed as both min and max value. Then you setup the loop to start from one rather than zero (because we already have the first number). The rest is the same, if num > max then max = num, and if num < min then min = num. Try it out, then we can talk again 👍
27th Nov 2019, 2:36 PM
Ipang
+ 1
(Ipang) Please can you help me or send the right coed You said exactly like our university doctor #include <iostream> using namespace std; int main() { int num,max,min; num=0; if (num=0) max=min; cout<<"enter the number :"<<endl; for(int i=1;i<10;i++) { cout<<i+1<<"-"; cin>>num; if (num>max) max=num; if (num<min) min=num; } cout<<"max="<<max<<endl; cout<<"min="<<min<<endl; }
27th Nov 2019, 2:52 PM
Muhammad Heloa Hannan
Muhammad Heloa Hannan - avatar
+ 1
No the min become negative
27th Nov 2019, 3:37 PM
Muhammad Heloa Hannan
Muhammad Heloa Hannan - avatar
+ 1
#include <iostream> using namespace std ; int main() { int num,max,min; cout<<"enter the number :"<<endl; cin>>num; num=min=max; for(int i=1;i<10;i++) { cout<<i+1<<"-"; cin>>num; if (num>max) max=num; if (num<min) min=num; } cout<<"max="<<max<<endl; cout<<"min="<<min<<endl; }
27th Nov 2019, 4:19 PM
Muhammad Heloa Hannan
Muhammad Heloa Hannan - avatar
+ 1
Mhh
28th Nov 2019, 5:35 PM
Minah
Minah - avatar
+ 1
this is another way simlar from the first way without int first number out for loop #include <iostream> using namespace std ; int main() { int i,num,min=INT_MAX,max=INT_MIN; cout<<"enter your number "<<endl; for (i=0;i<10;i++) { cout<<i+1<<"_"; cin>>num; if (num>max) max=num; if (num<min) min=num; } cout<<"max ="<<max<<endl; cout<<"min ="<<min<<endl; }
29th Nov 2019, 7:24 AM
Muhammad Heloa Hannan
Muhammad Heloa Hannan - avatar