0

I'm doing a course in C,where i was asked to find the roots of a quadratic equation.First i tried by hard-coding and it worked. Next i gave inputs using scanf(like a,b,c) it worked. But i failed in a scenario where the whole quadratic expression is taken as input i.e (ax^2+bx+c) and retrieve these a,b,c values from the expression. I spent a lot of time on this,searched online i couldn't find the answer so i am asking here for help.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

#define PI 3.14

int main(void)
{

puts("---- ROOTS ----");

char equ[20]; //Quadratic Expression in an Array 
float a,b,c,ope;
float root1,root2;

printf("please provide the expression :");
scanf("%d",&equ[20]);//Example : 5x^2+3x+1 as input

a == equ[0];//since ax^2+bx+c in the above expression a==5
b == equ[3];//b==3
c == equ[6];//c==1

ope = sqrt(b*b -4*a*c);
root1 = (-b + ope)/2*a;
root2 = (-b - ope)/2*a;

printf("The root 1 of the expression is : %d", root1); 
printf("\nThe root 2 of the expression is : %d", root2);

return EXIT_SUCCESS;
}

OUTPUT :

PS F:\Thousand C GO> gcc 3.c
PS F:\Thousand C GO> ./a
---- ROOTS ----
please provide the expression :5x^2+3x+1//edited
The root 1 of the expression is : 0
The root 2 of the expression is : 0  

I wanted to know if there is a way to solve this problem in C,if so how? if not why?.

Help is much appreciated. Thanks.

heeat
  • 128
  • 2
  • 9
  • Find the first `x` in the equation and parse the number before that. Then parse the number between the first `+` or `-` and the second `x` (including the `+` or `-` as the sign), then parse the number after the second `x`. You can use [`atoi`](https://en.cppreference.com/w/c/string/byte/atoi) to parse the numbers. – eesiraed Jan 27 '19 at 06:32
  • 1. Use `%s` for strings in `scanf`. `%d` is for ints. 2. `scanf` expects a pointer to the beginning of the char array. `&equ[20]` gives the address of the end of the string. Use `scanf("%d",equ);`. 3. `==` is the comparison operator. It's different from `=`, the assignment operator. 4. `equ[0]`, `equ[3]`, and `equ[6]` are characters, not numbers. Directing converting the character to a numerical value results in the encoding value of the character. Subtract `'0'` from the character to get the numerical value. Turn up your compiler warnings, it would have warned you about two of those bugs. – eesiraed Jan 27 '19 at 06:44
  • consider what happens if the user enters something as simple as "x^2 - 4x+4"? Notice how even this breaks your parsing model..what is `a`?. what if the user enters an equation with floating point coefficients? This problem is solvable in C, however it will require considerably more effort to get the parsing part correct. – thurizas Jan 27 '19 at 07:28
  • `#define PI 3.14` ==> `#define PI (acos(-1))` – pmg Jan 27 '19 at 09:05
  • @FeiXiang Thanks for helping out. – heeat Jan 27 '19 at 15:01

2 Answers2

0
  1. To read the string -

try

scanf("%s",equ);

or

scanf("%s",&equ[0]);
  1. == is the 'equal to' operator. You may want to change this to = (assignment operator)

  2. Recheck the index values that you are using to extract the coefficients in the quadratic equation.

  3. Coefficients are stored in ascii values in the character array. You need to convert them to appropriate integer or numeric value.

sri
  • 359
  • 2
  • 5
0

Hi here is the changed code :

scanf("%s",equ);//Example : 5x^2+3x+1 as input NOT

a = equ[0]-48;//since ax^2+bx+c in the above expression a==5
b = equ[5]-48;//b==3
c = equ[8]-48;//c==1
//printf("\n%f %f %f",a,b,c);

ope = sqrt(b*b -4*a*c);
printf("\n%f",ope);
root1 = (-b + ope)/(2*a);
root2 = (-b - ope)/(2*a);

printf("\nThe root 1 of the expression is : %f", root1); 
printf("\nThe root 2 of the expression is : %f", root2);

Now I will try to address the problems

  1. try to use the correct placeholder for inputting character array which is "%s" not "%d".
  2. don't give scanf("%d",&equ[20]) for scanning character array give scanf("%s",equ) only. here is the explanation.
  3. the index values are wrong correct them.
  4. the characters are stored as ASCII values. here is a link of all the ASCII values note that the ASCII value of 1 is 49, so while converting to float (implicit typecasting), convert to the appropriate value. In your case subtract 48.
  5. == is a comparison operator, change it to assignment operator =.
  6. put a bracket over (-b + ope)/(2*a), because your /2 evaluates first that *a is evaluated, this is operator precedence and associativity .
  7. now the major problem, Notice that I have changed the equation to 5x^2+3x+1, this is because in the case of 5x^2+3x+2 the value of b*b -4*a*c will be -31 which is negative and you can not store an imaginary number in a float type.here and here are some good reads for this topic.

It will take some time to grasp all this, but don't give up.

Happy Coding!

Edit: As mentioned in point 1 the right placeholder should be used for the corresponding datatype.

  1. In the last two lines, I have changed the %d to %f for printing root1 and root2 which are float and NOT int. here is a good read for very beginner on the same.
awadhesh14
  • 89
  • 7
  • 2
    "*characters are stored as ASCII values*" this is an assumption. To be independend of the character encoding us `'0'` instead of 48: `a = equ[0] - '0';` – alk Jan 27 '19 at 09:43
  • @alk hey thanks for the suggestion, adding the change. – awadhesh14 Jan 27 '19 at 11:22
  • @awadheshsingh i adjusted my program as you mentioned above.Thanks.But i still have a problem in the output. ---- ROOTS ---- 1 - Please provide the expression :2x^2+3x+1 2 - value you entered is 2x^2+3x+1 a: 50.000000 b: 51.000000 c: 49.000000 Ope value is : -1.#IND00 //b^2 -4ac The root 1 of the expression is : -1.#IND00 The root 2 of the expression is : -1.#IND00 //what are these – heeat Jan 27 '19 at 12:39
  • @heeat you seem to confuse the concept typecasting from char to float. I ran the code for the same input, here is the link https://ideone.com/tjHGKs – awadhesh14 Jan 27 '19 at 14:07
  • @heeat when I subtract '0' from equ[0], this means that I'm subtracting the ASCII value of the character '0'(zero) from the ASCII value of equ[0](here '2'). To differentiate between character zero('0') and Integer zero(0), we enclose the digit in single quote('0') for char. Example , char a = '0'; printf("ascii value of 0 is %d",a);printf(" the character is%c",a)//character 0 int b = 0; printf("the value of integer b is %d",b);//integer 0 – awadhesh14 Jan 27 '19 at 14:15
  • @heeat If it solves your problem, Please accept the answer by clicking the checkmark corresponding to my answer. You may ask more about it in comments. But I would appriciate accepting my answer if it solved the problem you asked. – awadhesh14 Jan 27 '19 at 14:22
  • @awadheshsingh Thanks for detailed explanation its solved my problem. – heeat Jan 27 '19 at 14:57