Thread: max and min of a list with sentenial at 0

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    6

    max and min of a list with sentenial at 0

    Im having trouble with the smoothness of running this program. If you run it you will see the program right away. The program completes the task but uses a GetInteger function unnecessarily in the third line. Any help would be appreciated.

    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);
    }
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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).

  3. #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

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603

  5. #5
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Quote Originally Posted by kbikkasani View Post
    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
    The problem is that you are calling GetInteger twice the first time.
    printf("Enter first number: ");
    n=GetInteger();
    {
    min=n;
    max=n;
    while ((n=GetInteger())!=0)
    {
    You can fix this by removing the first getInteger altogether, as the while loop will not enter if you enter 0. But, you must first initialize n to 0 as your setting min and max to 0. Also, the curly brace after the first getInteger is not required.

    Code:
    int n= 0;
    printf("Enter first number: ");
    min=n;
    max=n;
    while ((n=GetInteger())!=0)
    {
    Spidey out!

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Thanks so much. Setting n=0 works perfectly. Thank you so much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help finding the max and min of a 2d array
    By finalreign in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 11:39 AM
  2. Max Min Problem
    By smithc2005 in forum C Programming
    Replies: 7
    Last Post: 10-22-2008, 10:38 AM
  3. Ranged numbers
    By Desolation in forum Game Programming
    Replies: 8
    Last Post: 07-25-2006, 10:02 PM
  4. help finding max and min values from list
    By ericp023 in forum C Programming
    Replies: 7
    Last Post: 02-22-2003, 06:51 PM
  5. Double output!
    By Spurnout in forum C++ Programming
    Replies: 3
    Last Post: 11-02-2002, 03:35 AM

Tags for this Thread