Thread: Modular Programming Help

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    4

    Modular Programming Help

    Hi, I'm looking for help with a homework question. Any assistance will be greatly appreciated. Essentially, I am supposed to write a function (max_of_3), and call it in the main function. I'm getting a logical error, so please tell me if you notice any redundancies or wrong steps I have taken. (Don't mind the part comments.. There were individual parts for my assignment.) The function is supposed to take in three numbers, and find and return the largest of the three. If you look under the issue comment, you will see where I have attempted to call the function to display it.

    Code:
    #include <stdio.h>
    
    
    int max_of_3(int x, int y, int z)
    {
        int l;
        
        if (x > y && x > z)
            l = x;
        else 
            if (y > x && y > z)
            l = y;
        else 
            if (z > x && z > y)
            l = z;         
        
        return (l);
    }
    
    
    
    
    int main(void)
    
    
    {
                                          /*Part 1 & 2*/
        int a, b, c, larger, largest, largest1, smaller, smallest, x, y, z;
        printf ("Enter a number: ");
        scanf ("%d %d %d", &a, &b, &c);
        printf ("\nThe first number entered was %d.", a);
        printf ("\nThe second number entered was %d.", b);
        printf ("\nThe third number entered was %d.", c);
    
    
                                         /*Part 3 & 4*/
        
        if (a > b)
            larger = a;
        else
            larger = b;
        
        printf ("\n\n%d is the larger number of the first two.", larger);    
    
    
        if (larger > c)
            largest = larger;
        else
            largest = c;
        
        printf ("\n\n%d is the largest number.", largest);                                                                   
                                
                                              /*Part 6*/                                                                           
        
        if (a > b)
            smaller = b;
        else
            smaller = a;
        
        printf ("\n\n%d is the smaller number of the first two.", smaller);
        
        if (smaller > c)
            smallest = c;
        else
            smallest = smaller;
        
        printf ("\n\n%d is the smallest number.", smallest);
        
                                              /*Part 7*/     
                                                                                                                    
        printf ("\n\n%d is the middle number.", a+b+c-smallest-largest);
        
                                              /*Part 8*/
                                                                                                                                                     
        printf ("\n\nFrom least to greatest, the numbers are: %d, %d, %d.", smallest, a+b+c-smallest-largest, largest);    
        
                                              /*issue*/
    
    
        largest1 = max_of_3(x, y, z);
        printf ("\n\n\n\n %d", largest1);
         
        return (0);
    }

    Again, any help is very much appreciated. Thanks.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Your understanding of functions is slightly off.

    Here is your function:

    Code:
    int max_of_3(int x, int y, int z)
    That means within this function, there are three integer variables (x, y, and z).

    When you pass arguments to the function, it doesn't matter what they're called. Your function isn't looking for variables named 'x', 'y', and 'z'. It's simply looking for three integer values.

    So in the context of your program, this is not correct:

    Code:
    largest1 = max_of_3(x, y, z);
    ...because 'x', 'y', and 'z' aren't used (though they're declared). In "main()" (where you call this function), you're using 'a', 'b', and 'c'. So those are the variables you want to pass to the function.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    One more point: 'l' (lower case L) is a poor variable name, as in some character sets, it can be confused with the number one or a capital 'i'. You can, and should, make your variable names more descriptive.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    4
    Thanks a lot.. It works. Your explanation was very clear; I understood the moment I read it. I wish I could do more, but your help will contributes to a good mark, and a happy student. I will also keep your second reply in memory. Thanks a lot, God bless you sir.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modular Programming Help
    By DJ_Steve in forum C Programming
    Replies: 21
    Last Post: 09-11-2009, 12:01 AM
  2. modular c programming
    By akrlot in forum C Programming
    Replies: 5
    Last Post: 10-25-2007, 07:00 AM
  3. Modular Programming
    By St0rM-MaN in forum C Programming
    Replies: 7
    Last Post: 05-10-2007, 02:56 AM
  4. modular programming
    By breaka in forum C Programming
    Replies: 4
    Last Post: 08-18-2006, 07:27 AM
  5. Modular programming
    By Longie in forum Linux Programming
    Replies: 1
    Last Post: 07-08-2003, 11:03 PM

Tags for this Thread