identifier mod is undefined | filename extension .c - c

#include <stdio.h>
#include <math.h>
main()
{
float x,y,z;
printf("Enter the value of x,y:");
scanf("%f%f",&x,&y);
z= 2.5*log(x)+cos(32)+ mod(x*x-y*y)+ sqrt(2*x*y);
printf(" The value of the expression is %f",z);
}

By the equation you are solving, I feel that you want to find the absolute value of x*x - y*y. Something like |x|.
In that case, you should use fabs(x*x -y*y). It returns double.

Related

triangle area calculator, It keeps showing 0, why?

I am making a triangle area calculator, but it only show 0 instead of answer, It must be the formula 1/2 * ab sin c , can someone tell me what should I change to make it work.
#include <stdio.h>
#include <math.h>
float main ()
{
float a,b,c,pi ;
printf("Enter a : ");
scanf("%f",&a);
printf("Enter b : ");
scanf("%f",&b);
printf("Enter c :");
scanf("%f",&c);
printf("%f\n",(a*(1/2)*b*(c*pi)/180));
return 0;
}
The sub-expression (1/2) is always equal to 0 due to the integer arithmetic. You need to write for example (1.0f/2).
Apart from this the variable pi is not initialized
float a,b,c,pi ;
Also according to the C Standard the function main without parameters shall be declared like
int main( void )

Where do I wrong ? I don't get expected output

I try to write the code according to the problem. But I face problem with rounding issue. Can anyone explain me where I face the problem ?
M=12, T=20, X=8 tip=(20×12)/100=2.4 tax=(8×12)/100=0.96 final
price=12+2.4+0.96=15.36 Officially, the price of the meal is $15.36,
but rounded to the nearest
dollar (integer), the meal is $15.
Here's My Full Code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int t, x;
double m;
scanf("%lf", &m);
scanf("%d", &t);
scanf("%d", &x);
double tip, tax;
tip= m*t/100;
tax= t*x/100;
int total= (int)(round( tip + tax +m ));
printf("The final price of the meal is $%d.", total);
return 0;
}
When I take input 15.91,15,10 it shows output 19 instead of 20
Where have I made mistakes?
When you do
tax= t*x/100;
you are making an integer division, so it will be rounded.
You will have 15*10/100 = 1, instead of 1.5
Also, you have t*x instead of m*x, as you put on your question
M=12, T=20, X=8
tip=(20×12)/100=2.4
T M
tax=(8×12)/100=0.96
X M
Change it to
tip= m*t/100.0;
tax= m*x/100.0;
And you will get the expected 20 as the final result

Surface area of a hexagon

I am currently attempting to learn C, and have made this program to calculate the area of a regular hexagon:
#include <stdio.h>
#include <math.h>
void main(){
int a;
float ans;
scanf("%d", &a); // get length of side
ans = ((pow(a, (1/3)))/2)*(a*a);
printf("%f", ans);
}
However, it outputs seemingly random numbers.
Firstly your code doesn't compile (Missing semicolon) and also you should use int main() instead of void main().
Secondly your formula also wrong, the area of a regular hexagon of side length a is calculated as ((3√3)/2)*a².
Thirdly Expression like 1/3 always yield zero as both are integer, to get expected behavior make one of them float/double. like 1.0/3 or (float)1/3 etc.
#include <stdio.h>
#include <math.h>
int main()
{
int a;
float ans;
scanf("%d", &a); // get length of side
ans = (3*sqrt(3)/2.0)*a*a;
printf("%f", ans);
}

Input a value for a scanf(), but nothing happens

I'm writing some code as part of a few assignment exercises to learn C programming from absolute basics, and I've run into a problem, which is probably quite simple to solve, but I'm completely stuck! I'm writing a program to implement a basic Newton's method for differentiation. Whenever I input an initial value to the scanf(), the program simply stops, doesn't return anything, terminate or freeze. Any help would be great.
Here is my code to start with:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//function to be used f(x) = cos(x)-x^3
//f'(x) = -sin(x)-3x^2
int main()
{
double x, xold;
printf("Enter an initial value between 0 and 1\n");
scanf("%lf",&xold);
double eps = 1e-12;
x = xold - ((cos(xold)-pow(xold,3))/(-(sin(xold)-(3*pow(xold,2)))));
while (fabs(x-xold)>eps)
{
x = xold - ((cos(xold)-pow(xold,3))/(-sin(xold)-(3*pow(xold,2))));
}
printf("The answer is %.12lf",x);
return 0;
};
In your while loop:
x = xold - ((cos(xold)-pow(xold,3))/(-sin(xold)-(3*pow(xold,2))));
the value of the = right operand is always the same, how could you exit the loop once you enter it?
Actually the thing is you are not updating your xold variable. Try the below modified code for your problem, see if I have done correctly:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
double x, xold;
printf("Enter an initial value between 0 and 1\n");
scanf("%lf",&x);
double eps = 1e-12;
x = x - ((cos(x)-pow(x,3))/(-(sin(x)-(3*pow(x,2)))));
while (fabs(x - xold)>eps)
{
xold = x;
x = x - ((cos(x)-pow(x,3))/(-sin(x)-(3*pow(x,2))));
}
printf("The answer is %.12lf\n",x);
return 0;
}

Using pow() in C

I have this problem with this homework I'm supposed to do.
[ It says Create a program that's able to calculate and show the sum of S]
Like S=1+1/4+1/8+1/16 ... till 1/ [2 pow n]
So I worked on it and came up with this code
#include <stdio.h>
void main()
{
int n,i;
float p,s;
printf("Enter the maximum power n :");
scanf("%d",&n);
s=0;
p=0;
for (i=0;i<n;i++)
{
p+=1/pow(2, i);
s+=p;
printf("s = %f\n",s);
}
printf("The sum of this equation is :%f",&s);
}
But when I execute it is always like S=0.
What am I doing wrong?
You are printing an address ('&s) with%f` variable. Using a wrong specifier invokes undefined behavior. You may get anything.
Also, No need of variable s. Remove the line
s+=p;
It should be like:
#include <stdio.h>
int main(void)
{
int n,i;
float p;
printf("Enter the maximum power n :");
scanf("%d",&n);
p=0;
for (i=0;i<n;i++)
{
p+=1/pow(2, i);
printf("p = %f\n",p);
}
printf("The sum of this equation is :%f",p);
}
You need to include <math.h> to get the proper prototype of pow().
You might need to link to the math library too gcc main.c -Wall -lm
#include <math.h>
....
for (i=0;i<n;i++)
{
p=1/pow(2, i);
s+=p;
printf("s = %f\n",s);
}
printf("The sum of this equation is :%f",s);
Your program has multiple problems. Enabling compiler warnings should tell you about some of them.
You should include the C header which contains the declaration of the pow function.
You add each addend twice.
In your second printf statement, you pass a float. But the %f format specifier expects a double argument. In your third printf statement, you pass a pointer to a float.
Another cosmetic problem is that your main function should return an int.
just a guess, may you replace the line
p+=1/pow(2, i);
with
p+=1.0f/(float)pow(2, i);
and
printf("The sum of this equation is :%f",&s);
with
printf("The sum of this equation is :%f",s);
Typo may be.. but you will have say %f and s (not &s)
printf("The sum of this equation is :%f",s);
On side note:
Once you include <math.h> you will get compiler warning for using correct pow(..) prototype. Below code would be relevant.
p+=1.0f/(float)pow(2.0f, i);

Resources