I am new to both this site and C programming and I am attempting to make a quadratic formula but I cannot get the roots to work. I believe I am not calling a function or perhaps there is something else wrong. Any help would be appreciated, thank you!
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
float userinput(char prompt[]); //Function Prototype
float root(float a, float b, float c);
int main()
{
float a,b,c;
a=userinput("Enter the value for a:"); //Function Call
b=userinput("Enter the value for b:");
c=userinput("Enter the value for c:");
printf("The Equation you entered is |n%fx^2%+fx%+f=0", a, b, c);
return 0;
}
float root(float a, float b, float c)
{
float D,x,x1,x2,x3,x4;
D = b*b - 4*a*c;
if(D>0)
{
printf("There are two real roots, the roots are: ");
x1 = ((-b+(sqrt(D)))/(2*a));
x2 = ((-b-(sqrt(D)))/(2*a));
printf("%.2f and %.2f,x1 , x2");
}
if(D==0)
{
printf("There is one real root, the root is: ");
x = ((-b)/(2*a));
printf("%.2f,x");
}
if(D<0)
{
printf("There are two imaginary roots. The roots are: ");
x3 = ((-b/2*a)+(sqrt(fabs(D))/(2*a)));
printf("%.2f,x3i and");
x4 = ((-b/2*a)-(sqrt(fabs(D))/(2*a)));
printf("%.2f,x4i");
}
}
float userinput(char prompt[]) //Function definition
{
float answer;
int status;
do
{
printf("%s",prompt);
status=scanf("%f", &answer);
if(status!=1)
{
fflush(stdin);
printf("INPUT ERROR!\n");
}
}
while(status!=1);
return answer;
}
You never call the function root() so it will never be executed. Put the function call somewhere before return 0; of main():
root(a, b, c);
Also, fix the printf() calls as mentioned above.
I'm not sure if this will fix everything but in your code you have your print statements formatted incorrectly. For example, this line:
printf("%.2f and %.2f,x1 , x2");
Should be printf("%.2f and %.2f", x1, x2);
The variables whose values you are trying to use should be outside of the quotation marks. Hopefully this helps.
You have several errors.
You never call root().
root is declared to return float, but it doesn't return anything, it just prints the roots. It should be declared void.
You have mistakes in many of your printf() calls. When you're displaying the equation, you have |n instead of \n, and %+f instead of +%f. When you're displaying the roots, you have the variables that you want to print inside the format string, instead of as separate arguments to printf.
Here's the corrected code.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
float userinput(char prompt[]); //Function Prototype
void root(float a, float b, float c);
int main()
{
float a,b,c;
a=userinput("Enter the value for a:"); //Function Call
b=userinput("Enter the value for b:");
c=userinput("Enter the value for c:");
printf("The Equation you entered is \n%fx^2+%fx+%f=0\n", a, b, c);
root(a, b, c);
return 0;
}
void root(float a, float b, float c)
{
float D,x,x1,x2,x3,x4;
D = b*b - 4*a*c;
if(D>0)
{
printf("There are two real roots, the roots are: ");
x1 = ((-b+(sqrt(D)))/(2*a));
x2 = ((-b-(sqrt(D)))/(2*a));
printf("%.2f and %.2f" ,x1 , x2);
}
if(D==0)
{
printf("There is one real root, the root is: ");
x = ((-b)/(2*a));
printf("%.2f",x);
}
if(D<0)
{
printf("There are two imaginary roots. The roots are: ");
x3 = ((-b/2*a)+(sqrt(fabs(D))/(2*a)));
printf("%.2fi and ", x3);
x4 = ((-b/2*a)-(sqrt(fabs(D))/(2*a)));
printf("%.2fi", x4);
}
}
float userinput(char prompt[]) //Function definition
{
float answer;
int status;
do
{
printf("%s",prompt);
status=scanf("%f", &answer);
if(status!=1)
{
fflush(stdin);
printf("INPUT ERROR!\n");
}
}
while(status!=1);
return answer;
}
DEMO
Related
I've searched the internet for my problem but it won't get solved.
I have the following function:
#include <stdio.h>
#include <math.h>
int solve(double a, double b, double c, double *x1, double *x2){
*x1=(-b+sqrt(pow(b,2)-(4*a*c)))/(2*a);
*x2=(-b-sqrt(pow(b,2)-(4*a*c)))/(2*a);
}
int main(void){
double a, b, c;
scanf("%lf", &a);
scanf("%lf", &b);
scanf("%lf", &c);
double x1, x2;
int count= solve(a, b, c, &x1, &x2);
if(!count){
printf("no solution");}
else if(count==1){
printf("one solution x: %lf", x1);}
else if(count>1){
printf("two solutions x1: %lf x2: %lf", x1, x2);}
}
the program should return both values from the function solve but everytime I start the program I have a warning that says "missing return value". Where is my fault?
By the way struct is not an option cause of restrictions from my professor and it need to be all done in one function.
So, there's some misunderstanding here.
From what I can understand from the template code, your solve function is supposed to be returning the number of (real) solutions for a quadratic equation, and there's currently no logic for that.
You should implement logic that returns the number of solutions based on the input.
For a quadratic equation, you look at the discriminant. This is b^2 - 4*a*c.
If it is less than 0, there are no solutions, one solution if it's equal to 0, and two solutions otherwise.
Additionally, since there is no (real) square root of a negative number, you should not even run the calculations if the discriminant is less than 0.
If you run your function with a*c > b^2, x1 and x2 would be Not a Number.
You are performing the "return" of values using pointers correctly.
However, since your function is defined as int solve, it is expecting you use return <some value>; inside the function.
In fact, theoretically int main should be breaking it as well because you don't return anything (usually just return 0), but that's a special exception in the language to drop it for main.
#include <stdio.h>
#include <math.h>
int solve(double a, double b, double c, double *x1, double *x2){
if((pow(b,2)-(4*a*c)>=0)){
*x1=(-b+sqrt(pow(b,2)-(4*a*c)))/(2*a);
if(sqrt(pow(b,2)-(4*a*c)>0)){
*x2=(-b-sqrt(pow(b,2)-(4*a*c)))/(2*a);
return 2;
}
return 1;
}
else{
printf("it is imaginary term, ");
}
return 0;
}
int main(void){
double a, b, c;
scanf("%lf", &a);
scanf("%lf", &b);
scanf("%lf", &c);
double x1, x2;
int count= solve(a, b, c, &x1, &x2);
if(!count){
printf("no real solution");}
else if(count==1){
printf("one solution x: %lf", x1);}
else if(count>1){
printf("two solutions x1: %lf x2: %lf", x1, x2);}
}
You have to design if condition in solve function so it will calculate the x2 value. And if you want to reduce size you can also use char data type for count variable.
You should return an int value in solve function, as your functions signature is: int solve(double a, double b, double c, double *x1, double *x2),so just add "return 0;" at the end of solve function.
While analyzing can't seem to avoid the value stored to 'delta' not being read... what part of my loop isn't working and why?
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c;
float delta;
printf("a=");
scanf("%f", &a);
printf("b=");
scanf("%f", &b);
printf("c=");
scanf("%f", &c);
float x, x1, x2;
delta=((b*b)-(4*a*c));
if("delta==0")
{
x=((-b)/(2*a));
printf("x=%f \n", x);
}
else if("delta>0")
{
x1=((-b+sqrt(delta))/(2*a));
x2=((-b-sqrt(delta))/(2*a));
printf("x1=%f i x2=%f \n", x1, x2);
}
else printf("Nie ma w zbiorze liczb rzeczywistych rozwiazania tego rownania.\n");
return 0;
}
1st thing, if("delta==0") inside "" whatever is present that will be treated as a address so if(address) means true, remove the double quotation.
if(delta==0)
// some code
else if (delta > 0)
// some code
2nd thing, you are comparing(==) a float & an integer, so be aware of behaviour of comparing different operands.
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.
Ok so I have written this code with four different functions, and the main purpose of it is to display in a table from angles 0-90 what the angle, time, distance of a velocity is. the velocity is inputed from the user.
But when I call the void function that is making the function I get an error "undefined reference to `create_table'" Here is my code.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define G 9.8 /* gravitation acceleration 9.8 m/s^2 */
#define PI 3.141592654
void create_table(double v);
double Projectile_travel_time(double a, double v);
double Projectile_travel_distance(double a, double v);
double degree_to_radian(double d);
int main(void)
{
int n;
double velocity;
printf ("please enter the velocity at which the projectile is launched (m/sec): ");
n = scanf("%lf" ,&velocity);
if(n != 1)
{
printf("Invlid input. Bye...");
exit(1);
}
while (velocity < 0 )
{
printf ("please enter a positive number for velocity: ");
n = scanf("%lf", &velocity);
if(n != 1)
{
printf("Invlid input. Bye...");
exit(1);
}
}
create_table(velocity);
return 0;
}
void create_table(double v)
{
printf("Angle t d\n");
printf("(deg) (sec) (m)\n");
double a,i;
for( a=0; a<=90; a+=5)
{
for(i=0; i<=2; i++)
{
double t = Projectile_travel_time(a, v);
double s = Projectile_travel_distance(a, v);
printf("%d %d %d\n", a, t, s);
}
}
}
double Projectile_travel_time(double a, double v)
{
double t = ((2*v*sin(degree_to_radian(a)))/(G));
return t;
}
double Projectile_travel_distance(double a, double v)
{
double d = ((v*v)/G)*sin(2*degree_to_radian(a));
return d;
}
double degree_to_radian(double d)
{
double r = d*atan(1) * 4 / 180;
return r;
}
any help would be appreciated.
thanks
edit I have edited the code but now have encountered another problem with my outputs being completely off. Any suggestions how my functions are incorrect?
You must keep the functions you create outside of the main function
Try to implement your functions outside the main()
You need to move the function definitions out of main. C does not support nested functions.
Edit: That is, in GCC they are, but it's not portable.
I have edited the code but now have encountered another problem with my outputs being completely off.
Change
printf("%d %d %d\n", a, t, s);
to
printf("%lf %lf %lf\n", a, t, s);
You can use %7.3f to align all the values.
Write all the function definitions create_table, Projectile_travel_time, Projectile_travel_distance and degree_to_radian outside the main.
Linker is not able to find the definition of create_table at the point at which you are calling create_table.
I am working in c after a long time.Here i have to achieve three functionality which includes
get a number and show half
2.Get the square of the number
3.Get two number and show their summation and sabtraction.
I am using devC++ and when i compile the code i get the error i mentioned in the title which conflict type if squareInput.What is wrong here:
#include<stdio.h>
#include<conio.h>
int main(){
float x;
printf("enter a number\n");
scanf("%f",&x);
//TASK 1 : display half of the number
pirntf("half of x is = %.3f",x);
//TASK 2 : square of number
squareInput(x); //call square function from here
// TASK 3 : get two numbers and display both summation and sabtraction
float num1,num2; // declare two floating number( floating numbers can hold decimal point numbers
printf("enter num1 \n");
scanf("num1 is =%f",&num1);
printf("enter num2 \n");
scanf("num2 is =%f",num2);
calculate(num1,num2);// call calculate function
getch();
}
float squareInput(float input){
float square=input*input;
printf("\n square of the number is %.3f \n",square);
return 0;
}
float calculate(float num1,float num2){
//summation
float summation= num1+num2; // declare antoher variable called summation to hold the sum
//sabtraction
float sabtraction=num1-num2;
printf("summation is %.2f \n",summation);
printf("sabtraction is %.2f \n",sabtraction);
return 0;
}
Things will go wrong without prototypes. Add
float squareInput(float input);
float calculate(float num1,float num2);
in front of int main().
If you don't declare a function before it's called, the compiler assumes it as a int-returning function. However, squareInput() return float, so the compiler(or linker, maybe) complains to you.
Also note that definitions are declarations(but not vice versa, obviously), so moving the definitions of squareInput() and calculate() in front of where they are called works too.
At the time you call squareInput and calculate, they haven't been defined yet. So C assumes an implicit declaration of int squareInput() and int calculate(). These implicit declarations conflict with the definitions of these functions.
You can fix this by either adding declarations for each of these functions before main:
float squareInput(float input);
float calculate(float num1,float num2);
Or by simply moving the functions in their entirety before main.
Be sure to add prototypes when you use a function. That way you do not need to worry too much about the order in which you call them.
Also try to separate your problems into smaller bits if you can. A comment like TAKS1 shows you that you actually want a function with that name.
#include <stdio.h>
//prototypes
void AskUserForOneNumer(float * number, const char * question );
void TASK_1(float x);
void TASK_2(float x);
void TASK_3(float a, float b);
int main()
{
float x, a, b;
AskUserForOneNumer(&x, "enter x");
AskUserForOneNumer(&a, "enter a");
AskUserForOneNumer(&b, "enter b");
TASK_1(x);
TASK_2(x);
TASK_3(a, b);
}
void TASK_1(float x)
{
printf("x = %g\n", x);
printf("0.5 * x = %g\n", 0.5 * x);
}
void TASK_2(float x)
{
printf("x = %g\n", x);
printf("x * x = %g\n", x * x);
}
void TASK_3(float a, float b)
{
printf("a = %g\n", a);
printf("b = %g\n", b);
printf("a + b = %g\n", a + b);
printf("a - b = %g\n", a - b);
}
void AskUserForOneNumer(float * number, const char * question)
{
float x;
printf("%s\n", question);
scanf("%f", &x);
printf("your input was %g\n", x);
*number = x;
}