1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
6
Rep Power
0

#### Muti-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;
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);

//Get the radius of the circle

//Calculate and are of the circle
//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);

int main()
{
int length;
int width;

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("The area of the circle is %lf\n", CalculateAreaC(radius));
getchar();
}

int CalculateAreaR (int length, int width){
return length * width;
}

}```
2. Looks good so far.

All you need is to implement and use the getnum function as described.

> Break up the program (areas) from last week into one main function and 4 user-defined functions:
But where is function number 4?
You've only listed 3 of them.

Mind you, a 4th one might be contrived to being something like displayPrompt() or getPI()
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
6
Rep Power
0
Originally Posted by salem
Looks good so far.

All you need is to implement and use the getnum function as described.

> Break up the program (areas) from last week into one main function and 4 user-defined functions:
But where is function number 4?
You've only listed 3 of them.

Mind you, a 4th one might be contrived to being something like displayPrompt() or getPI()

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);

int main()
{
int length = 0;
int width = 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));

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;
}

}```
4. Originally Posted by cashout
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??
Well, let's see:
Code:
```int GetNum()
{
scanf("%lf");
return ;
}```
Two things here. You're wanting scanf to read in a double, but the function wants to return that value as an int. That means that you will lose precision.

Second, you're not returning any value. Sure, you're using return, but you're giving it nothing to return.

Third, you've not even giving scanf any address to store the converted double value into!

Conclusion: your GetNum function is totally frakked up. As your compiler had to have told you. What did your warning messages say? Are you ignoring those warnings? Don't you know that warnings are much more important than error messages? Never ignore warnings!

Now let's look at how you call GetNum:
Code:
```int main()
{
int length = 0;
int width = 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));

GetNum();

printf("The area of the circle is %lf\n", CalculateAreaC(radius));

return ;
}```
Except for the initialization in their declarations, where do you ever assign any value to length, width, or radius? You call GetNum a number of times and then you always throw away whatever value it returns! You tell it to get a number and then you completely ignore what it gives you. How could you ever possibly expect to have the values you input be used when you always throw those values away? Of course, that's ignoring how frakked up GetNum is in the first place; who knows what garbage value it would return to you if you didn't always throw that value away.

Plus you did not give main any value to return.

Correct GetNum so that it works. Then actually use the value that it returns.
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
6
Rep Power
0
Originally Posted by dwise1_aol
Well, let's see:
Code:
```int GetNum()
{
scanf("%lf");
return ;
}```
Two things here. You're wanting scanf to read in a double, but the function wants to return that value as an int. That means that you will lose precision.

Second, you're not returning any value. Sure, you're using return, but you're giving it nothing to return.

Third, you've not even giving scanf any address to store the converted double value into!

Conclusion: your GetNum function is totally frakked up. As your compiler had to have told you. What did your warning messages say? Are you ignoring those warnings? Don't you know that warnings are much more important than error messages? Never ignore warnings!

Now let's look at how you call GetNum:
Code:
```int main()
{
int length = 0;
int width = 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));

GetNum();

printf("The area of the circle is %lf\n", CalculateAreaC(radius));

return ;
}```
Except for the initialization in their declarations, where do you ever assign any value to length, width, or radius? You call GetNum a number of times and then you always throw away whatever value it returns! You tell it to get a number and then you completely ignore what it gives you. How could you ever possibly expect to have the values you input be used when you always throw those values away? Of course, that's ignoring how frakked up GetNum is in the first place; who knows what garbage value it would return to you if you didn't always throw that value away.

Plus you did not give main any value to return.

Correct GetNum so that it works. Then actually use the value that it returns.

dwise1_aol,
Thank you for your detailed reply. 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);

int main(){
int length = 0;
int width = 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));

GetInt();

printf("The area of the circle is: %lf\n", CalculateAreaC(radius));

return 0;
}

int length;
int width;

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){
}

int CalculateAreaR(int length, int width){
int areaR;
areaR = length * width;
return areaR;
}

double areaC;
return areaC;
}```
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
6
Rep Power
0
Okay, the program runs fine except for it doesn't calculate the area of the rectangle. Everything else works great. Please help

Code:
```
#include <stdio.h>

#define PI 3.14

int GetNum(void);
int CalculateAreaR (int length, int width);
double GetInt(void);

int main(void){
int length = 0;
int width = 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("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){

}

int CalculateAreaR(int length, int width){
return length * width;
}

}```
7. OK, here's the problem:
length, width = GetNum();
C functions only return one single value. You cannot return two values that way. There is a way to do it, but that involves pointers and I assume that that is a subject for later.

Also, the comma operator doesn't work the way you think. What that statement actually does is that first it evaluates length, which you had initialized to zero, and then it does the second part which is to call GetNum() and assign the return value to width. So length never gets changed.

Then in GetNum(), you have another two statements spliced together with the comma operator:
return length, width;
In this case, you would return length and then evaluate width, but because the return length part causes you to return from the function, the second part is never evaluated.

Actually, you will mainly only see the comma operator used in for-statements. Except for mistakes made by beginners, I've never seen the comma operator used anywhere near where you did.

I recommend that GetNum only scanf in one integer value and return that one value. Then you make two separate function calls, one for length and the second for width.

BTW, congratulations on two things:
1. Using code tags from the start.
2. Posting the code again reflecting the changes you made. Too often, the person will only mention having made the changes and then say it still doesn't work, but we have no idea what they did.
Both of those things go a long way in helping us to help you. Keep it up.
8. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
6
Rep Power
0
Originally Posted by dwise1_aol
OK, here's the problem:
length, width = GetNum();
C functions only return one single value. You cannot return two values that way. There is a way to do it, but that involves pointers and I assume that that is a subject for later.

Also, the comma operator doesn't work the way you think. What that statement actually does is that first it evaluates length, which you had initialized to zero, and then it does the second part which is to call GetNum() and assign the return value to width. So length never gets changed.

Then in GetNum(), you have another two statements spliced together with the comma operator:
return length, width;
In this case, you would return length and then evaluate width, but because the return length part causes you to return from the function, the second part is never evaluated.

Actually, you will mainly only see the comma operator used in for-statements. Except for mistakes made by beginners, I've never seen the comma operator used anywhere near where you did.

I recommend that GetNum only scanf in one integer value and return that one value. Then you make two separate function calls, one for length and the second for width.

BTW, congratulations on two things:
1. Using code tags from the start.
2. Posting the code again reflecting the changes you made. Too often, the person will only mention having made the changes and then say it still doesn't work, but we have no idea what they did.
Both of those things go a long way in helping us to help you. Keep it up.
Thanks again dwise1_aol
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);

int main(void){

int length = 0;
int width = 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("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){

}

int CalculateAreaR(int length, int width){
return length * width;
}