# Thread: need help with sin(x) and bar graph

1. ## 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;
}```

2. Code:
`const float pi=3.1459;`
Why not
Code:
`double pi = atan(1) * 4;`
or 3.14159265358979...

Code:
`printf("Error opening data file./n");`
No doubt you meant \n, not /n.

Code:
```        fprintf(joser,"_");
fprintf(joser,"y\n");```
Does that really warrant two calls to fprintf()? . . . and did you mean something like this?
Code:
`fprintf(joser, "_&#37;f\n", y);`
Code:
`for(x=0.0 ;x<=20.0;++x)`
Using floating point numbers to control a loop is a bad idea for accuracy reasons, especially when you're not using the loop variable in the loop body itself.

3. well, im fiddling around with the code and i think im almost there, i just need to make it so that when it writes to a .txt file, it prints out an X and Y lines along with STARS that resemeble the SIN(x) graph, just the top portion, but all i get are just plain stars all the same lengths....this is what the program is supposed to do, print to a .txt file this:
_________y
|*
|**
|***
|****
|***
|**
|*
x

except supposed to be around as long as the Y graph....how do i calculate it into this code???This program should graph the funcion y=sin(x)
on the interval from 0 to pi in incriments of pi/20.
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

main()
{
const float pi=3.1459;
float x,                   /*values for x in radians*/
y;                    /*outputted number of stars*/

int stars,
i =0.0;      /*number of stars graphed*/

FILE*fptr=NULL;

if((fptr =fopen("c:outputfilename.txt","w")) ==NULL)      /*test if files opened correctly*/
printf("\nError opening file\n\n");
else
{
for(i=1; i<=60; ++i)                                   /*for loop to print the y axis*/
fprintf(fptr,"_");
fprintf(fptr," Y\n");

for(x=0; x<=20.0; ++x)                            /*for loop to calculate the function*/
{                                                /*values and the number of stars*/
int g;
g=sin(x*pi+1);
fprintf(fptr,"|");                                /*prints the x-axis*/

for(y=g ;y<=20.0;++y)                   /*for loop to print the stars*/
fprintf(fptr,"*");
fprintf(fptr,"\n");         /*go to new line after printing a row*/
}                              /*of stars*/
fprintf(fptr,"|\n|\nX");

fclose(fptr);
}

system("pause");
return 0;
}```

4. Code:
`if((fptr =fopen("c:outputfilename.txt","w")) ==NULL)`
I never understood the rationale behind using full path names with fopen(). Just use the current directory! It's shorter and it's more convenient for the user. It' more portable: maybe you only have write access to the current directory. Maybe there isn't a c: drive, or the main drive is d: or something. Maybe the user doesn't want a file called outputfilename.txt suddenly appearing on the root of their drive. Maybe the user is running Linux, in which case fopen() will balk at that strange-looking filename, or you'll get a file called 'c:outfilefilename.txt'.

Now that that rant's out of the way . . .

Code:
```         int g;
g=sin(x*pi+1);```
Who gave you that algorithm? Because it's not right.

What you want is for the parameter to sin to vary from 0 to PI, so that you get that sine-curve you're looking for. Since x goes from 0 to 20, you need to divide x by 20, to get it to vary from 0 to 1, and then multiply that by PI, so that it varies from 0 to PI, which is what you want.

Okay. Now the return value of sin() will be from 0 to 1. Since you want a bar graph that is (presumably) 60 wide, you need to multiply the return value of sin by 60.

In other words:
Code:
`g = sin(x * pi / 20.0) * 60.0;`

5. Code:
```         stars=sin(x*pi/20)*60;

for(y=stars;y<=60;++y)                   /*for loop to print the stars*/
fprintf(fptr,"*");
fprintf(fptr,"\n");         /*go to new line after printing a row*/```
this is all that i changed, i forgot that i created "stars" so i took out g and replaced it with stars....

well, you gave me a pretty good idea on it, once i modified that graph, and ran the program, i got pretty damn close to what the txt file was supposed to look like now when i run it, i get

Code:
```____________________________________________________________ Y
|*************************************************************
|****************************************************
|*******************************************
|**********************************
|**************************
|*******************
|*************
|********
|****
|**
|**
|**
|****
|********
|*************
|*******************
|**************************
|**********************************
|*******************************************
|****************************************************
|*************************************************************
|
|
X```
the curving is quite good, EXCEPT its supposed to be in a mountain shape, not caved in...lolz, but other than that, the coding is almost there....now i have to figure out how to reverse the arrangement of the stars to get my

**
***
*****
******
*******
******
*****
***
**

any thoughts?

6. Code:
`for(y=stars;y<=60;++y)`
->
Code:
`for(y=0;y<=stars;++y)`
perhaps?

7. Originally Posted by dwks
Code:
`for(y=stars;y<=60;++y)`
->
Code:
`for(y=0;y<=stars;++y)`
perhaps?
thank you dwks, your help was quite enlightening, taking my programming "c" class, once a day per week for three hours doesnt quite make it into getting what i need to learn, even though i do come up with most of the code, you helped me finish it, thank you again...