Making a program that should calculate a polynomials y values derivative and integral... Right now having problems formatting the for loop so it cycles through all the x values I want it to (as determined by user).
Pretty sure the math func isnt fully correct right now too but I can go at them later once i get this loop working haha
heres my code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void explain();
void math(double a, double b, double c, double x, double xi, double *fx, double *fD, double *A);
int main()
{
double a, b, c;
double xi, xf, xc, x=0;
double fx=0, fD=0, A=0;
printf("SECOND DEGREE POLYNOMIAL CALCULATOR\n\n");
explain();
printf("\n\nEnter a value for a: ");
scanf("%lg", &a);
printf("Enter a value for b: ");
scanf("%lg", &b);
printf("Enter a value for c: ");
scanf("%lg", &c);
printf("\nYour function is %lgx^2%+-lgx%+-lg", a, b, c);
printf("\n\nEnter your initial x-value: ");
scanf("%lg", &xi);
printf("Enter your final x-value: ");
scanf("%lg", &xf);
printf("Enter what you would like to increment by: ");
scanf("%lg", &xc);
printf("| x | f(x) | f'(x) | A |\n"); //printing table
printf("----------------------------------------\n");
for(int i=xi; i<xf; i++) {
math(a, b, c, x, xi, &fx, &fD, &A);
printf("| %.3lf | %.3lf | %.3lf | %.3lf |\n", x, fx, fD, A);
x = x + xc;
}
return;
}
void explain() {
printf("This program computes the integral and derivative of a user inputted second-degree polynomial (f(x)=ax^2+bx+c).\n");
printf("You will be asked to enter the 3 coefficients of your polynomial, followed by your initial x-value, your\n");
printf("final x-value, and the increment value between each x.");
}
void math(double a, double b, double c, double x, double xi, double *fx, double *fD, double *A) {
*fx = (a*(x*x)) + (b*x) + c; //finding y values
*fD = (2*a) + b; //finding derivative values
*A = ((a/3)*pow(x,3) + (a/2)*pow(x,2) + c*x) - ((a/3)*pow(xi,3) + (a/2)*pow(xi,2) + c*xi); //finding integral values
return;
}
Heres a screenshot of the output, as you can see the table is printing only till 1 instead of 5 like wanted (indicated by inputs). I need to change whats in the for loop to get it to output right but Im not sure what to do
I saw some problem with your code:
1> Your derivation function is not correct. should be 2*a*x + b
2> I change the for loop to while loop with step is xc inputed from stdin
Here is my solution with your newest answer:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void explain();
void math(double a, double b, double c, double x, double xi, double *fx, double *fD, double *A);
int main()
{
double a, b, c;
double xi, xf, xc, x = 0;
double fx = 0, fD = 0, A = 0;
printf("SECOND DEGREE POLYNOMIAL CALCULATOR\n\n");
explain();
printf("\n\nEnter a value for a: ");
scanf("%lg", &a);
printf("Enter a value for b: ");
scanf("%lg", &b);
printf("Enter a value for c: ");
scanf("%lg", &c);
printf("\nYour function is %lgx^2%+-lgx%+-lg", a, b, c);
printf("\n\nEnter your initial x-value: ");
scanf("%lg", &xi);
printf("Enter your final x-value: ");
scanf("%lg", &xf);
printf("Enter what you would like to increment by: ");
scanf("%lg", &xc);
printf("| x | f(x) | f'(x) | A |\n"); //printing table
printf("----------------------------------------\n");
x = xi;
double nextX;
while (x <= xf) {
nextX = x + xc;
math(a, b, c, x, nextX, &fx, &fD, &A);
printf("| %.3lf | %.3lf | %.3lf | %.3lf |\n", x, fx, fD, A);
x = x + xc;
}
return 0;
}
void explain() {
printf("This program computes the integral and derivative of a user inputted second-degree polynomial (f(x)=ax^2+bx+c).\n");
printf("You will be asked to enter the 3 coefficients of your polynomial, followed by your initial x-value, your\n");
printf("final x-value, and the increment value between each x.");
}
void math(double a, double b, double c, double x, double xi, double *fx, double *fD, double *A) {
*fx = (a*(x*x)) + (b*x) + c; //finding y values
*fD = (2 * a * x) + b; //finding derivative values
*A = ((a / 3)*pow(x, 3) + (a / 2)*pow(x, 2) + c * x) - ((a / 3)*pow(xi, 3) + (a / 2)*pow(xi, 2) + c * xi); //finding integral values
return;
}
Related
Reference Image
The output always comes: 6.35 (I think it's a garbage value)
the code is
#include<stdio.h>
#include<math.h>
float formula(float,float);
int main()
{
float l=0,a=0;
printf("enter the length of the pendulum(l)\n");
scanf("%f",&l);
printf("Enter the angle of displacemnt(a)\n");
scanf("%f",&a);
printf("the length is %0.2f\n",l);
printf("the angle of displacemnt is %0.2f\n",a);
printf("the period of pendulum is %0.2f",formula(l,a));
return 0;
}
float formula(float l, float a)
{
float P=0,ran=0,g = 9.8;
ran = (l/g) * (1 + ((1/4)*(pow((float)(sin((double)a/2)),2))));
P = 2 * M_PI * ((float)sqrt((double)ran));
return P;
}
I don't know what is happening 😐
You can simplify this implementation by using doubles or floats exclusively. The 1/4 integer division yields 0. Here's an example:
#include <stdio.h>
#include <math.h>
float formula(float l, float a)
{
float ran = l / 9.8f * (1.0f + powf(sinf(a/2.0f), 2.0f) / 4.0f);
return 2.0f * (float) M_PI * sqrtf(ran);
}
int main(void)
{
float l;
float a;
printf("Enter the length of the pendulum(l): ");
scanf("%f", &l);
printf("Enter the angle of displacemnt(a): ");
scanf("%f", &a);
printf("The length is %0.2f\n", l);
printf("The angle of displacemnt is %0.2f\n", a);
printf("The period of pendulum is %0.2f\n", formula(l,a));
return 0;
}
//Enter a 4-digit integer n from the keyboard, and write a program to divide it into two 2-digit integers a and B. Calculate and output the results of the addition, subtraction, multiplication, division and redundancy operations of the split two numbers. For example, n=-4321, if the two integers after splitting are a and b, then a=-43 and b=-21. The result of division operation requires that it be precise to 2 decimal places, and the data type is float. Redundancy and division operations need to take into account the division of 0, that is, if the split B = 0, then output the prompt information "The second operator is zero!"
//Failure to pass the test,how should i fix
#include<stdio.h>
#include<math.h>
int main()
{
int x, a, b;
printf("Please input n:\n");
scanf("%d", &x);
a = x / 100;
b = x % 100;
printf("%d,%d\n", a, b);
printf("sum=%d,sub=%d,multi=%d\n", a + b, a - b, a*b);
if (b == 0)
printf("The second operater is zero!");
else
printf("dev=%.2f,mod=%d\n", (float)a / b, a%b);
}
You forgot to check that x is a 4-digits number. So if the input is 12345 or 123 you don't satisfy the requirement.
#include <stdio.h>
int main()
{
int x, a, b;
int passed = 0;
// Enter a 4 digits number: ABCD
do {
printf("Enter X = ");
scanf("%d", &x);
passed = (x >= 1000 && x <= 9999) || (x >= -9999 && x <= -1000);
} while (!passed);
a = x / 100;
b = x % 100;
printf("Numbers: %d %d \n", a, b);
printf("Sum = %d \n", a + b);
printf("Sub = %d \n", a - b);
printf("Mul = %d \n", a * b);
if (0 == b) {
printf("Div by Zero \n");
} else {
printf("Div = %f \n", (double)a / b);
printf("Mod = %d \n", a % b);
}
return 0;
}
I'm new to C language and coding and I encountered a question asking me to change the function header of:
float RealRoot_1(float a, float b, float c);
float RealRoot_2(float a,float b,float c);
to become:
void RealRoot_1(void);
void RealRoot_2(void);
I was told that it has something to do with Global Variables but I still couldn't figure it out after trying quite some time. Can anyone please explain on how to do it? Thanks a lot.
The source file is as below:
#include<stdio.h>
#include<math.h>
int main()
{
float RealRoot_1(float a, float b, float c); // Prototype declaration
float RealRoot_2(float a, float b, float c);
// Defining Input Variables
float x, y, z;
// Defining Output Variables
float Root_1, Root_2;
printf("Please enter the factor of X^2: ");
scanf("%f",&x);
printf("Please enter the factor of X: ");
scanf("%f",&y);
printf("Please enter the free factor: ");
scanf("%f",&z);
Root_1 = RealRoot_1(x,y,z);
Root_2 = RealRoot_2(x,y,z);
printf("the First Root is: %f \n", Root_1);
printf("the Second Root is: %f \n", Root_2);
system("pause");
}
float RealRoot_1(float a, float b, float c)
{
float x;
x = (-1*b + sqrt(pow(b,2) - 4 * a * c)) / (2 * a);
return x;
}
float RealRoot_2(float a, float b, float c)
{
float x;
x = (-1*b - sqrt(pow(b,2) - 4 * a * c)) / (2 * a);
return x;
}
This can be done by using global variables. You need to ensure that the variable names used in the function are the same as the ones used in the main code.
#include<stdio.h>
#include<math.h>
void RealRoot_1(void); // Prototype declaration
void RealRoot_2(void);
float x, y, z;
float Root_1, Root_2;
int main()
{
// Defining Output Variables
printf("Please enter the factor of X^2: ");
scanf("%f",&x);
printf("Please enter the factor of X: ");
scanf("%f",&y);
printf("Please enter the free factor: ");
scanf("%f",&z);
RealRoot_1();
RealRoot_2();
printf("the First Root is: %f \n", Root_1);
printf("the Second Root is: %f \n", Root_2);
system("pause");
}
void RealRoot_1(void)
{
Root_1 = (-1*y + sqrt(pow(y,2) - 4 * x * z)) / (2 * x);
}
void RealRoot_2(void)
{
Root_2 = (-1*y - sqrt(pow(y,2) - 4 * x * z)) / (2 * x);
}
Please note that this is a worse way of doing things than was given in the initial problem. In the initial exercise. You are loosing modularity and using too many globals is in general a bad idea.
You can also see Are global variables bad?
This should be self explanatory:
float RR_a, RR_b, RR_c;
float RR_d; // store result here(like a return value)
void RealRoot_1(void); // prototypes
void RealRoot_2(void);
void main(void)
{
printf("Please enter the factor of X^2: ");
scanf("%f",&RR_a);
printf("Please enter the factor of X: ");
scanf("%f",&RR_b);
printf("Please enter the free factor: ");
scanf("%f",&RR_c);
RealRoot_1();
printf("the First Root is: %f \n", RR_d);
RealRoot_2();
printf("the Second Root is: %f \n", RR_d);
system("pause");
}
void RealRoot_1(void)
{
float x;
x = (-1*RR_b + sqrt(pow(RR_b,2) - 4 * RR_a * RR_c)) / (2 * RR_a);
RR_d = x;
}
void RealRoot_2(void)
{
float x;
x = (-1*RR_b - sqrt(pow(RR_b,2) - 4 * RR_a * RR_c)) / (2 * RR_a);
RR_d = x;
}
Notice that after calling RealRoot_1 we now print the result before calling RealRoot_2. That's because the result of RealRoot_1 which is stored in RR_d is overwritten by RealRoot_2, thus it is lost.
You can circumvent this by declaring a second return variable, RR_d_2 and storing the result of RealRoot_2 in it.
We do not need duplicates for RR_a, RR_b or RR_c because their values are not modified within the functions.
This way of writing functions has limitations, which will be obvious when faced with recursion or multi-threading.
I have an assignment to compute the roots of quadratic equations in C, should be pretty simple and I know what I need to do with the program but I am having a problem nonetheless.
It works fine when the roots are imaginary and when the term inside the square root is zero.
But when I enter coefficients a, b and c which would give real roots it gives me the wrong answer, and I cant figure out whats wrong. (I am testing it with a=2, b = -5 and c=1)
This is my code, it compiles and runs, but gives the wrong answer.
#include<stdio.h>
#include<math.h>
int main()
{
float a, b, c, D, x, x1, x2, y, xi;
printf("Please enter a:\n");
scanf("%f", &a);
printf("Please enter b:\n");
scanf("%f",&b);
printf("Please enter c:\n");
scanf("%f", &c);
printf("The numbers you entered are: a = %f, b = %f, c = %f\n", a, b, c);
D = b*b-4.0*a*c;
printf("D = %f\n", D);
if(D > 0){
x1 = (-b + sqrt(D))/2*a;
x2 = ((-b) - sqrt(D))/2*a;
printf("The two real roots are x1=%fl and x2 = %fl\n", x1, x2);
}
if(D == 0){
x = (-b)/(2*a);
printf("There are two identical roots to this equation, the value of which is: %fl\n", x);
}
if (D<0){
y = sqrt(fabs(D))/(2*a);
xi = (-b)/(2*a);
printf("This equation has imaginary roots which are %fl +/- %fli, where i is the square root of -1.\n", xi, y);
}
return 0;
}
You don't calculate the result correctly:
x = y / 2*a
is actually parsed as
x = (y / 2) * a
so you have to put parentheses around 2*a.
you want this:
x = y / (2 * a)
#include <stdio.h>
float function (float x, float y);
float function2 (float x, float z);
float function3 (float y, float z);
float main()
{
float x;
float y;
float z;
{
printf("Please insert length of adjacent side");
scanf("%f", &x);
printf("Please insert length of opposite side");
scanf("%f", &y);
printf("Please insert length of the hypotenuse");
scanf("%f", &z);
}
{
if (z = 0){
printf("The length of the hypotenuse is %f", function (x, y));}
else if (y = 0){
printf("The length of the opposite side is %f", function2(x, z));}
else if (x=0){
printf("The length of the adjacent side is %f", function3(y, z));}
}
}
float function(float x, float y) {
return(sqrt(((x*x)+(y*y))));
}
float function2(float x, float z) {
return(sqrt(((z*z)-(x*x))));
}
float function3(float y, float z){
return(sqrt(((z*z)-(y*y))));
}
This is the code that I have to figure out the missing side of a right triangle. The input for the side that you do not know is 0. When I run the program it asks me for all the sides but then it does not go on and give me the answer...Could anyone please explain this?
Thanks
= is an assignment operator. Replace z = 0 and any others like it with z == 0
if (z == 0){ // = changed to ==
printf("The length of the hypotenuse is %f", function (x, y));}
else if (y == 0){ // = changed to ==
printf("The length of the opposite side is %f", function2(x, z));}
else if (x == 0){ // = changed to ==
printf("The length of the adjacent side is %f", function3(y, z));}
C Operators Reference Sheet