943,540 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1817
  • C RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jul 25th, 2008
0

Re: Quadratic Formula

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
plike922 is offline Offline
38 posts
since Jul 2008
Jul 25th, 2008
0

Re: Quadratic Formula

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.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Jul 25th, 2008
0

Re: Quadratic Formula

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. }
Reputation Points: 10
Solved Threads: 0
Light Poster
plike922 is offline Offline
38 posts
since Jul 2008
Jul 25th, 2008
1

Re: Quadratic Formula

Click to Expand / Collapse  Quote originally posted by plike922 ...
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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,371 posts
since Jan 2008
Jul 25th, 2008
0

Re: Quadratic Formula

Thanks for all the help... i am still learning and so glad i have other people than a program to help!
Reputation Points: 10
Solved Threads: 0
Light Poster
plike922 is offline Offline
38 posts
since Jul 2008
Jul 25th, 2008
0

Re: Quadratic Formula

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.
Reputation Points: 18
Solved Threads: 7
Junior Poster in Training
dmf1978 is offline Offline
51 posts
since Aug 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC