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));
Related
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.
#include <math.h>
#include <stdio.h>
main() {
int a, b, c, x, x1, x2;
printf("enter the values of a,b,c:");
scanf("%d%d%d", &a, &b, &c);
printf("The quadratic equation is %d*pow(x,2)+%d*x+%d=0", a, b, c);
if (pow(b, 2) - 4 * a * c >= 0) {
x1 = (-b + sqrt(pow(b, 2) - 4 * a * c)) / 2 * a;
x2 = (-b - sqrt(pow(b, 2) - 4 * a * c)) / 2 * a;
printf("the roots of the equation are x1=%d,x2=%d", x1, x2);
}
else
printf("roots of the equation in the form of x+iy and x-iy");
return 0;
}
Is this code alright for the given question, i had a bit confusion at that printing imaginary roots. could you please help
Use proper main prototype.
Use floating point numbers instead of integers
pow(b,2) == b*b
/ 2 * a -> / (2 * a)
int main(void) {
double a, b, c, x, x1, x2;
printf("enter the values of a,b,c:");
if(scanf("%lf %lf %lf", &a, &b, &c) != 3) { /* handle error */}
printf("\nThe quadratic equation is %f*x^2+%f*x+%f=0\n", a, b, c);
if (b*b - 4 * a * c >= 0) {
x1 = (-b + sqrt(b*b - 4 * a * c)) / (2 * a);
x2 = (-b - sqrt(b*b - 4 * a * c)) / (2 * a);
printf("the roots of the equation are x1=%f,x2=%f\n", x1, x2);
}
else
{
double r = -b / (2*a);
double z = sqrt(fabs(b*b - 4 * a * c));
printf("the roots of the equation are x1 = %f + i%f, x2 = %f - i%f", r,z,r,z);
}
}
https://gcc.godbolt.org/z/Ys1s8bWY7
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 am trying to calculate the square root of x using Newton's method. Everything works and a is equal to the square root of x right until I return it when it gives me back a completely different (always constant) number that is much larger.
int main()
{
float newtonA;
float newtonX = 35735;
float epsilon = 0.001;
newtonA = newtonX / 2;
printf("\n********RECURSIVE NEWTON********\n");
printf("The square root of %0.1f is: %0.2f\n", newtonX, newtonRec(newtonX, newtonA, epsilon));
return 0;
}
float newtonRec (float x, float a, float eps)
{
if (abs(a * a - x) <= eps )
{
printf("\n****%0.2f****\n", a);*/
return a;
}
else
{
printf("\n***a: %.1f x: %.1f***\n", a, x);
a = (a + x / a) / 2;
newtonRec(x, a, eps);
}
return a;
}
Please change
a = (a + x / a) / 2;
newtonRec(x, a, eps);
to
a = (a + x / a) / 2;
return newtonRec(x, a, eps);
> $./a.out
> ****189.04**** The square root of 35735.0 is: 189.04
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
// ...
}