Learnem Educational
May 18, 2012, 12:59:55 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Quadratic Solver  (Read 1530 times)
Randy
Newbie
*
Posts: 11


View Profile WWW
« on: August 23, 2002, 01:16:50 PM »

Yesterday I was working on a quadratic-equation solver and here is my latest effort:

[code:3udekpou]#include <stdio.h>
#include <math.h>

main()
{
   float   quad_coeff, lin_coeff, const_coeff, radicand, radical,
   root1, root2;

   printf("\n                           ax ** 2 + bx +c = 0\n\n");
   do {
      printf("Please enter a non-zero coefficient for \"a\" :\n");
      scanf("%f", &quad_coeff);
   } while (quad_coeff == 0);
   printf("Please enter the coefficient for \"a\" :\n");
   scanf("%f", &lin_coeff);
   printf("Please enter the coefficient for \"a\" :\n");
   scanf("%f", &const_coeff);

   radicand = pow(lin_coeff, 2) - 4 * quad_coeff * const_coeff;
   radical = sqrt(radicand);
   root1 = (-lin_coeff + radical) / (2 * quad_coeff);
   root2 = (-lin_coeff - radical) / (2 * quad_coeff);

   if (radicand < 0) {
      printf("----------------------------------------------------\n");
      printf("x1 = Not real\n");
      printf("x2 = Not real\n");
      printf("----------------------------------------------------\n");
   } else {
      printf("----------------------------------------------------\n");
      printf("x1 = %f\n", root1);
      printf("x2 = %f\n", root2);
      printf("----------------------------------------------------\n");
   }

}
[/code:3udekpou]

I managed to get away from the problem of squaring a variable and taking roots with the pow(x,y) and sqrt(x) functions that I found in the math.h header file. The above seems to work OK. I will try to add a routine that will also solve for complex roots later and allow the user to run the program again without exiting. One note though: I was compiling the program on an HP-UX 10.20 server, and I had to add the -lm flag to the compile-time options like so:

cc -o quad quad.c -lm

According to the man pages, this is to include the math libraries in the linkage process. Without doing that, I got an "unsatisifed symbols" error. I also found that my system had a utility called "cb" or the "C Beautifier" which formats the code in a more readable format. [/code]
« Last Edit: January 01, 1970, 12:00:00 AM by Randy » Logged
Randy
Newbie
*
Posts: 11


View Profile WWW
« Reply #1 on: August 23, 2002, 04:08:24 PM »

Strangely enough, when I use "double" as my variable type, the answers do not seem to come out correctly.
« Last Edit: January 01, 1970, 12:00:00 AM by Randy » Logged
Mr. Cool
Newbie
*
Posts: 11


View Profile
« Reply #2 on: August 29, 2002, 11:30:52 PM »

[quote:3he6yfgc]Strangely enough, when I use "double" as my variable type, the answers do not seem to come out correctly.[/quote]
Please note that "double" does not mean that it doubles a number. "Double" or "lf" means that the number of places to the right of the decimal has increased. eg; an int may be 12 or 123, it has so many designated places BUT a double can be 123456789 up to 14 place holders I believe.  In other words use a double when your answer is like 23,000 but if its 1 or 123 use int as a declaration in you algorithm.

To square a number it would look like so...
 
int x;
y= x*x /*thats squared "the number times itself*/

Now pow(x,x) = means it raises a number like 10 to the power of some number

i hope this helps a bit better but if not please let me know.
 :D
« Last Edit: January 01, 1970, 12:00:00 AM by Mr. Cool » Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!