Thread: some problem with my code

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    18

    some problem with my code

    Can you please tell me what I did wrong with this code?

    Code:
    #include <stdio.h>
    
    
    int min(int x, int y);
    
    
    int main(){
        
        int x;
        
        int y;
        
        int minimum;
        
        printf("les inn x: ");
        
        scanf("%d" , &x);
        
        printf("les inn y: ");
        
        scanf("%d" , &y);
        
        minimum=min(x, y);
        
        printf("%d er det minste tallet\n" , minimum);
    
    
        return 0;
    }
    
    
    int min(){
        
        int minimum;
        int x;
        int y;
        
        if (x<=y){
            
            minimum=x;
        }
        
        else if (y<=x){
          
          minimum=y;
        }
            
    
    
        return minimum;
    }
    Im having a real hard time understanding how functions work.

    EDIT: I now see what I did wrong. x and y is not declared to any value in the min function. How can I declare it to get the value from main function? I thought it would happen on its own

    EDIT: I see my mistake and fixed the problem
    Last edited by mangekyou; 04-06-2019 at 04:29 PM.

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Your min() function should be:
    Code:
    int min(int a, int b)
    {
      if ( a < b ) 
        return a;
      return b;
    }

  3. #3
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Theres also an even simpler way:
    Code:
    #define min(a,b) ((a < b) ? a : b)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The logic is essentially the same though, so I daresay it isn't simpler, and after all it also comes with pitfalls: the issue of precedence for complex expressions, and the issue of side effects, as rare as they might be in practice. So to address the former:
    Code:
    #define min(a,b) (((a) < (b)) ? (a) : (b))
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Be careful with the preprocessor - Consider what happens with the following in some sort of loop...
    Code:
    min(++i, MAXIMUM_APPLES);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem or compiler problem....?
    By miloki in forum C Programming
    Replies: 4
    Last Post: 03-05-2015, 12:48 AM
  2. problem with code
    By zeybek258388 in forum Game Programming
    Replies: 0
    Last Post: 06-07-2012, 02:41 PM
  3. Code problem
    By sybariticak47 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2006, 11:50 AM
  4. Have a problem with the following code.
    By dianazheng in forum C Programming
    Replies: 16
    Last Post: 06-05-2005, 04:41 PM
  5. Code problem....
    By subdene in forum C++ Programming
    Replies: 6
    Last Post: 09-12-2002, 08:15 AM

Tags for this Thread