gothic130
asked on
Urgent!!! Can't find the problem...
I wrote a program to find the bigger and the smaller number in a list but there's something wrong and don't know what it is becuse the program just gives me the biggest number while in the smaller number always appear 0. Thanks for your help!!!
Here it is:
#include <stdio.h>
void main()
{
int arr[10], n, dat, max=0, min=0;
for(n=0;n<=9;n++)
{
printf("Number: ");
scanf("%d",&dat);
if(dat>max)max=dat;
if(dat<min)min=dat;
}
printf("Bigger %d Smaller %d",max,min);
}
Here it is:
#include <stdio.h>
void main()
{
int arr[10], n, dat, max=0, min=0;
for(n=0;n<=9;n++)
{
printf("Number: ");
scanf("%d",&dat);
if(dat>max)max=dat;
if(dat<min)min=dat;
}
printf("Bigger %d Smaller %d",max,min);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You should initialize min to INT_MAX instead of 0
You should initialize max to INT_MIN instead of 0
They defined in <limits.h>
You should initialize max to INT_MIN instead of 0
They defined in <limits.h>
> Please could you explain me what this line does?
basically, your min starts out at 0... if all the numbers in the array are positive, min will always be 0.
what the line above does is it sets min to the first element in the array... if the first element is the min then it stays the min, otherwise it'll keep going through and update min accordingly (note that if (n==0) will only happen once)
basically, your min starts out at 0... if all the numbers in the array are positive, min will always be 0.
what the line above does is it sets min to the first element in the array... if the first element is the min then it stays the min, otherwise it'll keep going through and update min accordingly (note that if (n==0) will only happen once)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
gothic130 your algrythm will work if you just change the initialization as above.
It would also allow you to enter negative numbers.
It would also allow you to enter negative numbers.
ASKER
Thank you all:)
ASKER
Please could you explain me what this line does?