C++ Console Plotting Sine Function

  • Context: Comp Sci 
  • Thread starter Thread starter red123
  • Start date Start date
  • Tags Tags
    C++ Plotting
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
4 replies · 6K views
red123
Messages
22
Reaction score
0

Homework Statement



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

346ust5.jpg


Homework 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.

The Attempt at a Solution


Code:
#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:
Physics news on Phys.org
Are you not allowed to use loops? Or you just don't know how to use them? If not can you use if statements?
 
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:
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.