954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

plotting sine curve

Hi..
I have to make a program in c++ for plotting a sine curve..using arrays ..in horizontal direction not vertical ..
can I have an alogarithm or just a hint how to go on making ...thanks :)

aminura
Light Poster
47 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

It depends how sophisticated you want to get. If it is a simple text based console program, you can make an array of strings. Each string contains let's say eighty characters, all spaces but for one asterisk. It will be your genius to place the lonely asterisk at the right position according to y = sin(x). Print out about 50 strings with a new line each, and you have the rudimentary look of a sine function.

All in all a good exercise in arrays.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Oops, if you want to go in the horizontal direction there might be more than one astrisk in the string! Fuzzy thinking on my part!

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Thanks for the reply..
I tried the following code which is similar what you have suggested..but the output I am getting is somewhat weird.. I am getting only the curve for 0 to pi and that too 4 of them..
This is the code

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	double radius=11;
	double scaley=1.2;
	const int xmax=80,ymax=24;
	int x,y;
	char pos[xmax][ymax];
	const double pi= 3.14159;
	for(int i=0;i<xmax;i++)
	{
		for(int j=0;j<ymax;j++)
		{
			pos[i][j]=' ';
		}
	}

for(double angle=0;angle<=2*pi;angle+=pi/10)
{	double z=sin(angle);
	if(angle<pi/2)
	{
		y=int(scaley*radius*z);
		x=y+15;
		
	}
	if(angle>pi/2)
	{y=int(scaley*radius*z);
		x=12-y;
	}
	
	pos[y][x]= '#';

}
	for(i=0;i<ymax;i++)
	{
		for(int j=0;j<xmax;j++)
		{
			cout<<pos[i][j];
		}
	}

return 0;
}

Can u just correct the mistakes.. :)Hi..
I have to make a program in c++ for plotting a sine curve..using arrays ..in horizontal direction not vertical ..
can I have an alogarithm or just a hint how to go on making ...thanks :)

aminura
Light Poster
47 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

Not too pretty but you are getting there!
Print out your x and y values.
I don't think y should go negative.

For troubleshooting purposes

add: cout << "y=" << y << " x=" << x << endl;
above: pos[y][x]= '#';

comment out: // cout << pos[i][j];

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Well finally I got the sine curve(though in a vey complicated manner ;) )..I am getting not 1 but 4 curves..and I'm not able to point out the error..here is the program

#include
#include
using namespace std;
int main()
{
double radius=10;
double scaley=1;
const int xmax=80,ymax=24;
int x,y;
char pos[xmax][ymax];
const double pi= 3.14159;
double angle,z;
for(int i=0;i

aminura
Light Poster
47 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

The code shouldn't be that complicated. My suggestion would be to make a program that runs the sine curve down the vertical axis and get that one to work. You have to work with the idea of an axis down the center of the screen, positive y to the right, negative y to the left.

Once that all works, then transpose it it to the horizontal axis.

Another approach would be to locate the cursor at point x,y and print the character # or * or whatever. I have attached some code for that written in Dev-C++. It uses the Windows API call SetConsoleCursorPosition().
[php]// plot a text based sine curve down the vertical center axis
// written in DevCpp v.4.9.9.0

#include
#include // covers windows API calls

using namespace std;

void LocateCursor(int col,int row);

HANDLE hConsole;

int main(int argc, char *argv[])
{
hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
double x, y; // radians
int col, row; // screen loacations

for (x = -3.2; x < 6.4; x += 0.1) {
y = sin(x);
row = ((int) (x * 10)) + 32; // cast into positive screen location
col = ((int) (y * 30)) + 35; // center this one
LocateCursor(col, row);
}

cin.get(); // wait
return 0;
}

void LocateCursor(int col,int row)
{
COORD cursor;

cursor.X = col;
cursor.Y = row;
SetConsoleCursorPosition(hConsole,cursor);
cout << '*';
}

[/php]

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

I've trying to plot a simple gragh of a quadratic but I'm not getting expected result can you please write for a simple program for plotting a graph using c++ koolplot and send it to my email

Alexoo
Newbie Poster
1 post since Mar 2007
Reputation Points: 9
Solved Threads: 0
 

Looks like someone hijacked a thread and forgot to read the Rules

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You