Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |
 User Name: Password: Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

 Add This Thread To: Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb
 « Previous Thread | Next Thread » Thread Tools Search this Thread Rate Thread Display Modes
 Dev Shed Forums Sponsor:
#1
January 31st, 2013, 10:54 PM
 cashout
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;
}```

#2
February 1st, 2013, 01:10 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 3,840
Time spent in forums: 2 Months 3 Weeks 2 Days 20 h 44 m 40 sec
Reputation Power: 1774
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()
__________________
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
February 1st, 2013, 12:59 PM
 cashout
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;
}
```

#4
February 1st, 2013, 02:11 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,142
Time spent in forums: 2 Months 2 Weeks 4 Days 8 m 43 sec
Reputation Power: 1974
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.

#5
February 2nd, 2013, 07:25 PM
 cashout
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;
}```

#6
February 2nd, 2013, 09:28 PM
 cashout
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;
}```

#7
February 2nd, 2013, 10:54 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,142
Time spent in forums: 2 Months 2 Weeks 4 Days 8 m 43 sec
Reputation Power: 1974
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
February 3rd, 2013, 01:38 PM
 cashout
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?

#9
February 3rd, 2013, 02:33 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,142
Time spent in forums: 2 Months 2 Weeks 4 Days 8 m 43 sec
Reputation Power: 1974
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.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Muti-function assignment

## Developer Shed Advertisers and Affiliates

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap