The wrong with if funtion

Le
767204882
Hi,I am a new C++ learner.The follow is my program to compare the
largest and the smallest number in five numbers.However ,it always
produces wrong results.More strangely ,the largest output right,while
the smallest wrong.I don't known why


#include<stdio.h>

int main()
{
int num1,num2,num3,num4,num5;
int max=num1;
int min=num5;

printf("Enter the five numbers:");
scanf("%d %d %d %d %d",&num1,&num2,&num3,&num4,&num5);

if(num2>max)
max=num2;
if(num3>max)
max=num3;
if(num4>max)
max=num4;
if(num5>max)
max=num5;
printf("The largest number is %d",max);

if(num1<min)
min=num1;
if(num2<min)
min=num2;
if(num3<min)
min=num3;
if(num4<min)
min=num4;
printf("The smallest number is %d",min);
return 0;
}
Vos réponses
Gagnez chaque mois un abonnement Premium avec GNT : Inscrivez-vous !
Trier par : date / pertinence
xylo
Le #17700251
Le Thu, 30 Oct 2008 23:27:09 -0700, 767204882 a écrit:

int num1,num2,num3,num4,num5;
int max=num1;
int min=num5;



/*
wrong, you must assign min and max after you get your five numbers:
int max,
min;
*/


printf("Enter the five numbers:n");
scanf("%d %d %d %d %d",&num1,&num2,&num3,&num4,&num5);



/* assign min an max here */
max = num1;
min = num5;

if(num2>max)
max=num2;
if(num3>max)
max=num3;
if(num4>max)
max=num4;
if(num5>max)
max=num5;
printf("The largest number is %d",max);

if(num1<min)
min=num1;
if(num2<min)
min=num2;
if(num3<min)
min=num3;
if(num4<min)
min=num4;
printf("The smallest number is %d",min);





--
Apply rot13 to this e-mail address before using it.
JM Marino
http://jm.marino.free.fr
Publicité
Poster une réponse
Anonyme