# Sum up the largest and smallest ones

• 11-08-2011
programmer1
Sum up the largest and smallest ones
Hi!

I am learning C programming and I need some help. In my project I have to enter n numbers and find the smallest and the largest number. AND I DID THIS!:)

• 11-08-2011
FloridaJo
You did what?
• 11-08-2011
iceaway
Show us the code you have so far.
• 11-08-2011
programmer1
Quote:

Originally Posted by iceaway
Show us the code you have so far.

I am sorry, I am the new comer.Next time I will be more careful.:(

Code:

```#include<stdio.h> main() { int n,m,i,min,max; printf("Please enter a positive integer:"); scanf("%d",&n); printf("Enter integers:"); scanf("%d",&m); max=m; min=m; for(i=2;i<=n;i++) { scanf("%d",&m); if(m<min) min=m; if(m>max) max=m; } printf("The smallest number is %d\nThe greatest number is %d\n",min,max); scanf("%d %d",&min,&max); printf("The sum is : %d\n",min+max); }```

• 11-08-2011
spaghetticode
What is line 22 supposed to do? You're overriding your min and max there. Get rid of that and your summation should work correctly.
• 11-08-2011
programmer1
Thank you so much, Dennis.cpp:devil:. It works :devil: I am so happy!!!!!
• 11-08-2011
spaghetticode
Do you understand why and what's different now? What had you been trying to achieve with that line 22?
• 11-08-2011
programmer1
Quote:

Originally Posted by dennis.cpp
Do you understand why and what's different now? What had you been trying to achieve with that line 22?

Yeah , I got it. Because if I use scanf , the program wants new values for min and max. But I already have min and max! So we have to delete scanf! And program takes my first min and max :wink:. Is it right?
• 11-08-2011
spaghetticode
Right. :)