C++ Console Plotting Sine Function

  • Context: Comp Sci 
  • Thread starter Thread starter red123
  • Start date Start date
  • Tags Tags
    C++ Plotting
Click For Summary

Discussion Overview

The discussion revolves around writing a C++ program to plot and label points of the sine function. It focuses on the technical aspects of output formatting, particularly how to determine the horizontal position of points on a console output without using loops.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses uncertainty about how to give the points horizontal positions, suggesting that it involves printing spaces followed by an asterisk based on the sine function values.
  • Another participant questions whether the restriction on using loops is due to a lack of understanding or a specific assignment requirement.
  • A different participant critiques the current approach, stating that the output appears to be hard-coded rather than dynamically calculated, implying that logic is not being utilized effectively.
  • One participant suggests that to achieve the desired output, the sine values need to be scaled and translated to fit within a specific range, advocating for the use of a loop to handle the calculations for spacing.

Areas of Agreement / Disagreement

Participants express differing views on the necessity and appropriateness of using loops in the program. There is no consensus on the best approach to implement the sine function plotting, and the discussion remains unresolved regarding the optimal method.

Contextual Notes

Participants highlight limitations in the current approach, including the lack of dynamic calculation for point placement and the need for scaling sine values to fit the output format. The discussion does not resolve these issues.

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 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K