# multi function assignment

• 01-31-2013
CASHOUT
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

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; }```
• 01-31-2013
Tclausex
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.
• 02-01-2013
Salem
• 02-01-2013
CASHOUT
Quote:

Originally Posted by Tclausex
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; }```
• 02-01-2013
AndiPersti
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
• 02-01-2013
Tclausex
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.
• 02-02-2013
CASHOUT
Quote:

Originally Posted by Tclausex
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,

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; }```
• 02-02-2013
CASHOUT
Quote:

Originally Posted by CASHOUT
Tclausex,

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; }```
• 02-02-2013
c99tutorial
Quote:

Originally Posted by CASHOUT
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.
• 02-02-2013
Tclausex
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
• 02-03-2013
CASHOUT
Quote:

Originally Posted by Tclausex
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?
• 02-03-2013
AndiPersti
Quote:

Originally Posted by CASHOUT
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
• 02-03-2013
Tclausex
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.