The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Muti-function assignment
Discuss Muti-function assignment in the C Programming forum on Dev Shed. Muti-function assignment C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 31st, 2013, 10:54 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 6
Time spent in forums: 1 h 33 m 56 sec
Reputation 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;
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;
}
|

February 1st, 2013, 01:10 AM
|
 |
Contributed User
|
|
|
|
|
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()
|

February 1st, 2013, 12:59 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 6
Time spent in forums: 1 h 33 m 56 sec
Reputation Power: 0
|
|
Quote: | 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);
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;
}
|

February 1st, 2013, 02:11 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Quote: | 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;
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 ;
}
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.
|

February 2nd, 2013, 07:25 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 6
Time spent in forums: 1 h 33 m 56 sec
Reputation Power: 0
|
|
Quote: | 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;
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 ;
}
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);
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;
}
|

February 2nd, 2013, 09:28 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 6
Time spent in forums: 1 h 33 m 56 sec
Reputation 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);
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;
}
|

February 2nd, 2013, 10:54 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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.
|

February 3rd, 2013, 01:38 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 6
Time spent in forums: 1 h 33 m 56 sec
Reputation Power: 0
|
|
Quote: | 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);
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;
}
What do you think?
Would this be acceptable to turn in considering the directions given for the program which is posted at the top of this tread?
|

February 3rd, 2013, 02:33 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
You have five functions whereas your instructor wanted four. One of the basic ideas behind functions is that you can take a common task that's being done in several places and split that one common task off into a function, thus reducing the size and complexity of the program. In my first professional software job, I was given another programmer's code to maintain. Her experience was in FORTRAN, so that is how she had written it, about a page of code that was repeated over and over again with different inputs, but the exact same processing. Her listing was nearly 40 pages long. I split that common code off into a procedure (this was in Pascal) to which I passed the different sets of inputs and immediately cut the listing down to less than 8 pages of code that was much easier to read.
By having two different functions for your two int values, you are defeating the educational purpose of the exercise. Those two functions are the same except for the diagnostic printf. You could keep that printf in the function by passing in a string identifying the variable name, or far better you could move that printf out of the function and place it immediately after the function call. In fact, if you had done that second option to begin with, you would have immediately noticed that you weren't getting the values back from the function calls.
And make your function names more descriptive. One function gets a double from the user, so why not call it GetDouble? Another gets an int, so why not GetInt? That way, when you read that function call in your code, you have a much better idea of what that function does.
Got to rush off now.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|