-2

This is problem: Write a C program that reads the coefficients of a quadratic equation and prints its roots with two decimal points.

My code:

#include<stdio.h>
#include<math.h>
int main() {
   float a,b,c;
   float root1;
   float root2;
   root1=(-b+sqrt(b*b-4*a*c))/2*a;
   root2=(-b-sqrt(b*b-4*a*c))/2*a;
   scanf("%f %f %f",a,b,c);
   printf("%f +f",root1,root2);

   return 0; 
}

These are the errors:

/tmp/ccYLQQBg.o: In function main': hello.c:(.text+0x61): undefined reference tosqrt' hello.c:(.text+0xc9): undefined reference to `sqrt' collect2: error: ld returned 1 exit status

How can I fix that? What is the problem with my code? Thanks :) the new code :

#include<stdio.h>
#include<math.h>
int main(){
   float a,b,c;
   float root1=(-b-sqrt(b*b-4*a*c))/(2*a);
   float root2=(sqrt(b*b-4*a*c))/(2*a);
   scanf("%f %f %f",&a,&b,&c);
   printf("%f %f\n",root1,root2);
   return 0; 
}

but still there are errors. the outputs are always -nan and nan if ı took out the parantesis around 2*a the outputs are -0 and 0 regardles of values of coeffiencts.

Tormund Giantsbane
  • 355
  • 1
  • 4
  • 12
  • 1
    There is probably a linker option for `math.h`. You have errors in `scanf` too which should take the address of each variable, and, please check that `(b*b-4*a*c)` is non-negative before calling `sqrt`, and, parentheses are needed around `2*a`. And, you need to enter `a,b,c` *before* computing with them. – Weather Vane Mar 02 '18 at 09:12
  • 1
    Don't forget to check b*b-4*a*c before calling `sqrt`. – Bathsheba Mar 02 '18 at 09:19
  • thank for your answer.I did what you say now and ı get -nan and nan ı know every input is suitable that b*b -4*a*c wont be negative so there is no problem but still the code doesnt work as it should . – no one ever Mar 02 '18 at 09:29
  • 1
    Then please read my comment, and fix everything I mentioned. There are many mistakes. – Weather Vane Mar 02 '18 at 09:30
  • ı added the new code according to your comment . but there is still mistakes . thanks for your interest. – no one ever Mar 02 '18 at 09:35
  • 1
    No, you missed the most important, which is to enter `a,b,c` *before* computing the roots. C does not work from a set of rules you supply - it computes the roots once, immediately. – Weather Vane Mar 02 '18 at 09:36
  • 1
    Compile with all warnings and debug info (e.g. `gcc -Wall -Wextra -g`) and [use the debugger](https://sourceware.org/gdb/current/onlinedocs/gdb/) – Basile Starynkevitch Mar 02 '18 at 09:48
  • Yeah, C!=Excel. Like the underlying processor hardware, execution proceeds forwards from one line/instruction to the next unless a flow-of-control instruction, (or interrupt), causes a jump/call. – Martin James Mar 02 '18 at 10:32

2 Answers2

3

You need to link with the math lib, e.g.

gcc -o myprog myprog.c -lm
Flopp
  • 1,887
  • 14
  • 24
  • 2
    Exactly. See also https://stackoverflow.com/questions/1033898/why-do-you-have-to-link-the-math-library-in-c – MemAllox Mar 02 '18 at 09:15
  • Thanks for your quick answer but the output is -0.000000 0.000000 regardless of value of coefficients . ı think there must be a mistake inside the code . thank really. – no one ever Mar 02 '18 at 09:21
  • It's always a good idea to compile with full warnings (and to take them seriously): `gcc -Wall -Wextra ...` – Flopp Mar 02 '18 at 09:28
0

the outputs are always -nan and nan

scanf("%f %f %f",&a,&b,&c);

is placed too late in the code. Get the values for a,b,c before the calculations:

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

int main(void){
    float a,b,c;
    float root1, root2;

    scanf("%f %f %f",&a,&b,&c);

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

    printf("%8.1f %8.1f\n",root1,root2);
    return 0;
}

The above formulas work correctly: For input:

3 -9 6

Output is:

2.0 1.0
sg7
  • 6,108
  • 2
  • 32
  • 40
  • thanks for your answer ı had written the same code already , and it worked in my computer but when ı tried to embedd the code here(https://nihil.ceng.metu.edu.tr/opc/pcourse/11/3/) ,the bottom of the page , there is still a error can you explain ? – no one ever Mar 03 '18 at 02:32
  • @OgünAydın Sure: 1) ISO C90 forbids mixed declarations and code [-pedantic] - I moved declaration to the top. 2) Test program expect special formatting for output: 1 decimals after the . That can be done via %8.1f. 3) Program wants bigger value first! Now, updated program can pass! – sg7 Mar 03 '18 at 03:02
  • @OgünAydın If my help was useful, you can accept my answer. – sg7 Mar 03 '18 at 03:07
  • thanks your help is useful , ı am gonna accept your answer :) – no one ever Mar 04 '18 at 14:29
  • also sorry for not accepting your answer before, ı am a newbite at programming and also a new user . – no one ever Mar 04 '18 at 14:31
  • @OgünAydın Thanks! I am glad that I could help. – sg7 Mar 04 '18 at 14:46