multi function assignment

This is a discussion on multi function assignment within the C Programming forums, part of the General Programming Boards category; This first assignment I didnt have any problems with. The following assignment that is linked to this first assignment is ...

  1. #1
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88

    multi function assignment

    This first assignment I didnt have any problems with. The following assignment that is linked to this first assignment is the one i could use some help with.

    Create a small program based on the following
    (calculate area of a circle and a rectangle):
    //use integers for the length, width, areaR, and radius,
    //use double for areaC
    1. Declare variables: length, width, areaR, radius, and areaC.
    2. Ask the user to enter the length and width of the rectangle and the radius of the circle.
    3. Get the length, width, and radius from the user.
    4. Calculate the area of the rectangle.
    5. Display the length, width, and area of the rectangle onto the screen.
    6. Calculate the area of the circle (use 3.14).
    7. Display the radius and area of the circle onto the screen.



    #
    Code:
    include<stdio.h>;
    	//defines PI at 3.14
    #define PI 3.14
    
    int main(void){
    	// five declared variables
    int length = 0;
    int width = 0;
    int areaR = 0;
    int radius = 0;
    double areaC = 0.0;
    	
    	//Ask the user the length and width of the rectangle
    	printf("Please enter the legnth and width of the rectangle: \n");
    	//Get the length and width from the user 
    	scanf("%d%d", &length, &width);
    
    //Calculate the area of the rectangle
    areaR = length * width;
    //Print the area of the rectangle
    printf("Area of the rectangle is: %d\n", areaR); 
    
    	//Ask the useer for the radius of the circle
    	printf("Please enter the radius of the circle: \n");
    	//Get the radius of the circle
    	scanf("%d", &radius);
    
    //Calculate and are of the circle
    areaC = PI * radius * radius;
    //Print the area of the circle
    printf("The area of the circle is: %lf\n", areaC);  // %lf is for double
    
    return 0;
    
    }


    This is the assignment I need some help with.
    I have attached what i have written so far for this second assignment and am not sure what steps to take next, or if i have written it properly so far.

    Break up the program (areas) from last week into one main function and 4 user-defined functions:

    // gets an integer from the user and returns it
    // make 3 calls to this function:
    // get the length of the rectangle from the user and return it to main
    // get the width of the rectangle from the user and return it to main
    // get the radius of the circle from the user and return it to main
    int GetNum(void);

    // takes two arguments, the length and width of the rectangle and returns the area
    int CalculateAreaR(int length, int width);

    // takes one argument, the radius of the circle and returns the area
    double CalculateAreaC(int radius);



    Code:
    #include <stdio.h>
    #define PI 3.14
    
    int GetNum(void);
    int CalculateAreaR (int length, int width);
    double CalculateAreaC(int radius);
    
    
    int main()
    {
      int length;
      int width;
      double radius;
    
    
    	 printf( "Please enter the length of the rectangle: \n");
     scanf("%d", &length);
    	 printf( "Please enter the width of the rectangle: \n");
     scanf("%d", &width);
    	
    			 printf("The area of the rectangle is %d\n", CalculateAreaR(length, width));
    			 getchar(); 
    
    	printf("PLease enter the radius of the circle: \n");
     scanf("%lf", &radius);
       
    			printf("The area of the circle is %lf\n", CalculateAreaC(radius));
    			getchar(); 
    }
    
    int CalculateAreaR (int length, int width){
      return length * width;
    }
    
    double CalculateAreaC(int radius){
    	return PI * radius * radius;
    }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Looks OK so far, you just need to define your GetNum function (I'd call it GetInt unless your spec says to use GetNum). And I'm assuming your 4th function would be GetDouble()?

    You might want to specify precision for you circle area conversion, i.e. %.3lf, so your number isn't obnoxiously long.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88
    Quote Originally Posted by Tclausex View Post
    Looks OK so far, you just need to define your GetNum function (I'd call it GetInt unless your spec says to use GetNum). And I'm assuming your 4th function would be GetDouble()?

    You might want to specify precision for you circle area conversion, i.e. %.3lf, so your number isn't obnoxiously long.

    Okay so I implemented the GetNum function, but for reasons that are obvious to others i am not getting the program to calculate the area of the rectangle and the area of the circle anymore. What can i focus on to fix this bug??

    Code:
    #include <stdio.h>
    #define PI 3.14
    
    int GetNum(void);
    int CalculateAreaR (int length, int width);
    double CalculateAreaC(int radius);
    
    
    int main()
    {
      int length = 0;
      int width = 0;
      double radius = 0.0;
     
    	 
    	 printf("Please enter the length of the rectangle: \n");
    	 GetNum();
    	 printf("Please enter the width of the rectangle: \n");
         GetNum();
    	
    			 printf("The area of the rectangle is %d\n", CalculateAreaR(length, width));
    			
    
    	printf("PLease enter the radius of the circle: \n");
    	GetNum();
       
    			printf("The area of the circle is %lf\n", CalculateAreaC(radius));
    			
    
    			return ;
    }
    
    
    int GetNum(){
    	scanf("%lf");
    	return ;
    }
    
    int CalculateAreaR (int length, int width){
      return length * width;
    }
    
    double CalculateAreaC(int radius){
    	return PI * radius * radius;
    }

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    How about storing the input somewhere and returning that value from the function so that you can assign it in main() to your variables.

    Bye, Andreas

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    What exactly do you think your GetNum() function is doing? Look at your use of scanf() compared to how you were using in main() previously. You declared GetNum to return an int. Is that what it's doing? And finally, you call GetNum() in main() but then discard the return value.

  7. #7
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88
    Quote Originally Posted by Tclausex View Post
    What exactly do you think your GetNum() function is doing? Look at your use of scanf() compared to how you were using in main() previously. You declared GetNum to return an int. Is that what it's doing? And finally, you call GetNum() in main() but then discard the return value.
    Tclausex,
    Thank you for your response

    I have taken all what you said into consideration. If you would please continue to be patient with me and my amateur programming skills.

    The problem now with the program is that it doesn't calculate the area of the rectangle and the circle. I've been fooling around with the code and I cannot seem to figure out where my mistakes are.

    If you could continue to help me out I would greatly appreciate it. I have not had much sleep in the last couple days and my Cprogramming book for my class still has not arrived in the mail #thanksAmazon.


    Code:
    #include <stdio.h>
    #define PI 3.14
    
    
    int GetNum(void);
    int CalculateAreaR (int length, int width);
    double GetInt(void); 
    double CalculateAreaC(double radius);
     
     
    int main(){
      int length = 0;
      int width = 0;
      double radius = 0.0;
          
        printf("Please enter the length and width of the rectangle: \n");
        GetNum();
    	
    
        printf("The area of the rectangle is: %d\n", CalculateAreaR(length, width));
             
     
        printf("PLease enter the radius of the circle: \n");
        GetInt();
    	
        
        printf("The area of the circle is: %lf\n", CalculateAreaC(radius));
                 
     
         return 0;
    }
    
    
     int length;
     int width;
     double radius;
     
    int GetNum(void){
        scanf_s("%d%d", &length, &width);
    	printf("The length is: %d and the width is: %d\n", length, width);
    	return length, width;
    }
    
    double GetInt(void){
    	scanf_s(" %lf", &radius);
    	printf("The radius is: %lf\n", radius);
    	return radius;
    }
    
    int CalculateAreaR(int length, int width){
    	int areaR;
    	areaR = length * width;
        return areaR;
    }
     
    double CalculateAreaC(double radius){
    	double areaC;
    	areaC = PI * radius * radius;
        return areaC;
    }

  8. #8
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88
    Quote Originally Posted by CASHOUT View Post
    Tclausex,
    Thank you for your response

    I have taken all what you said into consideration. If you would please continue to be patient with me and my amateur programming skills.

    The problem now with the program is that it doesn't calculate the area of the rectangle and the circle. I've been fooling around with the code and I cannot seem to figure out where my mistakes are.

    If you could continue to help me out I would greatly appreciate it. I have not had much sleep in the last couple days and my Cprogramming book for my class still has not arrived in the mail #thanksAmazon.


    Code:
    #include <stdio.h>
    #define PI 3.14
    
    
    int GetNum(void);
    int CalculateAreaR (int length, int width);
    double GetInt(void); 
    double CalculateAreaC(double radius);
     
     
    int main(){
      int length = 0;
      int width = 0;
      double radius = 0.0;
          
        printf("Please enter the length and width of the rectangle: \n");
        GetNum();
    	
    
        printf("The area of the rectangle is: %d\n", CalculateAreaR(length, width));
             
     
        printf("PLease enter the radius of the circle: \n");
        GetInt();
    	
        
        printf("The area of the circle is: %lf\n", CalculateAreaC(radius));
                 
     
         return 0;
    }
    
    
     int length;
     int width;
     double radius;
     
    int GetNum(void){
        scanf_s("%d%d", &length, &width);
    	printf("The length is: %d and the width is: %d\n", length, width);
    	return length, width;
    }
    
    double GetInt(void){
    	scanf_s(" %lf", &radius);
    	printf("The radius is: %lf\n", radius);
    	return radius;
    }
    
    int CalculateAreaR(int length, int width){
    	int areaR;
    	areaR = length * width;
        return areaR;
    }
     
    double CalculateAreaC(double radius){
    	double areaC;
    	areaC = PI * radius * radius;
        return areaC;
    }



    Okay, the program now runs fine except for it doesn't calculate the area of the rectangle. From what I can tell everything else works fine. Please help if you can. Thank you


    Code:
    #include <stdio.h>
    #define PI 3.14
    
    
    int GetNum(void);
    int CalculateAreaR (int length, int width);
    double GetInt(void); 
    double CalculateAreaC(double radius);
     
     
    int main(void){
      int length = 0;
      int width = 0;
      double radius = 0.0;
          
        printf("Please enter the length and width of the rectangle: \n");
        length, width = GetNum();
    	
    
        printf("The area of the rectangle is: %d\n", CalculateAreaR(length, width));
             
     
        printf("Please enter the radius of the circle: \n");
        radius = GetInt();
    	
        
    	printf("The area of the circle is: %lf\n", CalculateAreaC(radius));
           
        return 0;
    }
    
    
    
    int GetNum(void){
    
     int length;
     int width;
    	scanf("%d%d", &length, &width);
    	printf(" The length is: %d\n The width is: %d\n", length, width);
    	return length, width;
    }
    
    double GetInt(void){
    
    double radius;
    	scanf(" %lf", &radius);
    	printf("The radius is: %lf\n", radius);
    	return radius;
    }
    
    int CalculateAreaR(int length, int width){
    	return length * width;
    }
     
    double CalculateAreaC(double radius){
    	return PI * radius * radius;
    }

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    1,056
    Quote Originally Posted by CASHOUT View Post
    Okay, the program now runs fine except for it doesn't calculate the area of the rectangle.

    Code:
    int main(void){
    // ...
        printf("Please enter the length and width of the rectangle: \n");
        length, width = GetNum();
    // ...
    int GetNum(void){
    
     int length;
     int width;
    // ...
         return length, width;
    }
    The above is not correct: you can't pass multiple parameters like that. A return type of int means exactly one integer. Also, the comma operator cannot be used to assign values to multiple objects. Compile with higher levels of warnings to catch errors like these.

  10. #10
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    I'd suggest compiling with warnings turned on, -Wall -Wextra for starters. It will help you find your mistakes.

    Code:
    line 17 -   length, width = GetNum();
    What you need to understand is that a function can only return 1 value. Also, a function should be designed to accomplish 1 task. Designing GetNum therefore to print a specific prompt when what it is really for is reading an integer is going outside of its domain. You'll want to print the specific prompts in main() and you'll need 2 calls to GetNum, one for the length, and one for the width.

    And just a semantic issue, but you call a function that returns a double, GetInt. It would be more appropriately called GetDouble. Again, move the prompt to main.

    In the meantime, until you get your book, feel free to check out
    C Tutorial - Learn C - Cprogramming.com
    or
    C Programming Notes

  11. #11
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88
    Quote Originally Posted by Tclausex View Post
    I'd suggest compiling with warnings turned on, -Wall -Wextra for starters. It will help you find your mistakes.

    Code:
    line 17 -   length, width = GetNum();
    What you need to understand is that a function can only return 1 value. Also, a function should be designed to accomplish 1 task. Designing GetNum therefore to print a specific prompt when what it is really for is reading an integer is going outside of its domain. You'll want to print the specific prompts in main() and you'll need 2 calls to GetNum, one for the length, and one for the width.

    And just a semantic issue, but you call a function that returns a double, GetInt. It would be more appropriately called GetDouble. Again, move the prompt to main.

    In the meantime, until you get your book, feel free to check out
    C Tutorial - Learn C - Cprogramming.com
    or
    C Programming Notes

    Thank you
    After understanding where my mistakes were, I came up with this:


    Code:
    #include <stdio.h>
    #define PI 3.14
    
    
    int GetNum1(void);
    int GetNum2(void);
    double GetNum3(void);
    int CalculateAreaR (int length, int width);
    double CalculateAreaC(double radius);
     
     
    int main(void){
    
    	int length = 0;
    	int width = 0;
    	double radius = 0.0;
          
        printf("Please enter the length of the rectangle: \n");
        length = GetNum1();
    	printf("Please enter the width of the rectangle: \n");
    	width = GetNum2();
    	
    
        printf("The area of the rectangle is: %d\n", CalculateAreaR(length, width));
             
     
        printf("Please enter the radius of the circle: \n");
        radius = GetNum3();
    	
        
    	printf("The area of the circle is: %lf\n", CalculateAreaC(radius));
           
        return 0;
    }
    
    
    
    int GetNum1(void){
    
     int length;
    	scanf_s(" %d", &length);
    	printf("The length is: %d\n", length);
    	return length;
    }
    
    
    int GetNum2(void){
    
    int width;
    	scanf_s(" %d", &width);
    	printf("The width is: %d\n",width);
    	return width;
    }
    
    
    double GetNum3(void){
    
    double radius;
    	scanf_s(" %lf", &radius);
    	printf("The radius is: %lf\n", radius);
    	return radius;
    }
    
    
    int CalculateAreaR(int length, int width){
    	return length * width;
    }
     
    
    double CalculateAreaC(double radius){
    	return PI * radius * radius;
    }

    Would this be acceptable to turn in considering the directions given for the program which is posted at the top of this tread?

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by CASHOUT View Post
    Would this be acceptable to turn in considering the directions given for the program which is posted at the top of this tread?
    IMHO, no.

    According to the description you have posted you should write one function
    Code:
    int GetNum(void)
    and call it three times, but you have written three different functions.

    If you carefully look at your three functions you will notice a general pattern. Use this pattern to write a general function which reads one integer and returns it. And don't include the printf()-call in the function. A function should only do one thing (in your case only the input).

    Bye, Andreas

  13. #13
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Indeed, read my last post more carefully.

    I assumed you'd need a separate function to get the value for radius because you declared radius as a double and not an int in your second block of code in your original post.
    Read your assignment spec carefully. If radius is an int, then you only need GetNum for your length, width, and radius.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. single function multi sturct
    By bandal27 in forum C Programming
    Replies: 6
    Last Post: 01-07-2009, 01:25 PM
  2. Multi-struct function?
    By DavidDobson in forum C Programming
    Replies: 13
    Last Post: 10-12-2008, 08:05 PM
  3. Replies: 2
    Last Post: 03-24-2007, 06:30 AM
  4. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM
  5. multi array to function
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-09-2001, 03:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21