Posted: 10/9/2005 2:23:25 PM EDT
|
I would appreciate some help if anybody knows how to make C++ programs go. Below is a program that is supposed to output a graphical representation of a damped sin wave by spacing over and placing an asterisk on the screen. The idea was to shift the equation output to positive by adding one and scaling it up to fit the width of the screen by multiplying by 80. This output was then to be type cast as an integer. We were attempting to use a while loop to output spaces while the while boolean was greater than zero. Damn thing is broke, get an infinite loop, just craps out spaces. Any ideas?? edited for looks. #include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() { double start, step, y, x, Pi; int lines, space; Pi=3.14159; cout << "Enter your start value: "; cin >> start; cout << "Enter your step value: "; cin >> step; cout << "Enter the total number of lines: "; cin >> lines; step = step*(Pi/180); x = start*(Pi/180); while (lines>0) { y = (((exp(-x/Pi))*sin(x)) + 1)*80; for (space = static_cast<int>(y);space>0;space=space-1) { cout << " "; if (space==0) { cout << "*\n"; x = step + x; lines--; } } } cout << "\n"; return 0; } |
Disclaimer: I Dont really know C++ but I do know Java and C.
|
|
Here's the problem. Your test "space==0" is always false, because the inner loop exits when "space<=0". "space==0" is impossible inside the inner loop. Thus you never decrement lines. |
| Just a lazy guess (I only get paid Mon-Fri, Sunday my brain is in lazy idiot mode), but in your FOR loop, you only enter it if space is >0. But inside that FOR loop, you check if space == 0, a condition you'll never hit, as the FOR loop doesn't let you in if space==0. Since you never get back into the FOR loop to check if space==0, you never decrement lines to 0, so you never exit the while loop, thus your eternal loop. |
The pi/180 is right, except that the waveform displays with "y" on the horizontal and "x" on the vertical. There is also a logical flaw in the program that displays negative "y"s as positive (and large). |
At that point, you could do it hi-res graphical, since you would most likely be inside of an IDE. |