Thread: Issue with function calls

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    2

    Issue with function calls

    Hello! I'm working on an assignment question as follows:
    Screenshot by Lightshot

    I've written the following code:
    Code:
    /*    File: lumberfunc.c
        This file reads base and height from an input file then uses 3 function to compute the values of the required engineering properties.
    */
    
    #include<stdio.h>
    
    float area(int base,int height) 
    {
        float result;
        
        result = base*height;
        
        return result;
    }
    
    float mod(int height)
    {
        float area,result;
        
        result = (area*height)/6.0;
        
        return result;
    }
    
    float inertia(int height)
    {
        float mod,result;
        
        result = (mod*height)/2.0;
        
        return result;
    }
    
    int main(void)
    {
        int base, height;
        float csarea, moinertia, secmod;
        
        FILE * fin = fopen("lumberin.txt","r");
        FILE * fout = fopen("lumberout.txt","w");
        
        fprintf(fout,"Lumber Size      Cross sectional      Moment of      Section\n                     Area             Inertia        Modulus\n--------------------------------------------------------------\n");
        
        while(fscanf(fin,"%d %d\n",&base,&height) != EOF) {
            
            csarea=area(base,height);
            
            secmod=mod(height);
            
            moinertia=inertia(height);
            
            fprintf(fout," %d x  %d             %.2f              %.2f           %.2f\n",base,height,csarea,moinertia,secmod);
        }
        
        fclose(fout);
        
        return 0;
    }
    My output is as follows:
    Screenshot by Lightshot

    Now, formatting issues aside, my Moment of Inertia isn't calculating correctly. In the inertia() function, for some reason mod = area, even though in main, secmod = mod is returning the correct value. Could someone explain to me what's going wrong here and how to fix it?

    Thank you for your time!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > float mod,result;
    mod here is an uninitialised local variable, and nothing to do with the result of the mod function you just called in main.

    Maybe pass mod from main to this function as another parameter.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    2
    Unfortunately the assignment specifically wants inertia() to call modulus().

    I replaced the modulus and area variables with proper function calls and it works now:
    Code:
    /*    File: lumberfunc.c
        This file reads base and height from an input file then uses 3 function to compute the values of the required engineering properties.
        
        Programmer: Sara Stredulinsky    Date: 15-Oct-19
    */
    
    #include<stdio.h>
    
    /*Prototypes*/
    float area(int base,int height);
    float modulus(int base, int height);
    float inertia(int base, int height);
    
    int main(void)
    {
        int base, height;
        float csarea, moinertia, secmod;
        
        FILE * fin = fopen("lumberin.txt","r");
        FILE * fout = fopen("lumberout.txt","w");
        
        fprintf(fout,"Lumber Size      Cross sectional      Moment of      Section\n                     Area             Inertia        Modulus\n--------------------------------------------------------------\n");
        
        while(fscanf(fin,"%d %d\n",&base,&height) != EOF) {
            
            csarea=area(base,height);
            
            secmod=modulus(base,height);
            
            moinertia=inertia(base,height);
            
            fprintf(fout," %d x  %d             %.2f              %.2f           %.2f\n",base,height,csarea,moinertia,secmod);
        }
        
        fclose(fout);
        
        return 0;
    }
    
    float area(int base,int height) 
    {
        float result;
        
        result = base*height;
        
        return result;
    }
    
    float modulus(int base,int height)
    {
        float result;
        
        result = (area(base,height)*height)/6.0;
        
        return result;
    }
    
    float inertia(int base, int height)
    {
        float result;
        
        result = (modulus(base,height)*height)/2.0;
        
        return result;
    }
    But I still don't get why in the original code modulus() correctly calls area() and uses ((base*height)*height)/6.0 as its calculation, but inertia() which looks like it's built identically to me, calls area() again for ((base*height)*height)/2.0 instead of calling modulus to make (((base*height)*height))/2.0. Can anyone explain what was happening there?

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by LioButtons View Post
    But I still don't get why in the original code modulus() correctly calls area() and uses ((base*height)*height)/6.0 as its calculation, but inertia() which looks like it's built identically to me, calls area() again for ((base*height)*height)/2.0 instead of calling modulus to make (((base*height)*height))/2.0. Can anyone explain what was happening there?
    In your original code

    Code:
    float mod(int height)
    {
        float area,result;
         
        result = (area*height)/6.0;
         
        return result;
    }
    What is the value of 'area'?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    float area(int base,int height) 
    {
        float result;
        
        result = base*height;
        printf("DEBUG: area=%f\n",result);
        
        return result;
    }
    
    float modulus(int base,int height)
    {
        float result;
        
        result = (area(base,height)*height)/6.0;
        printf("DEBUG: modulus=%f\n",result);
        
        return result;
    }
    
    float inertia(int base, int height)
    {
        float result;
        
        result = (modulus(base,height)*height)/2.0;
        printf("DEBUG: inertia=%f\n",result);
        
        return result;
    }
    I see area called 3 times, and returning the same answer each time.
    I see modulus called twice, and returning the same answer each time.

    Code:
    $ ./a.out 
    Lumber Size      Cross sectional      Moment of      Section
                         Area             Inertia        Modulus
    --------------------------------------------------------------
    10 20
    DEBUG: area=200.000000
    DEBUG: area=200.000000
    DEBUG: modulus=666.666687
    DEBUG: area=200.000000
    DEBUG: modulus=666.666687
    DEBUG: inertia=6666.666992
     10 x  20             200.00              6666.67           666.67
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function calls
    By Finnla in forum C++ Programming
    Replies: 3
    Last Post: 09-10-2012, 10:57 AM
  2. pointers and function calls inside a function
    By lexitron in forum C Programming
    Replies: 1
    Last Post: 12-03-2011, 05:43 PM
  3. Replies: 19
    Last Post: 09-08-2011, 07:56 AM
  4. Issue with gethostbyname() calls.
    By mcmillhj in forum C Programming
    Replies: 7
    Last Post: 02-16-2011, 12:30 AM
  5. arg...function calls
    By 2fastwrx in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2004, 10:55 PM

Tags for this Thread