![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 6
| max and min of a list with sentenial at 0 Here is the program ------------------------------------- #include <stdio.h> #include "simpio.h" #include "genlib.h" int main() { int n, min, max; printf("Please enter a list of integers.\n"); printf("Signal the end of your list with 0.\n"); printf("Enter first number: "); n=GetInteger(); { min=n; max=n; while ((n=GetInteger())!=0) { printf("Enter next number: "); if (n<=min) min=n; if (n>=max) max=n; } printf("The maximum number in the list is %d.\n", max); printf("The minimum number in the list is %d.\n", min); } } |
| kbikkasani is offline | |
| | #2 |
| Super Moderator Join Date: Sep 2001
Posts: 4,680
| I don't see any problem (although I don't know why you have a curly brace after your first call to GetInteger). Can you be more specific? Also, we don't have simpio.h and genlib.h, so we can't run your program and help you debug those portions. You posted this in the C forum, but your files are .cpp (C++) and they look like C. Are you doing C or C++? And always use code tags. Surround your code in [ code ] and [ /code ] (without the spaces) and it will format your code nicely (different font, preserve spacing, etc). |
| sean is offline | |
| | #3 |
| Registered User Join Date: Jul 2009
Posts: 6
| Thanks for the tips. The problem is that GetInteger occurs when running the function unnecessarily at one point right after running the program. I believe it is a problem with the while statement but when i do while (n!=0) It fixes the first error but I get the minimum = 0 although that is just supposed to be the sentinel, i will try to post in the c+ forums for help. Thanks again for the advice |
| kbikkasani is offline | |
| | #4 |
| Registered User Join Date: Jan 2008 Location: Seattle
Posts: 476
| Sounds very similar to this thread: Finding smallest and second smallest integer and largest and second largest integer |
| slingerland3g is offline | |
| | #5 | ||
| Webhead Join Date: Jul 2009
Posts: 278
| Quote:
Quote:
Code: int n= 0;
printf("Enter first number: ");
min=n;
max=n;
while ((n=GetInteger())!=0)
{
__________________ Spidey out! | ||
| Spidey is offline | |
| | #6 |
| Registered User Join Date: Jul 2009
Posts: 6
| Thanks so much. Setting n=0 works perfectly. Thank you so much. |
| kbikkasani is offline | |
![]() |
| Tags |
| error, loop, max, min |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| need help finding the max and min of a 2d array | finalreign | C Programming | 6 | 04-29-2009 11:39 AM |
| Max Min Problem | smithc2005 | C Programming | 7 | 10-22-2008 10:38 AM |
| Ranged numbers | Desolation | Game Programming | 8 | 07-25-2006 10:02 PM |
| help finding max and min values from list | ericp023 | C Programming | 7 | 02-22-2003 06:51 PM |
| Double output! | Spurnout | C++ Programming | 3 | 11-02-2002 03:35 AM |