Warning

 

Close
Confirm Action

Are you sure you wish to do this?

Cancel Confirm
AR15.COM
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;
            }
10/9/2005 2:25:18 PM EDT
[#1]
throw some tabs in there so i can read it.
10/9/2005 2:34:57 PM EDT
[#2]
For the love of god why wont it let me tab it over??
10/9/2005 2:49:36 PM EDT
[#3]
Disclaimer: I Dont really know C++ but I do know Java and C.



#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;\\Defines Start
   cout << "Enter your step value: ";
   cin >> step;\\Defines step
   cout << "Enter the total number of lines: ";
   cin >> lines;\\Defines 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;
}
10/9/2005 2:50:18 PM EDT
[#4]

Quoted:

#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;
}

10/9/2005 2:55:42 PM EDT
[#5]
I think the prob might be that you are dividing PI by 180.

Try using 2.
10/9/2005 2:56:20 PM EDT
[#6]
You are decrementing lines in a loop that can exit prior to having lines equal zero.  Basically what I'm trying to say is that the for loop does not decrement lines enough when it exists, and the while loop then keeps running.
10/9/2005 2:56:23 PM EDT
[#7]

Quoted:

 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--;
     }
   }



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.
10/9/2005 2:56:56 PM EDT
[#8]
eta: n/m
10/9/2005 2:57:08 PM EDT
[#9]

Quoted:
You are decrementing lines in a loop that can exit prior to having lines equal zero.  Basically what I'm trying to say is that the for loop does not decrement lines enough when it exists, and the while loop then keeps running.



Damn, 3 seconds too slow.  
10/9/2005 2:58:20 PM EDT
[#10]
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.
10/9/2005 3:00:11 PM EDT
[#11]
the infinate loop comes from only decrimenting lines when space is equal to zero and that only happens once when Y=0

And I dont think PI/180 is the right move


EDIt damn too slow
10/9/2005 3:03:15 PM EDT
[#12]
Here is my obligatory OO design comment:

Maybe decoupling the calculation step from the
display step would make everything easier to read
and understand.

Nine times out of ten, linear code is bad.
10/9/2005 3:03:45 PM EDT
[#13]

Quoted:

EDIt damn too slow



I didn't think there were so many nerd gun owners here.   Armed dorks unite!

10/9/2005 3:15:35 PM EDT
[#14]

Quoted:
the infinate loop comes from only decrimenting lines when space is equal to zero and that only happens once when Y=0

And I dont think PI/180 is the right move


EDIt damn too slow



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).
10/9/2005 4:44:22 PM EDT
[#15]

Quoted:
Here is my obligatory OO design comment:

Maybe decoupling the calculation step from the
display step would make everything easier to read
and understand.

Nine times out of ten, linear code is bad.



At that point, you could do it hi-res graphical, since you would most likely be inside of an IDE.
10/16/2005 3:38:39 PM EDT
[#16]
We got it to work, many thanks for all of the help! We ended up reducing the scaling factor from 100 to 30 in addition to the changes you guys noted.

Again, Thank you.