Thread: Minimum/maximum number problem. Please help!

  1. #31
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Please remove the line "min != 0;" from your program, recompile, run it.

    ....

    Now re-read my post.

  2. #32
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by AndrewHunter View Post
    Ok, so the easiest way to do this would be:
    Code:
    num = GetInteger();
    max=min=num;
    while.....
    And != does not work as an assignment operator.
    Every way I test my program it works with != 0. Is there any instance where if I input something it doesn't work? Seems to me that it's just not grammatically correct.

  3. #33
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Well, let's see I ran this:
    Code:
    #include <stdio.h>
    
    int main(void){
    
         int num;
    
         num != 0;
    
         printf("%d", num);
    
         return(0);
    }
    and my output was -858993460.


    The variable hasn't been initialized so anything you try to compare it with will result in undefined behaviour. Which means no one knows what it will do. I guarantee that this will not be accepted by your teacher. It isn't gramatically correct because it isn't an assignment operator, thus you never put any value into your variable min.
    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.

  4. #34
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by nonpuz View Post
    Please remove the line "min != 0;" from your program, recompile, run it.

    ....

    Now re-read my post.
    Oh, I am sorry. I never said what you said was wrong, I just said my way worked. I now realized that min = 0 is unnecessary. It is no longer in my code.

    Here is what my code now looks like:

    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.\nSignal the end of the list with 0.\n");
          printf ("Enter your first number: ");
          num = GetInteger();
          max = 0;
          while (num!=0)
          {
                if (num < min)
                min = num;
                if (num > max)
                max = num;
                printf ("Enter your next number: ");
                num = GetInteger();
                }
                printf ("\nThe maximum number in the list is %d.\n", max);
                printf ("The minimum number in the list is %d.\n", min);
                getchar();
          }
    I'm pretty sure that it is flawless now. Correct me if I am wrong.

  5. #35
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by AndrewHunter View Post
    Well, let's see I ran this:
    Code:
    #include <stdio.h>
    
    int main(void){
    
         int num;
    
         num != 0;
    
         printf("%d", num);
    
         return(0);
    }
    and my output was -858993460.


    The variable hasn't been initialized so anything you try to compare it with will result in undefined behaviour. Which means no one knows what it will do. I guarantee that this will not be accepted by your teacher. It isn't gramatically correct because it isn't an assignment operator, thus you never put any value into your variable min.
    Is my code correct now? ^^

  6. #36
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Ok, you are wrong. You cannot use a variable before you intialize it. See my above post.

    EDIT: And of course this:
    Quote Originally Posted by AndrewHunter View Post
    Ok, so the easiest way to do this would be:
    Code:
    num = GetInteger();
    max=min=num;
    while.....
    Last edited by AndrewHunter; 07-14-2011 at 11:58 PM.
    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. #37
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by AndrewHunter View Post
    Ok, you are wrong. You cannot use a variable before you intialize it. See my above post.
    EDIT: And of course this:
    Quote Originally Posted by AndrewHunter View Post
    Ok, so the easiest way to do this would be:
    Code:
    num = GetInteger();
    max=min=num;
    while.....
    I'm sort of confused on this one. Are you talking about max = 0? If I don't have that then the program doesn't work.

    EDIT: I get it now.

    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.\nSignal the end of the list with 0.\n");
          printf ("Enter your first number: ");
          num = GetInteger();
          max=min=num;
          while (num!=0)
          {
                if (num < min)
                min = num;
                if (num > max)
                max = num;
                printf ("Enter your next number: ");
                num = GetInteger();
                }
                printf ("\nThe maximum number in the list is %d.\n", max);
                printf ("The minimum number in the list is %d.\n", min);
                getchar();
          }
    Is this correct?
    Last edited by PYROMANIAC702; 07-15-2011 at 12:03 AM.

  8. #38
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Code:
    main()
    
    {
          int min, max, num;
          
          printf ("This will compute the minimum and maximum number in the list.\nSignal the end of the list with 0.\n");
          printf ("Enter your first number: ");
          num = GetInteger();
          max = 0;
          while (num!=0)
          {
                if (num < min)WHAT IS THE VALUE OF MIN RIGHT NOW?
                min = num;
                if (num > max)
                max = num;
                printf ("Enter your next number: ");
                num = GetInteger();
                }
                printf ("\nThe maximum number in the list is %d.\n", max);
                printf ("The minimum number in the list is %d.\n", min);
                getchar();
          }
    The easiest way to do this is:
    Code:
    printf ("This will compute the minimum and maximum number in the list.\nSignal the end of the list with 0.\n");
          printf ("Enter your first number: ");
          num = GetInteger();
          max = min=num;
          while (num!=0)
          {....
    This way before entering your while loop max and min are both initialized to a value, namely the first number entered. Which if you follow the logic of how you would do it by paper works. Watch keep track of max and min for me, ready?

    First number: 5

    So what is the max and min of the number sequence I gave you?
    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.

  9. #39
    Registered User
    Join Date
    Jul 2011
    Location
    Bangalore,India
    Posts
    24
    Quote Originally Posted by PYROMANIAC702 View Post
    EDIT: And of course this:


    I'm sort of confused on this one. Are you talking about max = 0? If I don't have that then the program doesn't work.

    EDIT: I get it now.

    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.\nSignal the end of the list with 0.\n");
          printf ("Enter your first number: ");
          num = GetInteger();
          max=min=num;
          while (num!=0)
          {
                if (num < min)
                min = num;
                if (num > max)
                max = num;
                printf ("Enter your next number: ");
                num = GetInteger();
                }
                printf ("\nThe maximum number in the list is %d.\n", max);
                printf ("The minimum number in the list is %d.\n", min);
                getchar();
          }
    Is this correct?
    yes it is correct now...

  10. #40
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by AndrewHunter View Post
    Code:
    main()
    
    {
          int min, max, num;
          
          printf ("This will compute the minimum and maximum number in the list.\nSignal the end of the list with 0.\n");
          printf ("Enter your first number: ");
          num = GetInteger();
          max = 0;
          while (num!=0)
          {
                if (num < min)WHAT IS THE VALUE OF MIN RIGHT NOW?
                min = num;
                if (num > max)
                max = num;
                printf ("Enter your next number: ");
                num = GetInteger();
                }
                printf ("\nThe maximum number in the list is %d.\n", max);
                printf ("The minimum number in the list is %d.\n", min);
                getchar();
          }
    The easiest way to do this is:
    Code:
    printf ("This will compute the minimum and maximum number in the list.\nSignal the end of the list with 0.\n");
          printf ("Enter your first number: ");
          num = GetInteger();
          max = min=num;
          while (num!=0)
          {....
    This way before entering your while loop max and min are both initialized to a value, namely the first number entered. Which if you follow the logic of how you would do it by paper works. Watch keep track of max and min for me, ready?

    First number: 5

    So what is the max and min of the number sequence I gave you?
    I understand your frustration. I am a coding newbie (as you can tell) and everything you say to me really means a lot and I respect every word that you say. I added the part you told me to in the post above in the EDIT. I hope I did it right.

  11. #41
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by subhash.rao View Post
    yes it is correct now...
    GREAT! Thank you guys so much!!!

  12. #42
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    It's ok, I'm not getting frustrated I was just trying to show you where in the code and it is hard to do without caps and colors. The code in the edit looks good, as long as you understand the logic. Is this a summer program thing?
    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. #43
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by AndrewHunter View Post
    It's ok, I'm not getting frustrated I was just trying to show you where in the code and it is hard to do without caps and colors. The code in the edit looks good, as long as you understand the logic. Is this a summer program thing?
    No, I just really wanted to learn how to program so I signed up. It's all year round, but you buy each class. This one is for 3 months and It ends August 1st.

  14. #44
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Ok, well after the class then take a look at getting a good compiler. May I suggest:

    Pelles C A great compiler and IDE but it is just C
    MS Visual Studio Express Great compiler and IDE and does C/C++

    Note both of these compilers are free. Also keep in mind you are learning how to program with this class but you are not learning standard C. If you want to get into that then our FAQ and tutorial board is a great place to start.
    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.

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