So I'm new to all this and I decided to independently create this program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include "../../std_lib_facilities.h"
int main(){
//Program to calculate the volume of spheres with radius's of 1-100
constdouble pi = 3.14159;
int i;
double volume;
for(i = 1; i<101; ++i){
(4/3)*pi*(i*i*i);
cin >> volume;
cout << "Volume of a sphere with radius " << i << " is: " << volume << endl;
}
system("pause");
return 0;
}
And before you ask, the std_lib_facilities.h is the library that I'm asked to use in the book that I'm reading on C++ at the moment.
Two problems:
1) Initially, when I try to run the program, nothing happens. I have to press and enter a character for the program to start. Why is this?
2) Now, I'm not sure if this is because of the type of equation I'm trying to do or whatever but this is the result for every calculation from 1 straight through to 100: -9.2556e+061
I guess you want to output volume of all spheres with radii of 1- 100.
Now....
Your line : (4/3)*pi*(i*i*i); just performs the calculation, doesn't store it anywhere.
You said that when you run your program, nothing happens? That is because it waits for you to input the volume: cin >> volume;. I guess you wanted something like:
1 2 3 4 5
for (int i = 1; i <= 100; i++)
{
volume = (4.0/3.0) * pi * (i * i * i);
cout<<"Volume of a sphere with radius "<<i<<" is: "<<volume<<endl;
}
And btw, do you read your book thoroughly, or do you just take an occasional look at it?
Thanks for the replies. Yes, I am reading the Programming Principles & Practice.
I thought I understood it all when I was reading it and I have been reading it thoroughly without distractions. Bit of a motivational blow that I still can't get the basics even though I've read quite a lot of it.
I've changed my code to this now and it works:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include "../../std_lib_facilities.h"
int main(){
//Program to calculate the volume of spheres with radii of 1-100
constdouble pi = 3.14159;
int i;
for(i=1; i<101; ++i){
cout << "Volume of a sphere with radius " << i << " is: " << (4.0/3.0)*pi*(i*i*i) << endl;
}
system("pause");
return 0;
}
So thank you very much :D No doubt I'll be back here again in the near future.