#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Find MAX and MIN without array!


    Hi! I'm new to C programming and I need some help by making a program that reads an input of integers and then finds the two highest and the two lowest values and also make an average on those. And i'm not allowed to use arrays.

    I think i'm supposed to include these in my program.
    #include <limits.h>
    INT_MAX
    INT_MIN

    Thanx for any help!
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    what have you done so far?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by bullet
    what have you done so far?
    Not much at all.. I dont understand how INT_MAX and INT_MIN works.

    (this is me trying something...)
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int main()
    {
    int max, min, values;
    max = INT_MAX;
    min = INT_MIN;
    printf("Enter integers");
    scanf("%d", &values);
    INT_MAX = values;
    printf("\nBiggest value: %d\n", max);
    
    return 0;
    }
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,396
    Rep Power
    1871
    Get someone to read you a list of numbers, and work out the steps you go through to remember the highest and lowest number you've heard so far.

    When you understand that, then you can do the same for two highest and two lowest.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,182
    Rep Power
    2222
    Originally Posted by CarlJ
    Not much at all.. I dont understand how INT_MAX and INT_MIN works.
    Do you know anything about what the pre-processor does? Or about what it does with macros? If not, then read C preprocessor. For that matter, read it even if you do know.

    If a keyword starts with a pound sign (#), then it's a preprocessor directive. Before the compiler even starts, the preprocessor modifies the source file. When you #include a header file, the preprocessor literally inserts that header file into the source file. When you #define a macro (such as INT_MAX and INT_MIN), then wherever that macro name is encountered in the source code it gets replaced by the text definition of that macro. That is called macro expansion.

    Open limits.h in an editor and read the #define statements for INT_MAX and INT_MIN. You will find limits.h in your compiler's INCLUDE directory. In the limits.h file for MinGW gcc, those #define's are:
    Code:
    /*
     * Maximum and minimum values for ints.
     */
    #define INT_MAX		2147483647
    #define INT_MIN		(-INT_MAX-1)
    So then everywhere that we find INT_MAX, the preprocessor will replace it with 2147483647, which is the maximum value that an int can hold -- and, since the size of int is implementation-dependent, it tells me that I'm using a 32-bit compiler.

    BTW, I just used code tags. They are needed when display code here because they preserve your code's indentation and hence your code does not lose its readability. Readability, the ability of others to read your code, is extremely important.

    Readability requires that we use a clear indentation style and that we use it consistently. You are doing neither. You must learn to do it. I strongly recommend the Allman style, which you can learn by following that link. Failure to indent your code and/or to use code tags will garner you rebukes of increasing intensity. You do not want that. Besides, indenting is far more for your own benefit, because you more than anybody else must be able to read your own code -- we have seen several people here make mistakes in their own code because they could not even read their own code, so that is a very real problem, especially for beginners.

    Here is what your code properly indented and properly posted:
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int main()
    {
        int max, min, values;
    
        max = INT_MAX;
        min = INT_MIN;
        printf("Enter integers");
        scanf("%d", &values);
        INT_MAX = values;
        printf("\nBiggest value: %d\n", max);
    
        return 0;
    }
    To see the effects of macro expansion, all you need to do is to make that substitution yourself. Using the macro definitions I posted above:
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int main()
    {
        int max, min, values;
    
        max = 2147483647;
        min = (-2147483647-1);
        printf("Enter integers");
        scanf("%d", &values);
        2147483647 = values;
        printf("\nBiggest value: %d\n", max);
    
        return 0;
    }
    Do you see the line I highlighted in red? Had you tried to compile that code yet? If you had, then you would have gotten an error message like this: "invalid lvalue in assignment". Why didn't you mention that error to us?

    Do you know what an lvalue is? It is the left side of an assignment. Do you know what an assignment statement does? It evaluates the right side of the assignment, the rvalue, and stores it at the memory location indicated by the lvalue. That means that the lvalue must evaluate to an address, such as a variable name would. A constant value, such as 2147483647, does not evaluate to an address and hence is not a valid lvalue.

    Do you understand now how INT_MAX and INT_MIN work?
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by dwise1_aol
    Do you know anything about what the pre-processor does? Or about what it does with macros? If not, then read C preprocessor. For that matter, read it even if you do know.

    If a keyword starts with a pound sign (#), then it's a preprocessor directive. Before the compiler even starts, the preprocessor modifies the source file. When you #include a header file, the preprocessor literally inserts that header file into the source file. When you #define a macro (such as INT_MAX and INT_MIN), then wherever that macro name is encountered in the source code it gets replaced by the text definition of that macro. That is called macro expansion.

    Open limits.h in an editor and read the #define statements for INT_MAX and INT_MIN. You will find limits.h in your compiler's INCLUDE directory. In the limits.h file for MinGW gcc, those #define's are:
    Code:
    /*
     * Maximum and minimum values for ints.
     */
    #define INT_MAX		2147483647
    #define INT_MIN		(-INT_MAX-1)
    So then everywhere that we find INT_MAX, the preprocessor will replace it with 2147483647, which is the maximum value that an int can hold -- and, since the size of int is implementation-dependent, it tells me that I'm using a 32-bit compiler.

    BTW, I just used code tags. They are needed when display code here because they preserve your code's indentation and hence your code does not lose its readability. Readability, the ability of others to read your code, is extremely important.

    Readability requires that we use a clear indentation style and that we use it consistently. You are doing neither. You must learn to do it. I strongly recommend the Allman style, which you can learn by following that link. Failure to indent your code and/or to use code tags will garner you rebukes of increasing intensity. You do not want that. Besides, indenting is far more for your own benefit, because you more than anybody else must be able to read your own code -- we have seen several people here make mistakes in their own code because they could not even read their own code, so that is a very real problem, especially for beginners.

    Here is what your code properly indented and properly posted:
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int main()
    {
        int max, min, values;
    
        max = INT_MAX;
        min = INT_MIN;
        printf("Enter integers");
        scanf("%d", &values);
        INT_MAX = values;
        printf("\nBiggest value: %d\n", max);
    
        return 0;
    }
    To see the effects of macro expansion, all you need to do is to make that substitution yourself. Using the macro definitions I posted above:
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int main()
    {
        int max, min, values;
    
        max = 2147483647;
        min = (-2147483647-1);
        printf("Enter integers");
        scanf("%d", &values);
        2147483647 = values;
        printf("\nBiggest value: %d\n", max);
    
        return 0;
    }
    Do you see the line I highlighted in red? Had you tried to compile that code yet? If you had, then you would have gotten an error message like this: "invalid lvalue in assignment". Why didn't you mention that error to us?

    Do you know what an lvalue is? It is the left side of an assignment. Do you know what an assignment statement does? It evaluates the right side of the assignment, the rvalue, and stores it at the memory location indicated by the lvalue. That means that the lvalue must evaluate to an address, such as a variable name would. A constant value, such as 2147483647, does not evaluate to an address and hence is not a valid lvalue.

    Do you understand now how INT_MAX and INT_MIN work?
    Yes, it's much clearer now. Thank you for your time to explain, it helpt alot. Really appreciate it!

IMN logo majestic logo threadwatch logo seochat tools logo