# C++ console plotting

1. Feb 18, 2010

### red123

1. The problem statement, all variables and given/known data

Write a program that plots and labels the points of the sine function.

2. Relevant equations

I can't figure out how to give the points horizontal position. I think it's position is determined by printing a series of 'space' characters followed by a '*' to mark the point, where the number of 'space' is determined by the function. Then I'd want to scale this spacing against a number line. Not using a loop function, haven't yet gotten to that in class.

3. The attempt at a solution
Code (Text):

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main ()
{
cout << "\n";
cout << "A plot of sine function from 0 to 180 degrees with 15 degrees increment:\n";
cout << "\n";
char space [] = "                    ";
cout << space << "-1       0         1" << endl;
cout << space << "++++++++++++++++++++" << endl;                    //number line
const double PI = 3.14159;
int degree = 0;
double x = degree * ( PI / 180 );
cout << fixed << setprecision(2);
cout << space << "*   sin(" << degree << ") = " << sin(x) << "\n";
degree = degree + 15, x = degree * ( PI / 180 );
cout << space << "*   sin(" << degree << ") = " << sin(x) << "\n";
degree = degree + 15, x = degree * ( PI / 180 );
cout << space << "*   sin(" << degree << ") = " << sin(x) << "\n";
degree = degree + 15, x = degree * ( PI / 180 );
cout << space << "*   sin(" << degree << ") = " << sin(x) << "\n";
degree = degree + 15, x = degree * ( PI / 180 );
cout << space << "*   sin(" << degree << ") = " << sin(x) << "\n";
degree = degree + 15, x = degree * ( PI / 180 );
cout << space << "*   sin(" << degree << ") = " << sin(x) << "\n";
degree = degree + 15, x = degree * ( PI / 180 );
cout << space << "*   sin(" << degree << ") = " << sin(x) << "\n";
degree = degree + 15, x = degree * ( PI / 180 );
cout << space << "*   sin(" << degree << ") = " << sin(x) << "\n";
degree = degree + 15, x = degree * ( PI / 180 );
cout << space << "*   sin(" << degree << ") = " << sin(x) << "\n";
degree = degree + 15, x = degree * ( PI / 180 );
cout << space << "*   sin(" << degree << ") = " << sin(x) << "\n";
degree = degree + 15, x = degree * ( PI / 180 );
cout << space << "*   sin(" << degree << ") = " << sin(x) << "\n";
degree = degree + 15, x = degree * ( PI / 180 );
cout << space << "*   sin(" << degree << ") = " << sin(x) << "\n";
degree = degree + 15, x = degree * ( PI / 180 );
cout << space << "*   sin(" << degree << ") = " << sin(x) << "\n";
degree = degree + 15, x = degree * ( PI / 180 );
cout << space << "*   sin(" << degree << ") = " << sin(x) << "\n";
degree = degree + 15, x = degree * ( PI / 180 );
cout << space << "*   sin(" << degree << ") = " << sin(x) << "\n";
degree = degree + 15, x = degree * ( PI / 180 );
cout << space << "*   sin(" << degree << ") = " << sin(x) << "\n";
degree = degree + 15, x = degree * ( PI / 180 );
cout << space << "*   sin(" << degree << ") = " << sin(x) << "\n";
degree = degree + 15, x = degree * ( PI / 180 );
cout << space << "*   sin(" << degree << ") = " << sin(x) << "\n";
degree = degree + 15, x = degree * ( PI / 180 );
cout << space << "*   sin(" << degree << ") = " << sin(x) << "\n";
degree = degree + 15, x = degree * ( PI / 180 );
cout << space << "*   sin(" << degree << ") = " << sin(x) << "\n";
degree = degree + 15, x = degree * ( PI / 180 );
cout << space << "*   sin(" << degree << ") = " << sin(x) << "\n";
degree = degree + 15, x = degree * ( PI / 180 );
cout << space << "*   sin(" << degree << ") = " << sin(x) << "\n";
degree = degree + 15, x = degree * ( PI / 180 );
cout << space << "*   sin(" << degree << ") = " << sin(x) << "\n";
degree = degree + 15, x = degree * ( PI / 180 );
cout << space << "*   sin(" << degree << ") = " << sin(x) << "\n";
degree = degree + 15, x = degree * ( PI / 180 );
cout << space << "*   sin(" << degree << ") = " << sin(x) << "\n";

cout << endl;
return 0;
}

Last edited: Feb 18, 2010
2. Feb 18, 2010

### nhartung

Are you not allowed to use loops? Or you just don't know how to use them? If not can you use if statements?

3. Feb 18, 2010

### nhartung

Well by looking at the assignment and the stage of the class that you are in right now I don't think there is any logic involved in this program. It looks to me like straight output. That being said I believe that the point at which the *'s are printed is hard coded and not calculated in any way by the program. Unless the specifications say otherwise I think this is the route to go.

Last edited: Feb 19, 2010
4. Feb 18, 2010

### red123

Okay, thanks.

5. Feb 19, 2010

### Staff: Mentor

To get the output you showed you will need to scale the sin(x) values so that the asterisk is placed in the right location. You need to scale and translate things so that the interval [-1, 1] is mapped to [0, 20], the length of the line segment with the + characters. For example, sin(0) = 0, so you want to print 10 spaces before printing the *. sin(30) = .5, so you want to print 15 spaces before the *. sin(210) = -.5, so print 5 spaces and then the *, and so on.

A for loop would really be the way to go, with logic inside to calculate the spacing and placement of the * characters.