C++ Console Plotting Sine Function

In summary: But since you haven't covered that yet, you can just manually calculate and print the correct number of spaces before each *.In summary, this program plots and labels points of the sine function from 0 to 180 degrees with 15 degree increments. It uses the number line and sin(x) values to determine the horizontal position of the asterisk, with each degree corresponding to a specific number of spaces. While a for loop would be more efficient, the program manually calculates and prints the necessary spaces for the * character.
  • #1
red123
22
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
  • #2
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
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:
  • #4
Okay, thanks.
 
  • #5
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.
 

Related to C++ Console Plotting Sine Function

1. How do I plot a sine function in C++ console?

To plot a sine function in C++ console, you will first need to include the <cmath> library. Then, use a loop to iterate through a range of x-values and calculate the corresponding y-values using the sin() function from the cmath library. Finally, use a character or symbol to represent each point on the graph and print the results to the console.

2. What is the mathematical equation for a sine function?

The mathematical equation for a sine function is y = sin(x), where x is the angle in radians and y is the corresponding y-value. The sine function is defined as the ratio of the opposite side to the hypotenuse in a right triangle, and it produces a wave-like pattern when graphed.

3. Can I customize the appearance of the sine function graph in C++ console?

Yes, you can customize the appearance of the sine function graph in C++ console. You can change the range of x-values to adjust the scale of the graph, use different symbols or characters to represent the points, or add labels and axes to make the graph more informative. You can also use different colors or formatting options to make the graph visually appealing.

4. How can I use the sine function to solve real-world problems?

The sine function can be used to solve many real-world problems, such as calculating the amplitude or frequency of a sound wave, determining the height of an object using trigonometry, or analyzing the motion of a pendulum. It is also commonly used in physics, engineering, and other fields to model and understand periodic phenomena.

5. Are there any other trigonometric functions that can be plotted in C++ console?

Yes, there are several other trigonometric functions that can be plotted in C++ console, such as cosine, tangent, secant, cosecant, and cotangent. These functions can be plotted using the same approach as the sine function, by using the corresponding functions from the cmath library and representing the points on the graph using a loop and appropriate symbols.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
770
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
872
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
2K
Replies
8
Views
956
Back
Top