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 :)

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.

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!

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 :)

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[j];

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<iostream>
#include<cmath>
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<xmax;i++)
    {
        for(int j=0;j<ymax;j++)
        {
            pos[i][j]=' ';
        }
    }

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

for(angle=pi/2;angle<pi;angle+=pi/10)
{     z=sin(angle);
      y=int(scaley*radius*z);
      x=10-y;
      x=20-x;
      pos[y][x]= '#';
}
for(angle=pi;angle<(3*pi)/2;angle+=pi/10)
{     z=sin(angle);
      y=int(scaley*radius*z);
      x=10-y;
      x=35+x;
      y=(-1)*y;
      y=10+y;
      pos[y][x]= '#';
}
    for(angle=(3*pi)/2;angle<=2*pi;angle+=pi/10)
{     z=sin(angle);
      y=int(scaley*radius*z);
      x=10-y;
      x=55-x;
      y=(-1)*y;
      y=10+y;
      pos[y][x]= '#';
}


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

return 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().

// plot a text based sine curve down the vertical center axis
// written in DevCpp v.4.9.9.0

#include <iostream>
#include <windows.h>    // 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 << '*';
}

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 <email snipped>

commented: Go away you sponge - this is a forum, not a stopover for you to boardcast your homework and then sit back and hope an anwer gets into your inbox -1

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.