Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 28th, 2008, 12:44 AM   #1
WalterSlopeboi
Newbie
 
Join Date: Jan 2008
Posts: 9
Rep Power: 0 WalterSlopeboi is on a distinguished road
Does this code look correct?

Assignment:

The program asks for the user to input the radius and height of a right
cylinder. It then calculates surface area and volume of this cylinder
and prints the out-put on the screen in the FORMAT GIVEN BELOW. The
main program calls following three functions to accomplish the above job.
calc_area (double rad, double height)
calc_volume (double rad, double height)
and
print_info (void)

II. Helpful Math Hints:
Surface area = 2*PI*r*h + PI*r*r; and volume = PI * r*r*h

III. Helpful Programming Hints:

1. Preprocessor define PI= 3.14159
2. Declare PROTOTYPE FUNCTIONS shown above with proper return
parameters.
3. Define the three programs mentioned above, in region out-side your
main program.
3. Declare variables r, d, h, V, A as “double”.
4. Ask user to enter r in centimeters, read the value and assign to r;
ask to enter h in cms, read the value (scan in lf format) and assign
to h.
5. Call the three programs, assigning their return values
appropriately.
6. Using the results print the out-put.

IV: Output to the screen in the format:
__________________________________________________ ________________________

Enter diameter in centimeters: 3
Enter height in centimeters: 5

A right circular cylinder with a diameter = 3.00 cm and a height = 5.00
cm
will have a volume and surface area

VOLUME SURFACE AREA
------------------------------------------------------

35.3429 cu_cm 61.2610 sq_cm
Press any key to continue




Okay, I'm new to this, but I've been working on this all day and studying up on it and here's what I have so far:

#include <stdio.h>
#define PI 3.14159

//prototypes
double calc_area(double rad, double height);
double calc_volume(double rad, double height);

int main()
{

double rad, height, area, vol;

printf("Please enter radius (in cm): ");
scanf("%lf",&rad);
printf("\nPlease enter height (in cm): ");
scanf("%lf",&height);

area = calc_area(rad, height);
vol = calc_volume(rad, height);

printf("Surface Area: %.2lf cubic centimeters\nVolume: %.2lf square centimeters\n", area,vol);
return 0;

}

double calc_area (double rad, double height)
{
return 2*PI*rad*height + PI*rad*rad;
}

double calc_volume (double rad, double height)
{
return PI*rad*rad*height;
}



Is this correct?
WalterSlopeboi is offline   Reply With Quote
Old Jan 28th, 2008, 12:51 AM   #2
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 908
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Re: Does this code look correct?

For one thing, surface area should be in square centimeters while volume should be in cubic centimeters.
titaniumdecoy is offline   Reply With Quote
Old Jan 28th, 2008, 12:57 AM   #3
WalterSlopeboi
Newbie
 
Join Date: Jan 2008
Posts: 9
Rep Power: 0 WalterSlopeboi is on a distinguished road
Re: Does this code look correct?

Quote:
Originally Posted by titaniumdecoy View Post
For one thing, surface area should be in square centimeters while volume should be in cubic centimeters.
Okay...I will change that. Any other problems?
WalterSlopeboi is offline   Reply With Quote
Old Jan 28th, 2008, 1:02 AM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,034
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Does this code look correct?

Beautiful.

But I see a couple things.

The example input shows that the diameter is entered. But the requirements say the radius should be entered. This is an inconsistency in the assignment. You should find out what is really required.

Depending on how picky your teacher is, he might not like how you used "rad", "height", "area", and "volume", when he told you to use "r", "h", "A", and "V".

Then the last thing you have left is to write the print_info() function which will properly format your answer. Although your numbers are correct, you are currently not outputting it with the exact format that the assignment demonstrates, nor are you creating a third function as specified in the requirements.

Good work.

Aside: I think the example output is incorrect. It says the surface area is 61.2610 for (r=1.5, h=5), when the Math says it should be 54.19.

Edit: Lastly, the example output displays the result with four decimal places. Currently %.2lf shows only two decimal places.
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Viewing VB's Automated code john Wesley Visual Basic .NET 3 Jun 8th, 2006 6:37 AM
FTP and return code fetching Serinth C 2 May 29th, 2006 12:05 AM
executing code in the data segment para C 1 Jan 4th, 2006 1:52 PM
String to Morse Code - Copying from string to string Xenon C 29 Nov 10th, 2005 3:30 PM
Code Reading Tips linuxpimp20 Other Programming Languages 4 Sep 8th, 2005 4:53 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:57 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC