need help with sin(x) and bar graph
Instructions:
You may use the template to organize your program.
No input from user is required.
x & y are float.
The function sin(x) is in the math.h header file and requires radians as input.
Must use a for loop.
Output MUST print to a file. However, you may also print to the screen.
Print a horizontal y axis using “-“. (Outside loop)
Print a vertical x axis using “|”. (Inside loop)
After finding the y value, print a scaled number of stars to represent y (inside loop).
Refer to bar graph code on p. 108.
Avoid mixed mode expressions.
Follow order of operations and precedence of operators.
i cant seem to figure out how to print all the stars in the sin(x) "shape" on x/y line when it prints to a file....i just need the top portion of the sin(x), help!
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
main()
{
const float pi=3.1459;
float x=pi, /*values for x in radians*/
y=.0437; /*outputted number of stars*/
int stars,
i =0.0; /*number of stars graphed*/
FILE*joser=NULL;
if((joser=fopen("c:outputfilename.txt","w")) ==NULL)
printf("Error opening data file./n");
else
{
for(i=0.0; i<=50.0;++i)
fprintf(joser,"_");
fprintf(joser,"y\n");
{
for(x=0.0 ;x<=20.0;++x)
fprintf(joser,"|\n");
}
y=sin(x);
stars=(y*50+.5);
for(y=0.0; x<=stars;++x)
fprintf(joser,"*");
}
fprintf(joser,"|\nx");
fclose(joser);
system("PAUSE");
return 0;
}