C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 22nd, 2005, 10:21 PM
earache earache is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 5 earache User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 32 m 50 sec
Reputation Power: 0
Help with school assignment?

Hi, I can't figure out what I'm doing wrong - the program builds fine but the math it returns is wrong; the output is always:

8.40779e-45

I'm compiling with XCode 1.5. I've tried zapping gremlins with TextWrangler. Here is the code:

/*
Problem:

Write a program that asks the user how many cylinders they would like to find the volume of. Then it repeats the correct number of times (using a for loop):
Ask for the radius and height of the cylinder
Send these values to a function that returns the volume
Output the volume
Add the volume to a running total
At the end the program outputs the total volume of all the cylinders and the average volume. (Make sure your program still works if the user says at the beginning that they want the volume of zero cylinders.)

*/
Code:
 
#include <iostream>
#include <cmath>
using namespace std;

float radius;//Radius of each cylinder
float height;//Height of each cylinder
float computeVolume (float, float);//Function prototype

int main () //Main function:

{
    int numCylinders;//Problem Inputs: Number of cylinders
	int counter;//Problem variables: Counter
	
	
	float volume;//Volume of each cylinder
	float totalVolume;
	totalVolume = 0;
	float average;
	
	//Get the number of cylinders
	cout << "How many cylinders would you like to calculate the volume of? ";
	cin >> numCylinders;
	for (counter = 0; counter < numCylinders; counter++)
	{
		//Get the radius and height of each cylinder
		cout << "Enter the radius of the cylinder: ";
		cin >> radius;
		cout << "Enter the height of the cylinder: ";
		cin >> height;
		computeVolume (radius, height);//Call function volume
		cout << "The volume of a cylinder with radius " << radius << " and height " << height << " is " << volume << endl;//Display volume
		totalVolume += volume;//Add volume to total volume
	}
	if (numCylinders != 0)
	{
		cout << "The total volume of all cylinders is " << totalVolume << endl;//Output the total volume
		average = totalVolume / numCylinders;//Compute the average volume
		cout << "The average volume of all cylinders is " << average;//Display the average volume
	}
	else
		cout << "Invalid number.  Please try again." << endl;//Display warning

	return 0;
}
float computeVolume (float thisRadius, float thisHeight)//Volume function

{
	float thisVolume;//local variable
	
	float const PI = 3.14159;//Problem constant
	thisVolume = ((PI * (pow (thisRadius, 2))) * thisHeight);//Calculate volume of each cylinder
	return thisVolume;
}


TIA!!!

Last edited by B-Con : July 23rd, 2005 at 03:53 AM. Reason: don't forget your [code] tags ;)

Reply With Quote
  #2  
Old July 23rd, 2005, 03:58 AM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 8 m 6 sec
Reputation Power: 4
Finally, a properly phrased homework question. Thank you for doing your own work and asking us for help only when you got stuck

look at the line where you call "computeVolume()":
Code:
computeVolume (radius, height);//Call function volume

You compute the volume, but you throw the returned value away because you don't assign it to anything. You should have:
Code:
volume = computeVolume (radius, height);//Call function volume


hth
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips.



Reply With Quote
  #3  
Old July 23rd, 2005, 01:09 PM
jayv jayv is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 4 jayv User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 37 m 51 sec
Reputation Power: 0
Quote:
Originally Posted by earache
Hi, I can't figure out what I'm doing wrong - the program builds fine but the math it returns is wrong; the output is always:

8.40779e-45

I'm compiling with XCode 1.5. I've tried zapping gremlins with TextWrangler. Here is the code:

/*
Problem:

Write a program that asks the user how many cylinders they would like to find the volume of. Then it repeats the correct number of times (using a for loop):
Ask for the radius and height of the cylinder
Send these values to a function that returns the volume
Output the volume
Add the volume to a running total
At the end the program outputs the total volume of all the cylinders and the average volume. (Make sure your program still works if the user says at the beginning that they want the volume of zero cylinders.)

*/
Code:
 
#include <iostream>
#include <cmath>
using namespace std;

float radius;//Radius of each cylinder
float height;//Height of each cylinder
float computeVolume (float, float);//Function prototype

int main () //Main function:

{
    int numCylinders;//Problem Inputs: Number of cylinders
	int counter;//Problem variables: Counter
	
	
	float volume;//Volume of each cylinder
	float totalVolume;
	totalVolume = 0;
	float average;
	
	//Get the number of cylinders
	cout << "How many cylinders would you like to calculate the volume of? ";
	cin >> numCylinders;
	for (counter = 0; counter < numCylinders; counter++)
	{
		//Get the radius and height of each cylinder
		cout << "Enter the radius of the cylinder: ";
		cin >> radius;
		cout << "Enter the height of the cylinder: ";
		cin >> height;
		computeVolume (radius, height);//Call function volume
		cout << "The volume of a cylinder with radius " << radius << " and height " << height << " is " << volume << endl;//Display volume
		totalVolume += volume;//Add volume to total volume
	}
	if (numCylinders != 0)
	{
		cout << "The total volume of all cylinders is " << totalVolume << endl;//Output the total volume
		average = totalVolume / numCylinders;//Compute the average volume
		cout << "The average volume of all cylinders is " << average;//Display the average volume
	}
	else
		cout << "Invalid number.  Please try again." << endl;//Display warning

	return 0;
}
float computeVolume (float thisRadius, float thisHeight)//Volume function

{
	float thisVolume;//local variable
	
	float const PI = 3.14159;//Problem constant
	thisVolume = ((PI * (pow (thisRadius, 2))) * thisHeight);//Calculate volume of each cylinder
	return thisVolume;
}


TIA!!!


//================================================== =========================================

I agree with B-Con. volume must be set to:
volume = computeVolume (radius, height);

//================================================== =========================================

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Help with school assignment?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

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





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT