I wrote this code in C and it shows me this error.
I don't know what it is or how can I solve it.
#include <stdio.h>
#include <math.h>
int main()
{
int z;
scanf("%d", &z);
double x1 , x2 , x3 , x4 , y1 , y2 , y3 , y4;
for(int i = 0;i<=z;i++)
{
scanf("%lf %lf", &x1 , &y1);
scanf("%lf %lf", &x2 , &y2);
scanf("%lf %lf", &x3 , &y3);
scanf("%lf %lf", &x4 , &y4);
double tule_parekhat1 = sqrt(pow(y2-y1, 2) + (pow(x2-x1), 2));
double tule_parekhat2 = sqrt(pow(y3-y2, 2) + (pow(x3-x2), 2));
double tule_parekhat3 = sqrt(pow(y4-y1, 2) + (pow(x4-x1), 2));
double tule_parekhat4 = sqrt(pow(y4-y3, 2) + (pow(x4-x3), 2));
}
}
I get the error (line 15, error : too few arguments to function 'pow')
I don't know what it is.
You have wrong usage of parentheses.
double tule_parekhat1 = sqrt(pow(y2-y1, 2) + (pow(x2-x1), 2));
in the (pow(x2-x1), 2)) part, it should be
double tule_parekhat1 = sqrt(pow(y2-y1, 2) + pow(x2-x1, 2));
function pow sees only x2-x1
Your code contains:
double tule_parekhat1 = sqrt(pow(y2-y1, 2) + (pow(x2-x1), 2));
with 2 call of pow function. In first call you correctly write pow(y2-y1, 2), actually calling the function with 2 parameters. But in second one, you write:
(pow(x2-x1), 2)
Here you only call pow with the single x2-x1 parameter and then use the comma (,) operator to discard that value and only retain the second one. This is incorrect and the compiler correctly raises an error.
You should write pow(x2-x1, 2) as you did for first call.
BTW, and IMHO you are abusing the pow function here. It is intended to be used for complex operation where both the operands are floating point values. Here the idiomatic way would be:
double tule_parekhat1 = sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1));
...
using only addition and product operators.
Related
I'm a beginner in C and have this problem: I'm supposed to make an app where you insert the coordinates of a triangle's vertices, and then it prints details about its area, perimeter and most interesting of all, it's supposed to print its angles. The code is supposed to be written using the double tangent equation from Heron's formula. I've tried doing it using atan(), but I guess I should add +n to avoid going out of the domain. Don't know how though.
Here's the equation. And below is my code:
#include <stdio.h>
#include <math.h>
#define PI (4. * atan(1))
int main() {
// Defining floats
float xa, ya, xb, yb, xc, yc, s, P, a, b, c, x, y, z, alphadeg, alpharad, betadeg, betarad, gammadeg, gammarad;
// Inserting coordinates of each points
printf("Insert the first point's coordinates (a space should be between the X and Y coordinate):\n");
scanf("%f %f", &xa, &ya);
printf("Insert the second point's coordinates (a space should be between the X and Y coordinate):\n");
scanf("%f %f", &xb, &yb);
printf("Insert the third point's coordinates (a space should be between the X and Y coordinate):\n");
scanf("%f %f", &xc, &yc);
// Calculating and printing length of each side
printf("Distance between point 1 and 2: %f\n", sqrt(pow(xa - xb, 2) + pow(ya - yb, 2)));
printf("Distance between point 1 and 3: %f\n", sqrt(pow(xa - xc, 2) + pow(ya - yc, 2)));
printf("Distance between point 2 and 3: %f\n", sqrt(pow(xb - xc, 2) + pow(yb - yc, 2)));
// Defining each side
a = sqrt(pow(xa - xb, 2) + pow(ya - yb, 2));
b = sqrt(pow(xa - xc, 2) + pow(ya - yc, 2));
c = sqrt(pow(xb - xc, 2) + pow(yb - yc, 2));
// Defining s as the parameter from Heron's formula
s = ((a + b + c) / 2);
// Defining P as the area from Heron's formula
P = sqrt(s * (s - a) * (s - b) * (s - c));
// Printing the area and perimeter of the triangle
printf("The area of your triangle is %f\n", P);
printf("The perimeter of your triangle is %f\n", a + b + c);
// Angles
/*
tan(alpha/2)=sqrt(((P-b)*(P-c))/(P*(P-a)));
tan(beta/2)=sqrt(((P-a)*(P-c))/(P*(P-b)));
tan(gamma/2)=sqrt(((P-a)*(P-a))/(P*(P-c)));
Let
x = tan(alpha/2)
y = tan(beta/2)
z = tan(gamma/2)
*/
x = sqrt(((P - b) * (P - c)) / (P * (P - a)));
y = sqrt(((P - a) * (P - c)) / (P * (P - b)));
z = sqrt(((P - a) * (P - b)) / (P * (P - c)));
alphadeg = (atan(x)) * 360 / PI;
betadeg = (atan(y)) * 360 / PI;
gammadeg = (atan(z)) * 360 / PI;
alpharad = 2 * (atan(x));
betarad = 2 * (atan(y));
gammarad = 2 * (atan(z));
printf("The value of the alpha angle is %0.3f\n", alphadeg);
printf("The value of the beta angle is %0.3f\n", betadeg);
printf("The value of the gamma angle is %0.3f\n", gammadeg);
printf("%f = %f", PI, (alpharad + betarad + gammarad));
return 0;
}
The issue is caused by a misunderstanding of the mathematical formulas.
// That's the formula to calculate the semi perimeter of a triangle
// given the length of its sides a, b and c.
s = ((a + b + c) / 2);
// Defining P as the area from Heron's formula
P = sqrt(s * (s - a) * (s - b) * (s - c));
The name P, here, is misleading (s too, actually) and not only because they are one-letter variable names. In the following formulas, the OP uses P instead of the semi-perimeter:
x = sqrt(((P - b) * (P - c)) / (P * (P - a)));
// ^ ^ ^ ^ You should use 's' instead.
You can consider to add epsilon like below:
float EPSILON = 0.1
x = sqrt(((P-b)*(P-c))/(EPSILON+P*(P-a)));
I was given a problem to write a C program which would solve the equation ax2+bx+c=0, where a, b and c are coefficients with double type. Any of the coefficients may be zero. In this problem it is unclear to me how to handle the double variables.
Here is my code. As for now, I know that my program can't distinguish between two roots and infinitely many roots. It also doesn't detect the "linear equation situation". How can I make it detect an infinite number of solutions? I was also advised in the comments to calculate the root with the minus before the discriminant if b > 0 and then use the Viet's theorem. I understand that it is because it is always more accurate to sum two numbers. I also guess I should do the exact opposite with b < 0. But what if b == 0 ? In this case, the program will not do anything. Or should I just include b == 0 in b < 0 and have b <= 0 ?
#include <stdio.h>
#include <math.h>
#include <float.h>
int main() {
double a, b, c, x1, x2;
scanf("%lf", &a);
scanf("%lf", &b);
scanf("%lf", &c); // just reading variables
//ax^2+bx+c=0
if ((b * b - 4 * a * c) < 0) {
printf("no");
} else {
x1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a); //calculating roots
x2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
if ((fabs((a * x1 * x1 + b * x1 + c)) < DBL_EPSILON) & (fabs((a * x2 * x2 + b * x2 + c)) < DBL_EPSILON)) { //plugging the roots in
if (fabs((x1 - x2)) < DBL_EPSILON) { //checking if the roots are equal
printf("%lf", &x1); // if they are equal, we print only one of them
} else {
printf("%lf", &x1); // if they are not equal, we print both.
printf("\n %lf", &x2);
}
} else { // if there are no two valid roots
if ((fabs((a * x1 * x1 + b * x1 + c)) < DBL_EPSILON)) // we try to find one root.
printf("%lf", &x1);
if (fabs((a * x2 * x2 + b * x2 + c)) < DBL_EPSILON)
printf("%lf", &x2);
if ((fabs((a * x1 * x1 + b * x1 + c)) > DBL_EPSILON) & (fabs((a * x2 * x2 + b * x2 + c)) > DBL_EPSILON)) // if both of the plugged roots don't satisfy the equation
printf("no");
}
}
return 0;
}
Solve quadratic equation when coefficients may be 0
How can I make it detect an infinite number of solutions?
When a==0 && b == 0 && c == 0.
No DBL_EPSILON needed really anywhere in this code. See also #Eric Postpischil.
But what if b == 0 ?
if (b == 0) { // y = a*x*x + c
if (a) {
double dd = -c/a;
if (dd >= 0) {
double d = sqrt(d);
printf_roots("+/- roots", d,-d);
} else {
printf_roots("Complex roots", NAN, NAN); // Note NAN may not exist
}
} else if (c) { // y = 0*x*x + c, c != 0
printf_roots("No roots", NAN, NAN);
} else { // y = 0*x + 0
printf_roots("Infinite roots", -HUGE_VAL, HUGE_VAL);
}
Or should I just include b == 0 in b < 0 and have b <= 0 ?
Unless the coding goal requires a special output when b==0, I would only vector code on b==0 as a subtest when a==0 occurred.
if (a==0) {
if (b == 0) {
The quadric equation, like much FP code, can readily overflow and hit 0, both cases losing all precision.
Consider the code below: the unnecessary subtraction may cause overflow or truncation to 0 versus the second which may not. It is dependent on many things.
if ((b * b - 4 * a * c) < 0)
//
if (b * b < 4 * a * c)
Further, C allows various calculations to occur using wider math. Research FLT_EVAL_METHOD. Because of this, to prevent sqrt(value_less_than_0), code should calculate the discriminate and then test the object x that is going to be applied to sqrt(x).
//if ((b * b - 4 * a * c) < 0) {
// printf("no");
//} else {
// x1 = (-b + sqrt(b * b - 4 * a * c))
double discriminate = b * b - 4 * a * c;
if (discriminate < 0) {
printf("no");
} else {
double d = sqrt(discriminate);
x1 = (-b + d)
As to the idea of "calculate the root with the minus before the discriminant if b > 0 and then use the Viet's theorem", I'd suggest for improved retained precision the below which does not subtract like signed values.
double d = sqrt(discriminate);
// Note x1*x2 = c/a
if (b < 0) {
x2 = (-b + d)/(2*a);
x1 = c/a/x2;
} else {
x1 = (-b - d)/(2*a);
x2 = c/a/x1;
}
printf_roots("2 roots", x1, x2);
Notes on printf("%lf", &x1);. You are not compiling with all warnings enabled. Save time - enable them. Should be printf("%lf", x1); No &.
Further double is floating point. For FP code development use "%e", "%a" or"%g" to full see significant information.
printf("%g\n", some_double);
// or better
printf("%.*e\n", DBL_DECIMAL_DIG -1, some_double);
Since division by zero is not allowed, you have to split the problem into 4 cases :
a != 0:
this is case you treated in your code.
a == 0 && b != 0 :
This is a linear equation where the solution is x = -c/b
a == 0 && b == 0 && c != 0 : There's no possible value for x.
In this last case, a, b and c are equals to 0 : there's infinitly many solutions for x.
EDIT: comparisons with epsilon removed since they seem to be useless
There are some problems in your code:
you should check the return values of scanf() to avoid undefined behavior on invalid input.
you should use local variables for intermediary results to improve code readability
your printf statements are incorrect: you should pass the values of the double variables instead of their addresses: printf("%lf", &x1); should read:
printf("%f", x1);
Regarding the degenerate cases, you should just test those before trying to resolve the second degree equation.
Here is a corrected version:
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, delta, x1, x2;
if (scanf("%lf%lf%lf", &a, &b, &c) != 3) {
printf("invalid input\n");
return 1;
}
if (a == 0) {
// not a quadratic equation
if (b != 0) {
printf("one solution: %g\n", -c / b);
} else {
if (c != 0) {
printf("no solution\n");
} else {
printf("all real values are solutions\n");
}
}
} else {
delta = b * b - 4 * a * c;
if (delta < 0) {
printf("no real solution\n");
} else
if (delta == 0) {
printf("one double solution: %g\n", -b / (2 * a));
} else {
x1 = (-b + sqrt(delta)) / (2 * a);
x2 = (-b - sqrt(delta)) / (2 * a);
printf("two solutions: %g, %g\n", x1, x2);
}
}
return 0;
}
I've been trying to do Riemann Sums to approximate integrals in C. In my code below, I'm trying to approximate by both the trapezoidal way and the rectangular way (The trapezoidal way should be better, obviously).
I tried making an algorithm for this on paper, and I got the following:
NOTE: N is the number of rectangles (or trapezoids) and dx is calculated using a, b and N ( dx = (b-a)/N ). f(x) = x^2
Rectangular Method:
<img src="http://latex.codecogs.com/png.latex?\int_a^b&space;x^2&space;dx&space;\approx&space;\sum_{i=1}^N&space;f(a&space;+&space;(i-1)dx)dx" title="\int_a^b x^2 dx \approx \sum_{i=1}^N f(a + (i-1)dx)dx" />
Trapezoidal Method:
<img src="http://latex.codecogs.com/png.latex?\int_a^b&space;x^2&space;dx&space;\approx&space;\sum_{i=1}^N&space;[f(a&space;+&space;(i-1)dx)&space;+&space;f(a&space;+&space;i\cdot&space;dx)]dx" title="\int_a^b x^2 dx \approx \sum_{i=1}^N [f(a + (i-1)dx) + f(a + i\cdot dx)]dx" />
Code (In the following code, f(x)=x^2 and F(x) is it's antiderivative (x^3/3):
int main() {
int no_of_rects;
double a, b;
printf("Number of subdivisions = ");
scanf("%d", &no_of_rects);
printf("a = ");
scanf("%lf", &a);
printf("b = ");
scanf("%lf", &b);
double dx = (b-a)/no_of_rects;
double rectangular_riemann_sum = 0;
int i;
for (i=1;i<=no_of_rects;i++) {
rectangular_riemann_sum += (f(a + (i-1)*dx)*dx);
}
double trapezoidal_riemann_sum = 0;
int j;
for (j=1;j<=no_of_rects;j++) {
trapezoidal_riemann_sum += (1/2)*(dx)*(f(a + (j-1)*dx) + f(a + j*dx));
printf("trapezoidal_riemann_sum: %lf\n", trapezoidal_riemann_sum);
}
double exact_integral = F(b) - F(a);
double rect_error = exact_integral - rectangular_riemann_sum;
double trap_error = exact_integral - trapezoidal_riemann_sum;
printf("\n\nExact Integral: %lf", exact_integral);
printf("\nRectangular Riemann Sum: %lf", rectangular_riemann_sum);
printf("\nTrapezoidal Riemann Sum: %lf", trapezoidal_riemann_sum);
printf("\n\nRectangular Error: %lf", rect_error);
printf("\nTrapezoidal Error: %lf\n", trap_error);
return 0;
}
Where:
double f(double x) {
return x*x;
}
double F(double x) {
return x*x*x/3;
}
I have included the math and stdio header files. What is happening is that the rectangular riemann sum is okay, but the trapezoidal riemann sum is always 0 for some reason.
What is the problem? Is it something in my formulas? Or my code?
(I am a newbie in C by the way)
Thanks in advance.
In this statement:
trapezoidal_riemann_sum += (1/2)*(dx)*(f(a + (j-1)*dx) + f(a + j*dx));
1/2 == zero, so the whole statement is zero. Change at least the numerator, or the denominator to the form of a double to get a double value back. i.e. 1/2.0 or 1.0/2 or 1.0/2.0 will all work.
I have the following code in C to make some arithmatic calculation
#include <stdio.h>
#include <stdlib.h>
int main()
{
float x,y;
float z;
printf("Enter x y z \n");
scanf("%f %f %f ", &x, &y , &z);
z = ((4.2 (x+y)))/ (z - (0.25*z))/ (y+z)/ ((x+y) * (x+y));
printf("\n z = %f", z);
return 0;
}
when i build the program , i get the following error message in the following code line
z = ((4.2 (x+y)))/ (z - (0.25*z))/ (y+z)/ ((x+y) * (x+y));
called object is not a function or dunction pointer
That's a typo, you're missing an operator:
z = ((4.2 * (x + y))) / (z - (0.25*z)) / (y + z) / ((x + y) * (x + y));
^
whatever the operator is
C has no support for mathematics-implicit multiplication operator (more or less as you would write in an equation in school). E.g.
// y = 2x
int y;
y = 2 * x;
I made a program that calculates the equation ( gives me the values x1 and x2 ). But the problem is though , i needed to write 2 seperate functions for x1 and x2 , even though i only needed to change a "+" sign to a "-" sign to get x2. Is it possible to get the same output put only using one function ? Heres the code :
double equation(double a, double b, double c) {
double argument, x1;
argument = sqrt(pow(b, 2) - 4*a*c);
x1 = ( -b + argument ) / (2 * a);
return x1;
}
double equation2(double a, double b, double c) {
double argument, x2;
argument = sqrt(pow(b, 2) - 4*a*c);
x2 = ( -b - argument ) / (2 * a); // here i changed the "+" sign to "-"
return x2;
}
Thank you in advance !
Theres a couple different ways you can do this. Gareth mentions one, but another is to use output parameters.
Using pointers as input parameters, you can populate them both in one function, and you don't need to return anything
void equation(double a, double b, double c, double *x1, double *x2) {
double argument, x1;
argument = sqrt(pow(b, 2) - 4*a*c);
*x1 = ( -b + argument ) / (2 * a);
*x2 = ( -b - argument ) / (2 * a);
}
Then call it from your main code:
int main (void )
{
//Same up to the prints above
double x1, x2;
equation ( a , b, c , &x1, &x2);
printf("\nx1 = %.2f", x1);
printf("\nx2 = %.2f", x2);
}
Pass in another argument that's either +1 or -1, and multiply argument by it. Or, pass in another argument that's either 0/false or non-0/true, and add or subtract conditionally (with an if statement or a ...?...:... "ternary operator".
[EDITED to remove a response to part of the original question that's now been removed.]
Well, you could do something like:
double equation_either (double a, double b, double c, double d) {
double argument, x1;
argument = sqrt(pow(b, 2) - 4*a*c);
x1 = ( -b + (d * argument)) / (2 * a);
// ^^^^^^^^^^^^^^
// auto selection of correct sign here
//
return x1;
}
:
printf("\nx1 = %.2f", equation_either(a, b, c, 1.0));
printf("\nx2 = %.2f", equation_either(a, b, c, -1.0));