Thread: Minimum/maximum number problem. Please help!

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    51

    Minimum/maximum number problem. Please help!

    I am trying to right code for an assignment I have, which is to take an infinite number of integers and then display the minimum and maximum number. With my code in the output screen it says that the minimum and maximum numbers are both 0.

    Here's what i have:

    Code:
    #include <stdio.h>
    #include "genlib.h"
    #include "simpio.h"
    
    main()
    
    {
          int min, max, num;
          
          printf ("This will compute the minimum and maximum number in the list. Signal the end of the list with 0.\n");
          printf ("Enter your first number: ");
          num = GetInteger();
          min = 0;
          max = 0;
          while (num!=0)
          {
                if (num < min)
                num = min;
                else if (num > max)
                num = max;
                else num = num;
                printf ("Enter your next number: ");
                num = GetInteger();
                }
                printf ("The maximum number in the list is %d.\n", max);
                printf ("The minimum number in the list is %d.\n", min);
                getchar();
          }

  2. #2
    Registered User
    Join Date
    Jul 2011
    Location
    Bangalore,India
    Posts
    24
    I will suggest u a few changes...
    1)change main() to int main(void)
    2)assign min to the maximum number that can be inputted lik say min=9999
    3)change the line num=min to min=num
    4)change the line num=max to max=num

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're assignments are backwards. And your braces aren't aligned. And ... well, here:

    Code:
    #include <stdio.h>
    #include "genlib.h"
    #include "simpio.h"
    
    
    int main(void)
    {
          int min, max, num;
          
          printf ("This will compute the minimum and maximum number in the list. Signal the end of the list with 0.\n");
          printf ("Enter your first number: ");
          num = GetInteger();
          min = 0;
          max = 0;
          while (num!=0)
          {
                if (num < min)
                num = min;  <--- backwards
                else if (num > max)
                num = max;  <--- backwards
                else num = num;
                printf ("Enter your next number: ");
                num = GetInteger();
          }
          printf ("The maximum number in the list is %d.\n", max);
          printf ("The minimum number in the list is %d.\n", min);
          getchar();
    
          return 0;
    }

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by subhash.rao View Post
    I will suggest u a few changes...
    1)change main() to int main(void)
    2)assign min to the maximum number that can be inputted lik say min=9999
    3)change the line num=min to min=num
    4)change the line num=max to max=num
    wow... In my head it was max = num, but i put num = max. DERP! Thanks hash. step 1 and 2 weren't necessary though.

    I now have a new problem . 0 signals the end of the list, but now it's saying that the minimum number is 0. I have to input 0 for the loop to stop. Is there any way around this?

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Quote Originally Posted by PYROMANIAC702 View Post
    wow... In my head it was max = num, but i put num = max. DERP! Thanks hash. step 1 and 2 weren't necessary though.

    I now have a new problem . 0 signals the end of the list, but now it's saying that the minimum number is 0. I have to input 0 for the loop to stop. Is there any way around this?
    Actually the new problem is because of not listening to step 2

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by PYROMANIAC702 View Post
    step 1 and 2 weren't necessary though.
    Which compiler are you using? And what course are you taking that you are using this psuedo managed c stuff?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Change it to a "do ... while" loop so that the loop criteria terminates the loop before cycling back and writing over "min".

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by Matticus View Post
    You're assignments are backwards. And your braces aren't aligned. And ... well, here:
    That is how my compiler aligns the braces, so that has nothing to do with the problem. I have not learned return 0 in my class yet, so if I put it in my program, they will know i got outside help. The backwards assignments were the whole problem. If you could help me with my new problem, I would appreciate it. Thanks for the help!

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by AndrewHunter View Post
    Which compiler are you using? And what course are you taking that you are using this psuedo managed c stuff?
    I am using Dev-C++ with Windows 7 64 bit. The compiler is both for C and C++. The class I am taking is the Stanford EPGY Introduction to C. (EPGY = Education Program for Gifted Youth)

  10. #10
    Registered User
    Join Date
    Jul 2011
    Location
    Bangalore,India
    Posts
    24
    Quote Originally Posted by Matticus View Post
    Change it to a "do ... while" loop so that the loop criteria terminates the loop before cycling back and writing over "min".
    what matticus is trying to say is use
    Code:
    do
          {
                if (num < min)
                num = min;
                else if (num > max)
                num = max;
                else num = num;
                printf ("Enter your next number: ");
                num = GetInteger();
          }while (num!=0);
    now if u follow the step two i said...it will give the minimum of the entered numbrs neglecting the final 0..
    Last edited by subhash.rao; 07-14-2011 at 11:17 PM.

  11. #11
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by nonpuz View Post
    Actually the new problem is because of not listening to step 2
    Step 2 TOTALLY messes up the program. Say I input 76 and 8. It puts the max number to 0 and the minimum to 8.

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Hmm....well Matticus is right, but do what you need to do for your class. Are you allowed to use a do-while loop like Matticus suggested?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by subhash.rao View Post
    what matticus is trying to say is use
    Code:
    do
          {
                if (num < min)
                num = min;
                else if (num > max)
                num = max;
                else num = num;
                printf ("Enter your next number: ");
                num = GetInteger();
          }while (num!=0);
    I wish it was that easy! I haven't learned that in my class yet .

  14. #14
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Post your updated code and note that the code that hash posted still has the assignments backwards.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  15. #15
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by AndrewHunter View Post
    Hmm....well Matticus is right, but do what you need to do for your class. Are you allowed to use a do-while loop like Matticus suggested?
    No, I am not. Here is the full assignment:

    Epgy Lesson Number – 15600.
    5.5. The while statement
    5.5.3. Using sentinel values
    Due: End of week 7
    #1. Write a program that gets a list of integers from the user and computes the minimal
    and maximal number appearing in the list.
    The file name should be: minMax.c
    Run your program twice on the following inputs:
    A. 570, 6, 94, 76, 2, 0 (0 = sentinel value)
    B. 78, 50, 4, 9, 0
    The output for A, for example should be:
    The maximum number in the list is 570.
    The minimum number in the list is 2.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. finding mean minimum and maximum values
    By begginer in forum C Programming
    Replies: 1
    Last Post: 03-25-2011, 11:55 PM
  2. maximum and minimum
    By aslak in forum C Programming
    Replies: 35
    Last Post: 12-14-2008, 03:54 PM
  3. Displaying Minimum and Maximum values from input
    By mgardnertech in forum C Programming
    Replies: 1
    Last Post: 06-29-2008, 08:47 PM
  4. Maximum and Minimum Values
    By swaugh in forum C Programming
    Replies: 7
    Last Post: 12-16-2006, 09:43 PM
  5. Maximum And Minimum
    By drdodirty2002 in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2004, 12:39 AM