### Random number for value in an equation

Hello,

My code will compile, but will not populate the table, or it runs an infinite loop. This version shows the empty table. The infinite loop happens when i move the fallDistance function outside of the for loop. I thought it needed a while loop since the value of seconds and meters weren't determined.

My textbook is only pseudocode, the C++ Language Companion is very sparse. I have 3 C++ books I have been consulting for, oh gee, let's say the last 6 hours.

Please help me find where it is out of sequence or whatever I've done wrong. This is the first problem using srand(), time(0, and the #include <cstdlib> and #include <ctime>

Thank you for any advice you could send my way.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667`` ``````#include #include #include using namespace std; // function prototypes double fallDistance(int &seconds, double gravity, double &meters); // to solve time, gravity, distance equation // global constants const int RANDOM_MAX = 10; // maximum number in seconds range const int RANDOM_MIN = 1; // minimum number in seconds range const double GRAVITY = 9.8; // force of gravity int main() { // begin main function //variables int seconds = 0; int range = 0; double meters = 0; double gravity = 0; // title and explanation cout << "Falling Distance of a Star" << endl; //title cout << endl << endl; cout << "This chart shows how far a star falls per second." << endl; cout << "Seconds\t\tMeters" << endl; unsigned seed = time(0); // get the system time to determine seed srand(seed); // seed for rand() for(seconds = 1; seconds <= 10; seconds++) { range = 1 + rand() % 100; meters = fallDistance (seconds, gravity, meters); cout << seconds << "\t" << meters << endl; seconds++; break; } cin.get(); //pause program for debugging cin.get(); return 0; }//end main f. double fallDistance(int &seconds, double gravity, double &meters) { seconds = 0; //gravity = 0; meters = 0; meters = .5 * GRAVITY * (seconds * seconds); return meters; } ``````
Line 38: Do you ever use range? >_>

Line 44: I'm guessing this is a leftover from the while loop. seconds will iterate twice as fast because of that line, and I don't think you wanted that.

Line 58: This is the cause of your infinite loop. You're resetting seconds to 0, but seconds being greater than 10 is the condition for your loop to end.

What is this program supposed to do?

-Albatross
We got it! Excellent!

Line 38: Using range seems a lot more straightforward. I have only used it in Javascript. It's good to know you can use it in C++. I think we are supposed to follow the chapter example though. -- left all of that in there

Line 44: I copied that from the Celsius to Fahrenheit program I had written for another assignment because I thought that may have been a missing piece. -- took that out

Line 58: I set the seconds to 10, that only displayed 10. I renamed the variables of the function and that made it display correctly but only 1 second. Then I uninitialized those variables and it worked!

The program is to calculate how far (in meters) a star falls per second ( ranging from (1-10 seconds). I should start writing that at the top.

Thank you! Thank you! Thank you!
Topic archived. No new replies allowed.