Click here to Skip to main content
15,870,165 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hi I spent a couple of hours trying to figure this out. I tested it without a an array and loop and the write answer was printed for a single value but when its placed in an array it just seems to screw it all up?
I have just started learning C so any tips or help is very much appreciated!
Thanks

C#
#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
    float array[18][3];
    float Sin_Value[18], Cos_Value, Tan_Value;
    int Degrees = 0;
    float radians[18];
    int i,j;
    float PI=3.14159;

    for(j=0, Degrees=0;j<=17 && Degrees <= 180; ++j, Degrees= Degrees+10)
    {
            radians[j]= Degrees*PI/180;
            Sin_Value[j]=sin(radians[j]);
            Sin_Value[j]= array[j][1];
            printf("%.5f\n %.5f\n %.5f\n", radians[j], Sin_Value[j], array[j][0]);
    }
    getch();
}
Posted

You have not said what you are wishing to achieve so answering is difficult.

1. Simplify the loop.
2. Where do you want sin value to be in the array and what do you want to print out? You are printing array[j][0] which never gets a value. In fact nothing is put into the array at all.

Try something like this and work on it:

C++
for(j=0; j < 18 ; j++)
{
        Degrees = j * 10;

        radians[j]= Degrees*PI/180;
        Sin_Value[j]=sin(radians[j]);
        array[j][1] = Sin_Value[j];
        printf("%.5f\n %.5f\n %.5f\n", radians[j], Sin_Value[j], array[j][1]);
}
getch();
 
Share this answer
 
Comments
LBIreland 26-Feb-15 19:19pm    
I ended up with this, the question was to produce a table of sin cos tan

<pre lang="cs">#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
float Table[18][3], Sin_Value[18][1], Cos_Value[18][1], Tan_Value[18][1], radians[18];
int Degrees = 0, i,j,x;
float PI=3.14159;

for(j=0, Degrees=0;j<=17 && Degrees <= 180; ++j, Degrees= Degrees+10)
{
radians[j]= Degrees*PI/180;
Sin_Value[j][0]=sin(radians[j]);
Cos_Value[j][0]=cos(radians[j]);
Tan_Value[j][0]=tan(radians[j]);
}

for(i=0;i<17;++i)
{
Table[i][0]=Sin_Value[i][0];
Table[i][1]=Cos_Value[i][0];
Table[i][2]=Tan_Value[i][0];
}

printf("Sin Cos Tan\n");

for(x=0; x<17; ++x)
{
printf("%.5f %.5f %.5f\n", Table[x][0],Table[x][1],Table[x][2]);
}
getch();
}</pre>
[no name] 26-Feb-15 19:22pm    
You would be well served to examine my answer.
LBIreland 26-Feb-15 19:28pm    
that loop is a lot easier than including two conditions! Thanks
[no name] 26-Feb-15 19:36pm    
Exactly. You are welcome.
array[j][1]

You never initialize array.

And you set Sin_Value[j] twice.

I don't see any need for arrays, but if you want to store stuff you could declare a struct that contains degrees, radians, sin, cos, tan, etc. and make an array of those. It might be a good exercise.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900