| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 ;) |
|
#2
|
||||
|
||||
|
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. ![]() |
|
#3
|
|||
|
|||
|
Quote:
//================================================== ========================================= I agree with B-Con. volume must be set to: volume = computeVolume (radius, height); //================================================== ========================================= |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Help with school assignment? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|