Can't find minimum (C-Program)

Jul 17, 2014 at 12:08pm
I'm making a program in which user enters the number of pancakes eaten by 10 people. Then the computer tells the max and mim number of pancakes eaten.

The max is coming out right but min is always wrong. Please tell me what I'm doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  #include <stdio.h>

int main(void)
{	
   int people[10];
   int pancakes;
   int pancakes_max=0;
   int pancakes_min=;
   int i;
   
   for(i=0; i<10; i++)
   {
   	   printf("The number of pancakes eaten by person %d: ", i);
   	   scanf("%d", &people[i]);
   	   
   	   
   	   if (people[i] > pancakes_max)
	   {
	   	  pancakes_max = people[i];
	   }
	   
	   if (people[i] < pancakes_min)
	   {
	   	  pancakes_min = people[i];
	   }
	   
   }
   
      
   printf("The maximum number of pancakes eaten are %d.\n", pancakes_max);
   printf("The minimum number of pancakes eaten are %d.\n", pancakes_min);
   
	getch();
}
Jul 17, 2014 at 12:16pm
You need to initialize pancakes_min to a value before you can compare it in line 22. (I'd use something larger than any value that people[i] will contain.)
Last edited on Jul 17, 2014 at 12:16pm
Jul 17, 2014 at 12:23pm
@ wildblue: Thanks. It worked :)

Btw in this same program I'm required to find the PERSON who ate the maximum and minimum number of pancakes. I can't seem to figure out that how will I find that.
Jul 17, 2014 at 12:31pm
I'm required to find the PERSON who ate the maximum and minimum number of pancakes.
in your program, the only way to distinguish one person from another is by their number, the integer i in your loop.

All you need to do is to add two new integer variables to store the number. Then at the place in the code where pancakes_max and pancakes_min are modified, simply store the current value of i in the corresponding variable. Print each out at the end, with the other results.
Jul 17, 2014 at 3:50pm
@ Chervil: I think I understood the concept but I don't know how to do that in the program. I know its better for me to make it myself. But I just can't.

So, can you please code that bit for me?
Jul 17, 2014 at 3:58pm
Well, at least have a go, show the code for your attempt and we'll take it from there. If you're stuck because you don't really understand what needs to be done, feel free to ask for clarification.
Last edited on Jul 17, 2014 at 4:27pm
Topic archived. No new replies allowed.