Link to home
Start Free TrialLog in
Avatar of gothic130
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);
}
ASKER CERTIFIED SOLUTION
Avatar of bobbit31
bobbit31
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gothic130
gothic130

ASKER

>>if (n == 0) min = dat;

 Please could you explain me what this line does?
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>

> 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)
SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
gothic130 your algrythm will work if you just change the initialization as above.
It would also allow you to enter negative numbers.
Thank you all:)