Function call a for loop - c

I'm trying to function call the for loop and print AngleValue and SinValue, but whatever I do I can't get it to work. All I need is to print those 2 from the loop into the main function.
int trigof(int x, double y, double AngleValue, double SinValue);
int main(void)
{
printf("%35s","***************************\n");
printf("%35s","****TABLE OF SIN VALUES****\n");
printf("%35s","***************************\n");
printf("%17s", "ANGLE(DEG)");
printf("%18s","SIN(ANGLE)\n");
printf("Press any key to exit");
getchar();
return 0;
}//endprogram
int trigof(int x, double y, double AngleValue, double SinValue)
{
for (x = 0; x < 18; x++)
{
y = AngleValue * PI / 180;
SinValue = sin(y);
printf("%13.1f", AngleValue);
printf("%18.4f\n", SinValue);
AngleValue = AngleValue + 20.0;
}
return AngleValue;
return SinValue;
}

First of all there are lots of mistakes.
You have declared function but you haven't called it in your main. To call function inside main use this statement inside main function trigof(0,0,45,0); 45 is the angle value. Secondly you don't need x, y, signvalue as function parameters. You can declare them locally inside your trigof function. Lastly there should be only one return statement returning only one object.

#define PI (4*atan(1))
static void trigOf(void);
int main(void) {
printf("%35s","***************************\n");
printf("%35s","****TABLE OF SIN VALUES****\n");
printf("%35s","***************************\n");
printf("%17s", "ANGLE(DEG)");
printf("%18s","SIN(ANGLE)\n");
trigOf();
printf("Press any key to exit");
getchar();
return 0;
}//endprogram
static void trigof() {
int x;
double y=0.0,AngleValue=0.0,SinValue=0.0;
for (x = 0; x < 18; x++) {
y = AngleValue * PI / 180;
SinValue = sin(y);
printf("%13.1f", AngleValue);
printf("%18.4f\n", SinValue);
AngleValue = AngleValue + 20.0;
}
}

For starters...
You have to define global variables.
#define PI 3.14159265358979323846
Your function prototype is incorrect, it requires no parameters in this scenario.
//this is wrong
int trigof(int x, double y, double AngleValue, double SinValue);
//this is right
int trigof();
You never call the function trigof(); in main therefore you get no results.
printf("%35s","***************************\n");
printf("%35s","****TABLE OF SIN VALUES****\n");
printf("%35s","***************************\n");
printf("%17s", "ANGLE(DEG)");
printf("%18s","SIN(ANGLE)\n");
//you should have function call here
trigof();
You simply need to define your local variables in the function.
example:
int trigof()
{
//these are local variables
int x;
double y, AngleValue, SinValue;
for (x = 0; x < 18; x++)
{
y = AngleValue * PI / 180;
SinValue = sin(y);
printf("%13.1f", AngleValue);
printf("%18.4f\n", SinValue);
AngleValue = AngleValue + 20.0;
}
return AngleValue;
return SinValue;
}
If you want your output to resemble this:
***************************
****TABLE OF SIN VALUES****
***************************
ANGLE(DEG) SIN(ANGLE)
0.0 0.0000
20.0 0.3419
40.0 0.6425
60.0 0.8658
80.0 0.9847
100.0 0.9850
120.0 0.8666
140.0 0.6437
160.0 0.3434
180.0 0.0016
200.0 -0.3404
220.0 -0.6413
240.0 -0.8650
260.0 -0.9844
280.0 -0.9852
300.0 -0.8673
320.0 -0.6450
340.0 -0.3448

Related

Taylor Series in C (problem with sin(240) and sin(300))

#include <stdio.h>
#include <math.h>
const int TERMS = 7;
const float PI = 3.14159265358979;
int fact(int n) {
return n<= 0 ? 1 : n * fact(n-1);
}
double sine(int x) {
double rad = x * (PI / 180);
double sin = 0;
int n;
for(n = 0; n < TERMS; n++) { // That's Taylor series!!
sin += pow(-1, n) * pow(rad, (2 * n) + 1)/ fact((2 * n) + 1);
}
return sin;
}
double cosine(int x) {
double rad = x * (PI / 180);
double cos = 0;
int n;
for(n = 0; n < TERMS; n++) { // That's also Taylor series!
cos += pow(-1, n) * pow(rad, 2 * n) / fact(2 * n);
}
return cos;
}
int main(void){
int y;
scanf("%d",&y);
printf("sine(%d)= %lf\n",y, sine(y));
printf("cosine(%d)= %lf\n",y, cosine(y));
return 0;
}
The code above was implemented to compute sine and cosine using Taylor series.
I tried testing the code and it works fine for sine(120).
I am getting wrong answers for sine(240) and sine(300).
Can anyone help me find out why those errors occur?
You should calculate the functions in the first quadrant only [0, pi/2). Exploit the properties of the functions to get the values for other angles. For instance, for values of x between [pi/2, pi), sin(x) can be calculated by sin(pi - x).
The sine of 120 degrees, which is 40 past 90 degrees, is the same as 50 degrees: 40 degrees before 90. Sine starts at 0, then rises toward 1 at 90 degrees, and then falls again in a mirror image to zero at 180.
The negative sine values from pi to 2pi are just -sin(x - pi). I'd handle everything by this recursive definition:
sin(x):
cases x of:
[0, pi/2) -> calculate (Taylor or whatever)
[pi/2, pi) -> sin(pi - x)
[pi/2, 2pi) -> -sin(x - pi)
< 0 -> sin(-x)
>= 2pi -> sin(fmod(x, 2pi)) // floating-point remainder
A similar approach for cos, using identity cases appropriate for it.
The key point is:
TERMS is too small to have proper precision. And if you increase TERMS, you have to change fact implementation as it will likely overflow when working with int.
I would use a sign to toggle the -1 power instead of pow(-1,n) overkill.
Then use double for the value of PI to avoid losing too many decimals
Then for high values, you should increase the number of terms (this is the main issue). using long long for your factorial method or you get overflow. I set 10 and get proper results:
#include <stdio.h>
#include <math.h>
const int TERMS = 10;
const double PI = 3.14159265358979;
long long fact(int n) {
return n<= 0 ? 1 : n * fact(n-1);
}
double powd(double x,int n) {
return n<= 0 ? 1 : x * powd(x,n-1);
}
double sine(int x) {
double rad = x * (PI / 180);
double sin = 0;
int n;
int sign = 1;
for(n = 0; n < TERMS; n++) { // That's Taylor series!!
sin += sign * powd(rad, (2 * n) + 1)/ fact((2 * n) + 1);
sign = -sign;
}
return sin;
}
double cosine(int x) {
double rad = x * (PI / 180);
double cos = 0;
int n;
int sign = 1;
for(n = 0; n < TERMS; n++) { // That's also Taylor series!
cos += sign * powd(rad, 2 * n) / fact(2 * n);
sign = -sign;
}
return cos;
}
int main(void){
int y;
scanf("%d",&y);
printf("sine(%d)= %lf\n",y, sine(y));
printf("cosine(%d)= %lf\n",y, cosine(y));
return 0;
}
result:
240
sine(240)= -0.866026
cosine(240)= -0.500001
Notes:
my recusive implementation of pow using successive multiplications is probably not needed, since we're dealing with floating point. It introduces accumulation error if n is big.
fact could be using floating point to allow bigger numbers and better precision. Actually I suggested long long but it would be better not to assume that the size will be enough. Better use standard type like int64_t for that.
fact and pow results could be pre-computed/hardcoded as well. This would save computation time.
const double TERMS = 14;
const double PI = 3.14159265358979;
double fact(double n) {return n <= 0.0 ? 1 : n * fact(n - 1);}
double sine(double x)
{
double rad = x * (PI / 180);
rad = fmod(rad, 2 * PI);
double sin = 0;
for (double n = 0; n < TERMS; n++)
sin += pow(-1, n) * pow(rad, (2 * n) + 1) / fact((2 * n) + 1);
return sin;
}
double cosine(double x)
{
double rad = x * (PI / 180);
rad = fmod(rad,2*PI);
double cos = 0;
for (double n = 0; n < TERMS; n++)
cos += pow(-1, n) * pow(rad, 2 * n) / fact(2 * n);
return cos;
}
int main()
{
printf("sine(240)= %lf\n", sine(240));
printf("cosine(300)= %lf\n",cosine(300));
}

Why am I getting an output of 0

Why am I getting an output of 0? I think there's something wrong about my angle conversion and possibly my equation, yet fiddling around with it and moving some stuff always gives me the same result.
My goal is to write a C code that will compute the angle θ for any two given vectors u and v.
#include <stdio.h>
#include <math.h>
int main()
{
double ux, uy;
double vx, vy;
double inner_product(double vx, double vy, double ux, double uy);
double v;
double u;
double i;
double x;
double k;
double pi;
double angle;
double p;
ux = 1.0;
uy = 1.0;
vx = 1.0;
vy = 1.0;
printf ("input value for ux\n", ux);
scanf_s ("%f", &ux);
printf ("input value for uy\n", uy);
scanf_s ("%f", &uy);
printf ("input value for vx\n", vx);
scanf_s ("%f", &vx);
printf("input value for vy\n", vy);
scanf_s ("%f", &vy);
u = ux * vx;
v = uy * vy;
i = u * v;
x = u * u;
k = v * v;
pi = acos(-1.0);
p = acos(i / (sqrt(x * k)));
angle = ((p * 180) / pi); //converting from radians to degrees
printf("%f", angle);
return;
}
The math error is in the following:
p = acos(i / (sqrt(x * k)));
Change it to:
p = acos((ux*vx + uy*vy) / (sqrt(ux*ux + uy*uy) * sqrt(vx*vx + vy*vy)));
That's just the dot product divided by the two lengths.
You equation is wrong.
The correct is:
|U|=√[Ux^2+Uy^2]
|V|=√[Vx^2+Vy^2]
U*V=(Ux,Uy)(Vx,Vy)=Ux*Uy+Vx*Vy
cos=U*V/[|U|*|V|]
Not %f but %lf specifier have to be used to read data having type double via scanf() family.
Note that %f should be used to print data having type double via printf() family because float will be automatically converted to double for variable number arguments.

Program to find root of a third degree polynomial in c (Sidi's Method)

I am trying to write a program to find the root of a third degree polynomial function using Sidi's method which is similar to Newton's Method and the Secant Method. The only difference is using the interpolated polynomial instead of the derivative of the function. I also have to test my code with 50 guesses, and I have tried writing the code but when I execute it nothing happens. This is my code
double f(double x)
{
return ((3*x*x*x)-(6*x*x)-(6*x)-5);//polynomial equation
}
double y(double z)
{
return ((17.8*z*z*z)-(34.5*z*z)+(199.7*z)+30);//interpolation equation
}
double dy (float z)
{
return ((53.4*z*z)-(69*z)+199.7);//derivative of interpolation equation
}
main()
{
double i;
int maxitr = 10;
int itr;
double a;
double b;
double maxdec = 0.000001;
for(i=1; i<= 50; i++){
printf("Input guess ");
scanf("%lf", i);
for(itr = 1; itr < maxitr; itr++){
a = f(i) / dy(i);
b = i - a;
if(fabs(a) < maxdec){
printf("root: %lf", b);
return 0;
}
i = b;
}
}
Hope this helps you. My program solves this equation f(x)=x^7-1000 = 0.You can add your equation by editing the void f(double x[],int x1,int x2) function. Also you add whatever else you want more.
#include<stdio.h>
#include<math.h>
void f(double x[],int x1,int x2);
int main()
{
double x[50];
int x1,x2;
printf("Enter x1:\n");
scanf("%d",&x1);
printf("Enter x2:\n");
scanf("%d",&x2);
printf("\nCalculating....\n");
f(x,x1,x2);
return 0;
}
void f(double x[],int x1,int x2)
{
// Assuming Equation is x^7 - 1000
// you can edit this code to keep your
// own equation
int i,prec=10000;
double a,b,num;
double denom,d1,d2;
x[0] = 0.00; //let's start with index 1 instead of 0
x[1] = x1;
x[2] = x2;
printf("x[2]:%f\n",x[2]);
for(i=3;i<=50;i++)
{
num = pow(x[i-1],7)-1000;
d1 = (pow(x[i-1],7)-1000) - (pow(x[i-2],7)-1000);
d2 = (x[i-1] - x[i-2]);
denom = d1/d2;
x[i] = x[i-1] - num/denom;
printf("x[%d]:%f\n",i,x[i]);
a= (long int)(x[i]*prec);
b= (long int)(x[i-1]*prec);
if(a == b)
{
break;
}
}
printf("x[%d]=%f and x[%d]=%f are apprx equal!\n",i-1,x[i-1],i,x[i]);
}
Where,
num is (x2)^7 - 1000
d1 is ((x2)^7 - 1000)-((x1)^7-1000)
d2 is x2-x1
denom is d1/d2

#define an input number in C

I need to get the user to input a number into a program and then need to be able to use that number in many other parts functions in the program. Any way to do that?
Here is the code:
#include <stdio.h>
#include <math.h>
#define W 8.
#define H 4.
double ellipse(double);
typedef double (*DfD) (double);
double simpsons_int (DfD, double, double, int);
int main()
{
double len, w, h, volume;
printf("Please enter a length, width and height (in meters) of the an elliptical storage tank \n");
scanf("%lf %lf %lf", &len, &w, &h);
double a = h/2.*-1., r;
for (double depth=10; depth<=400; depth=depth+10)
{
r=a+(depth/100);
volume = len*simpsons_int(ellipse, a, r, 10000);
printf("depth is %.1f, volume is %f\n", depth, volume);
}
}
double ellipse(double y)
{
double x;
double A=W/2.;
double B=H/2.;
x=2*sqrt((1-(y*y)/(B*B))*(A*A));
return x;
}
double simpsons_int(DfD f, double y0, double y1, int n)
{
double y, sum, dy = (y1 - y0)/n;
sum = f(y1) + f(y0);
for(y = y0; y <= y1-dy; y += dy)
sum += 2.0 * f(y+dy) + 4.0 * f(y + dy/2);
return sum * dy / 6.0;
}
but I need H and W to be number that are input by the user not 8 and 4.
You can either pass it as argument of the function, or declare it as global variable. I'd rather use the first, depending on the application.
1) passing as parameter. Your function should be:
double ellipse(double y, double W, double H )
{
double x;
double A=W/2.;
double B=H/2.;
x=2*sqrt((1-(y*y)/(B*B))*(A*A));
return x;
}
And then you declare and scanf W and H within main()
2) Just declare W and H before main();
double W,H;
int main()
{
double len, w, h, volume;
printf("Please enter a length, width and height (in meters) of the an elliptical storage tank \n");
scanf("%lf %lf %lf", &len, &w, &h);
scanf("%lf %lf",&W,&H);
double a = h/2.*-1., r;
for (double depth=10; depth<=400; depth=depth+10)
{
r=a+(depth/100);
volume = len*simpsons_int(ellipse, a, r, 10000);
printf("depth is %.1f, volume is %f\n", depth, volume);
}
}
Preprocessor directives like #define must be known at compile-time. Think of them as constants: you can set them, but as soon as you run the program they're set in stone.
You should be using your variables to do this; you could possibly define w and h to be global variables, but better practice would be to pass them in as parameters to the ellipse function.

Integrating function pointer

I'm new to C and I ran into some problems understanding a part of the function below.
Shortly, it integrates a numerical R -> R function with the rectangle method:
double numint(double (*f)(double), double x1, double x2, double dx)
{
double x, sum = 0;
for (x = x1; x < x2; x += dx)
sum += f(x) * dx;
return sum;
}
My question is:
1.) What does double (*f)(double) stand for? How do I call this part of the function? Is it a type not defined in the example, or is it usable by itself?
For example, exampledouble = numint( ?? , double1, double2, double3);
Thank you for your help!
f is a function pointer which requires a double as an argument and returns a double. So you have to pass in a function address using this prototype
double myfunc(double);
It is called here in this line:
sum += f(x) * dx;
Example:
double myfunc(double v)
{
return v*v;
}
int main(int argc, char *argv[])
{
double x1 = 1.0;
double x1 = 2.0;
double x3 = 5.0;
double val = numint(myfunc, x1, x2, x3)
return 0;
}
To add to the answer by Devolus, you call numint like this:
double parabola(double x) {
return x * x + 3 * x + 1;
}
int main() {
int ans = numint(parabola, 0, 3, 0.1);
// gets integral from 0 to 3 of x^2 + 3x + 1
// ...
}

Resources