Quadratic Formula

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2008
Posts: 38
Reputation: plike922 is an unknown quantity at this point 
Solved Threads: 0
plike922's Avatar
plike922 plike922 is offline Offline
Light Poster

Re: Quadratic Formula

 
0
  #11
Jul 25th, 2008
Originally Posted by VernonDozier View Post
iostream isn't in C. I think you have quotes where you want <> in your #include for math.h, and you can't use "using namespace std" in C. Maybe you're not using a C compiler.

The problem with the decimals is that you've declared all of your variables as integers and integers can't hold decimals. Try declaring x, y, d as doubles or floats.

I'm also suspicious of this line:

  1. d = (b*b-4*a*c)^(1/2);

^ is the bitwise XOR operator and I think (1 / 2) is going to be 0 due to integer division.
How do you do that?... Sorry i am kind of new this is my 3rd week in the course i am taking.
_________________________
||||||||||[][][]|||||||||||||||||||
|||||||||||||||____|
||||||||||
||||||||||
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Quadratic Formula

 
0
  #12
Jul 25th, 2008
I just meant to say he's trying to create a C program I guess, using a C++ compiler since only a C++ compiler will throw that error. Nothing more.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 38
Reputation: plike922 is an unknown quantity at this point 
Solved Threads: 0
plike922's Avatar
plike922 plike922 is offline Offline
Light Poster

Re: Quadratic Formula

 
0
  #13
Jul 25th, 2008
i am still confused here is my program so far do you see any changes?

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include "simpio.h"
  4. #include "strlib.h"
  5. #include "random.h"
  6. #include "math.h"
  7. using namespace std;
  8.  
  9. int main(double d, double, x, double, y)
  10. {
  11. int a, b, c, x, y, d;
  12.  
  13. printf("Enter A: ");
  14. a = GetInteger();
  15.  
  16. printf("Enter B: ");
  17. b = GetInteger();
  18.  
  19. printf("Enter C: ");
  20. c = GetInteger();
  21.  
  22. d = (b*b-4*a*c)^(1/2);
  23. x = ((-b - d)/(2.0*a));
  24. y = ((-b + d)/(2.0*a));
  25.  
  26.  
  27. if (x == y)
  28. printf("There is one solution: %f\n", x);
  29. else if (x != y)
  30. printf("There are two solutions: %f and %f\n", x, y);
  31. else if (d <= 0)
  32. printf("There are no solutions.\n");
  33. else
  34. printf("There are no solutions.\n");
  35.  
  36. system("pause");
  37. return 0;
  38. }
_________________________
||||||||||[][][]|||||||||||||||||||
|||||||||||||||____|
||||||||||
||||||||||
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,829
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Quadratic Formula

 
1
  #14
Jul 25th, 2008
Originally Posted by plike922 View Post
i am still confused here is my program so far do you see any changes?

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include "simpio.h"
  4. #include "strlib.h"
  5. #include "random.h"
  6. #include "math.h"
  7. using namespace std;
  8.  
  9. int main(double d, double, x, double, y)
  10. {
  11. int a, b, c, x, y, d;
  12.  
  13. printf("Enter A: ");
  14. a = GetInteger();
  15.  
  16. printf("Enter B: ");
  17. b = GetInteger();
  18.  
  19. printf("Enter C: ");
  20. c = GetInteger();
  21.  
  22. d = (b*b-4*a*c)^(1/2);
  23. x = ((-b - d)/(2.0*a));
  24. y = ((-b + d)/(2.0*a));
  25.  
  26.  
  27. if (x == y)
  28. printf("There is one solution: %f\n", x);
  29. else if (x != y)
  30. printf("There are two solutions: %f and %f\n", x, y);
  31. else if (d <= 0)
  32. printf("There are no solutions.\n");
  33. else
  34. printf("There are no solutions.\n");
  35.  
  36. system("pause");
  37. return 0;
  38. }

First, make sure you are using a C compiler if this is a C program and make sure the extension at the end of the file is .c, not .cpp. If this is a C++ program, you're in the wrong forum. Assuming it is a C program and you are using a C compiler, this shouldn't compile due to these lines:

  1. #include <iostream>
  2. using namespace std;

You can't use those in C and your program doesn't need them anyway, so you can delete them. Also, you need to replace

  1. #include "math.h"

with

  1. #include <math.h>

I don't know what these files are, but I imagine GetInteger is in one of them.

  1. #include "simpio.h"
  2. #include "strlib.h"
  3. #include "random.h"

Keep your main function as:

  1. int main ()

not

  1. int main(double d, double, x, double, y)

Declare d, x, and y as doubles inside the program, where you are already declaring them as integers.

  1. int a, b, c;
  2. double x, y, d;

^ is not the exponential operator. Use pow or sqrt from math.h instead:

http://www.cplusplus.com/reference/c...cmath/pow.html
http://www.cplusplus.com/reference/c...math/sqrt.html

Keep in mind that 1 / 2 = 0 by integer division.
1.0 / 2.0 = 0.5, on the other hand, which is what you would want if you use the pow function.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 38
Reputation: plike922 is an unknown quantity at this point 
Solved Threads: 0
plike922's Avatar
plike922 plike922 is offline Offline
Light Poster

Re: Quadratic Formula

 
0
  #15
Jul 25th, 2008
Thanks for all the help... i am still learning and so glad i have other people than a program to help!
_________________________
||||||||||[][][]|||||||||||||||||||
|||||||||||||||____|
||||||||||
||||||||||
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 51
Reputation: dmf1978 is an unknown quantity at this point 
Solved Threads: 7
dmf1978's Avatar
dmf1978 dmf1978 is offline Offline
Junior Poster in Training

Re: Quadratic Formula

 
0
  #16
Jul 25th, 2008
The only thing that don't make me feel good having read this thread is the sign of plike:

COBOL was designed so that managers could read code.
BASIC was designed for people who are not programmers.
FORTRAN is for scientists.
PILOT is for teachers.
C, however, is for programmers.


Because the ^ operator is used in Basic...

Last edited by dmf1978; Jul 25th, 2008 at 6:26 pm.
-- Martín
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC