Comp Sci  C++ Console Plotting Sine Function

  • Thread starter Thread starter red123
  • Start date Start date
  • Tags Tags
    C++ Plotting
Click For Summary
The discussion focuses on writing a C++ program to plot the sine function without using loops, which presents challenges in calculating the horizontal positions for the plotted points. The initial code provided outputs sine values at fixed intervals but lacks dynamic spacing based on the sine values. Participants suggest that to accurately position the asterisks representing the sine values, scaling and translating the output is necessary, mapping the sine range to a defined number line. A for loop is recommended for better efficiency and logic in calculating the correct spacing for each point. Overall, the conversation emphasizes the need for proper scaling and the potential benefits of using loops for this task.
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:
Okay, thanks.
 
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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
1K
  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K