d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need A Lot Of Help C++ > Easy For The Experienced
Add Reply New Topic New Poll
Member
Posts: 13,237
Joined: Mar 4 2007
Gold: 655.00
Oct 19 2012 02:04pm

Develop a program that will output the volume V of a sphere for a given radius R. The program user inputs the initial, incremental, and ending values of the radius. For each value of the radius, the program will calculate the corresponding volume V = 4πR3/3 and will print the radius and the volume. Use SI units.
(Hint: if the loop starts at 10.2 mm and the incremental value is 2.5 mm the program should display a series of lines like this:
The volume of a sphere with a radius of 10.2 mm is … mm3
The volume of a sphere with a radius of 12.7 mm is … mm3
...)

My current program looks like this:
Code
#include <iostream>
#include <cmath>
using namespace std;

int main(){

//Input radius, volume, initialValue, increment, ending Value,
double x, volume, initialRadius, increment, endingRadius, radius;
#define PI 3.14159

//Processing


//Output Volume of Sphere, Radius,
cout<<" Please input the initial radius of the sphere: "<<endl;
cin>>initialRadius;
cout<<"Please input your increment value: "<<endl;
cin>>increment;
cout<<"Please input the final radius of the sphere."<<endl;
cin>>endingRadius;

for (x=initialRadius;x<=endingRadius; x=increment)
volume=4*PI*pow(radius,3)/3.0;
cout<<"The Volume of a sphere with a radius of "<<radius<<" mm is "<<volume<<" mm3."<<endl;



cin.get();
return 0;
}


How can I make it so that my radius variable changes to the new radius everytime?
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 19 2012 02:46pm
well my first question is:

when does radius ever take on a value in this program?


As for the radius problem, maybe something like this?


Code
int i = 0; //declare an integer to use
...
//INSIDE THE FOR LOOP:

cout<<"The Volume of a sphere with a radius of "<<radius+i*increment<<" mm is "<<volume<<" mm3."<<endl; //so it prints out radius + 0*increment, then i becomes 1, then it prints out radius + 1*increment.....etc
i++;


This post was edited by Eep on Oct 19 2012 02:52pm
Member
Posts: 6,192
Joined: Dec 13 2010
Gold: 6,669.99
Oct 19 2012 03:00pm
Code
for (x=initialRadius;x<=endingRadius; x = x + increment)
{ radius = x;
 volume=4/3.0*PI*pow(radius,3);
 cout<<"The Volume of a sphere with a radius of "<<radius<<" mm is "<<volume<<" mm3."<<endl;
}


It will start at x = initialradius then increment it by "increment" until it reaches endingradius
x will be given to radius in the loop every time then it just calculates and displays.

In your code, you forgot to give a number/value to radius so it crashed every time when it tries to calculate it.
And for the increment, if you place x = increment, it will simply give the value of increment to x every time and make an infinite loop which you don't want.
You can also place x += increment which does the same thing

This post was edited by ShadowFiend on Oct 19 2012 03:02pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 19 2012 03:02pm
oh I didn't notice he messed up the for loop.


it has to be x = x+increment
Member
Posts: 12,565
Joined: Dec 29 2007
Gold: Locked
Oct 29 2012 09:11pm
wrong thread
my bad

This post was edited by clanwkya on Oct 29 2012 09:12pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 29 2012 10:33pm
Quote (ShadowFiend @ Oct 19 2012 05:00pm)
Code
for (x=initialRadius;x<=endingRadius; x = x + increment)
{ radius = x;
 volume=4/3.0*PI*pow(radius,3);
 cout<<"The Volume of a sphere with a radius of "<<radius<<" mm is "<<volume<<" mm3."<<endl;
}


this.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll