Search This Blog

Sunday, 5 June 2011

Sine Wave and Cosine Wave in C/C++ shown graphically


Sine Wave and Cosine Wave in C/C++ shown graphically:-


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
#include<dos.h>
#define PI 3.1416
int main()
{
int gdriver=DETECT,gmode,errorcode;
int x,x1;
double y,y1;
int a;
char ch;
clrscr();
printf("Initiating graphics mode press any key to continue");
initgraph(&gdriver,&gmode,"C:\\TC\\BGI\\");
errorcode=graphresult();
if(errorcode!=0)
{
        printf("ERROR:%s",grapherrormsg(errorcode));
getch();
exit(1);
}


printf("enter the ampiltude:");    /*only enter 1 or -1 otherwise it will go out of the screen*/
scanf("%d",&a);                         //you can also remove this value.


for(x=0,x1=0;!(kbhit());x++,x1++)
{
y=a*sin(x*PI/180);            //sine wave equation used to generate sine wave.
if(y>0)
{
putpixel(x,y*150+200,YELLOW);
}
if(y<0)
{
putpixel(x,y*150+200,YELLOW);
}


y1=a*cos(x1*PI/180);          //cosine wave equation used to generate cosine wave.
if(y1>0)
{
putpixel(x1,y1*150+200,WHITE);
}
if(y1<0)
{
putpixel(x1,y1*150+200,WHITE);
}
delay(10);
}
getch();
closegraph();
return 0;
}

Related Posts Plugin for WordPress, Blogger...