+ 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; }
51 odpowiedzi
+ 6
Yes it works but it should be like
max=min=num;
not num=min=max
+ 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.
+ 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.
+ 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.
+ 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;
}
+ 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.
+ 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>
+ 2
Ipang
Thank you alot❤️❤️❤️ the coed work very well
+ 2
Ipang thank you about your advice
+ 2
Muhammad Heloa Hannan
I understand that. That's why telling you...
And copy paste from what Ipang said to you in last reply..
+ 2
Hehe.. sorry
+ 2
embed your code in the code playground and send the infused links to help people edit it and resend you 😊
+ 1
Take inputs;
Compare in if clause for min, max then print...
do it then show your code or
error if any there..
+ 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
+ 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 👍
+ 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;
}
+ 1
No the min become negative
+ 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;
}
+ 1
Mhh
+ 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;
}