C basic - called object is not a function or function pointer - c

That's my code -
main()
{
double x;
double y = pow(((1/3 + sin(x/2))(pow(x, 3) + 3)), 1/3);
printf("%f", y);
return 0;
}
I get an error in double y = pow((1/3 + sin(x/2))(pow(x, 3) + 3), 1/3);, it says that called object is not a function or function pointer. I don't get it though - (1/3 + sin(x/2))(pow(x, 3) + 3) is the first element of pow(x, y); that is the x I want to raise to y (1/3) power. Where lies the problem? I'm pretty new to c basic but I can't find the answer anywhere.

If you want to multiply, you need to use the * operator. You can't put parenthesized expressions adjacent to each other to denote multiplication.
(1/3 + sin(x/2))*(pow(x, 3) + 3)

That's because the return value of pow is a double, not a function. Maybe what you're trying to do is pass a call to pow as the second argument to the first pow.

#include <stdio.h>
#include <math.h> // For pow() function
int main() {
// Initialize with whatever value you want
double x = 100;
// Make sure to use an arithmetic operator
double y = pow(((1/3.0 + sin(x/2))*(pow(x, 3) + 3)), 1/3.0);
// Use right format specifier
printf("%lf", y);
return 0;
}
Don't forget to include math.h library.
Initialize x with a value.
Avoid using integer division 1/3, instead use 1/3.0 or 1.0/3.0 because 1/3 == 0.
Use an arithmetic operator (+, -, *, /) before middle pow() like pow(((1/3.0 + sin(x/2))+(pow(x, 3) + 3)), 1/3.0).
(Opt.) Use right format specifier for y which is %lf. But you may get away with %f.

Related

Two different answers for same expression in C

I have an expression which does the same calculation. When I try to do the whole calculation in a single expression and store it in variable "a", the expression calculates the answer as 0. When I divide the equations in two different parts and then calculate it, it gives the answer -0.332087. Obviously, -0.332087 is the correct answer. Can anybody explain why is this program misbehaving like this?
#include<stdio.h>
void main(){
double a, b, c;
int n=0, sumY=0, sumX=0, sumX2=0, sumY2=0, sumXY=0;
n = 85;
sumX = 4276;
sumY = 15907;
sumX2 = 288130;
sumY2 = 3379721;
sumXY = 775966;
a = ((n*sumXY) - (sumX*sumY)) / ((n*sumX2)-(sumX*sumX));
b = ((n*sumXY) - (sumX*sumY));
c = ((n*sumX2) - (sumX*sumX));
printf("%lf\n", a);
printf("%lf\n", b/c);
}
Output:
0.000000
-0.332097
In your program
a = ((n*sumXY) - (sumX*sumY)) / ((n*sumX2)-(sumX*sumX));
all the variables in the right hand side are of type int, so it will produce a result of type int. The true answer -0.332097 is not a int value, so it will be converted to a valid int value, namely 0. And this 0 is assigned to variable a.
But when you do
b = ((n*sumXY) - (sumX*sumY));
c = ((n*sumX2) - (sumX*sumX));
printf("%lf\n", b/c);
The variable b and c are of type double, so the expression b/c produce a double typed value and the true answer -0.332097 is a valid double value. Thus this part of your code give a right result.
In first equation i.e. a = ((n*sumXY) - (sumX*sumY)) / ((n*sumX2)-(sumX*sumX)) both numerator and denominator will give integer results and the value stored in a will also be integer as integer/integer is integer. In second and third expression as you are solving them individually both b and c will be stored in double and double/double will result in a double i.e. a decimal value.
This problem can be solved by using type casting - or better still using float for the variables.
Add double before your calculation, so after you do your integer calculation in "a", it will convert it to double.
a = (double)((n*sumXY) - (sumX*sumY)) / ((n*sumX2)-(sumX*sumX));
First of all (INTEGER)/(INTEGER) is always an INTEGER. So, you can typecast it like a = (double)(((n*sumXY) - (sumX*sumY)) / ((n*sumX2)-(sumX*sumX)));
OR
We know that any number (n ∈ ℂ), multiplied by 1.0 always gives the same number (n). So, your code shall be like:
a = ((n*sumXY*1.0L) - (sumX*sumY)) / ((n*sumX2)-(sumX*sumX));
Multiplying by 1.0L or 1.0f converts the whole arithmetic operation to long double data type.
Now you can print the number (-0.332097) to stdout.
Non-standard Code
Your code is:
void main()
{
//// YOUR CODE
}
Which is non-standard C. Instead your code should be like:
int main(int argc, char **argv)
{
//// YOUR CODE
return 0;
}
Change all your INTEGERS to DOUBLES. That should solve the problem
The type of 1st expression is int whereas in 2nd expression it is double.

the code does not work error: called object is not a function or function pointerr

#include <math.h>
#include <stdio.h>
int main() {
double x0, x, eps, dx, r;
x0 = 0.60000;
x = x0;
eps = 0.0001;
do {
r = x + pow(x, 1 / 2) + pow(x, 1 / 3) - 2.5;
dx = (r / (1 - 1 / 2 (x , -1 / 2) + 1/3 (x, -1 / 3)));
x = (x - dx);
} while ((abs(dx)) > eps);
printf("%f %f\n", x, r);
return 0;
}
*error: called object is not a function or function pointer *
the code does not work, what needs to be done to make everything right
You have tons of syntax and semantic errors:
pow(x, 1 / 2)
Here 1/2 does integer division which means that the resulting 0.5 is chopped to 0.
To get floating point results you must ensure that at least one operand is a floating type like 1.0/2.
Then you have a similar piece
1/2(x,-1/2)
Again, due to integer division -1/2 evaluates to 0.
Furthermore you are missing some operator before the brackets.
That means the compiler expects some function call.
But 2 is not a function. That is probably the place where you get your error message.
If you use , in other situations than a parameter list or variable definition list, it is taken as a "comma operator" that evaluates to the second operand. That means (x,-1/2)evaluates to (-1/2) which again evaluates to 0.
In this case I have no idea what you want to achieve with that expression and cannot give some fix for that.
Finally, you use a function that is not suitable for floating point numbers and don't provide a prototype for it as well:
abs(dx)
You should use fabs instead.
Usage of abs is also broken because it requires header stdlib.h which you do not include.

How do I translate a polynomial with C programming without exponent operators?

I was looking in my C textbook and inside there was a page where the prompt told me to translate a polynomial into C code. We have not discussed exponent operators yet and are specifically instructed not to use them at this point and find another way with basic operators. The polynomial goes as such: 5x^(2)+3x-2.
How do I do this?
Note that ax^2 + bx + c can be written as
c + x*(b + x*(a))
This can easily be extended to any order of polynomial.
There is no such thing as an exponent operator in C. While you can accomplish the same thing using pow(). I suspect your book does not want this. Given this limitation you can do the operation of x^2 as simply x * x where x is a variable for your function.
i.e. You can do something like this:
int poly(int x) {
int y = ((5 * x * x) + (3 * x) - 2);
return y;
}
Addendum:
If you want to have a general formula that you can easily extend for any polynomial degree, you can use this formula instead, with inputs for a, b, c and x:
int poly(int a, int b, int c, int x) {
int y = c + x*(b + x*(a));
return y;
}
Thanks to chux and FredK for this.
I think you should parameter a,b,c and x in the second polynomial function
int poly2(int a, int b, int c, int x)
{
int y = a*x*x+b*x+c;
return y;
}
when using this function for your case you can call
int result = poly2(a,b,c, x)
with a specific set of a,b,c,x
C doesn't have an exponent operator.
One really handy way to model polynomials is to use an array to store the coefficients, such that the array index corresponds to the power of x. IOW, to model 5x2 + 3x - 2, use
double coef[] = {-2.0, 3.0, 5.0}; // -2.0 + 3.0x + 5.0x^2
To evaluate the polynomial, use a loop, taking into account the property that FredK mentions in his answer - 5x2 + 3x - 2 == ((5)x + 3)x - 2:
size_t num_elements = sizeof coef / sizeof coef[0]; // yields 3 in this case
double result = 0;
for (size_t i = num_elements - 1; i > 0; i--)
result += x * ( result + coef[i] );
result += coef[0];
This method will work for polynomials of any degree.

Invalid operands error while calculating consecutive not free square numbers

I am trying to create a program in C to find the k'th continuing not free square numbers.
For example if k=3 it will print 48,49,50.
However I'm constantly hitting this error:
[Error] invalid operands of types 'double' and 'double' to binary 'operator%'
The error is in this line: if (x % pow(j, 2)=0)
Here is my code:
#include <stdio.h>
#include <math.h>
#define K 6
int main()
{
int i,j,x;
while(i!=0)
{
for (x=4; x<=1000000000; x++)
for(j=2; j<=113; j++)
{
if (x % pow(j, 2)=0)
{
printf("%d",x);
}
}
}
}
A single equality sign in C is assigning the value form the right to the variable on the left. For comparison, use a dounle equality sign.
As soon as you have a bug you do not understand, instead of relying on the precedence of operators, add brackets
The % operator works on integers and pow returns a double, so you have to cast your result to an integer
Try replacing if (x % pow(j, 2)=0) by if ((x % (int) pow(j, 2))==0).

Casting int as a double is returning #INF00

So I am trying to cast (3/17) as double. This is for an assignment so the professor wants it to be this way for some reason.
I am trying to cast it by doing the following:
(double)(3/17)
Actual code:
int assignment7()
{
#include <stdio.h>
#define PI 3.14
int a=0;
double Ny=0,y=0,z=0,x=0,amod2=0;
printf("Enter values for x,y,z and a(must be an odd number): ");
scanf("%lf%lf%lf%d",&x,&y,&z,&a);
amod2=a%2;
printf("%.2lf\n",test);
Ny=y / (double)(3/17) - z + x / amod2 + PI;
printf("%lf\n",Ny);
}
The problem is occurring on the second to last line where it is interpreting 3/17 as an int thus it would equal 0. y / 0
Professors exact instructions:
"General equation: y = y / (3/17) - z + x / (a % 2) + PI (recall: a is an integer; the 3 and 17 constants in the equation should be left as integers initially, but explicitly type-casted as floating-point values)"
(3/17) is equal to 0 because it is evaluated using integer arithmetic, and so you get a divide by zero, which is of course a run-time error. Change:
(double)(3/17)
to:
(3.0 / 17.0)
Note that the cast is redundant.
You have a division by zero as 3/17 is zero.
Instead use doubles to begin with: 3.0 / 17.0, then you don't even need the cast.

Resources