| | |
Quadratic Formula
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
•
•
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:
C Syntax (Toggle Plain Text)
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.
_________________________
||||||||||[][][]|||||||||||||||||||
|||||||||||||||____|
||||||||||
||||||||||
||||||||||[][][]|||||||||||||||||||
|||||||||||||||____|
||||||||||
||||||||||
i am still confused here is my program so far do you see any changes?
C Syntax (Toggle Plain Text)
#include <iostream> #include <stdio.h> #include "simpio.h" #include "strlib.h" #include "random.h" #include "math.h" using namespace std; int main(double d, double, x, double, y) { int a, b, c, x, y, d; printf("Enter A: "); a = GetInteger(); printf("Enter B: "); b = GetInteger(); printf("Enter C: "); c = GetInteger(); d = (b*b-4*a*c)^(1/2); x = ((-b - d)/(2.0*a)); y = ((-b + d)/(2.0*a)); if (x == y) printf("There is one solution: %f\n", x); else if (x != y) printf("There are two solutions: %f and %f\n", x, y); else if (d <= 0) printf("There are no solutions.\n"); else printf("There are no solutions.\n"); system("pause"); return 0; }
_________________________
||||||||||[][][]|||||||||||||||||||
|||||||||||||||____|
||||||||||
||||||||||
||||||||||[][][]|||||||||||||||||||
|||||||||||||||____|
||||||||||
||||||||||
•
•
Join Date: Jan 2008
Posts: 3,829
Reputation:
Solved Threads: 501
•
•
•
•
i am still confused here is my program so far do you see any changes?
C Syntax (Toggle Plain Text)
#include <iostream> #include <stdio.h> #include "simpio.h" #include "strlib.h" #include "random.h" #include "math.h" using namespace std; int main(double d, double, x, double, y) { int a, b, c, x, y, d; printf("Enter A: "); a = GetInteger(); printf("Enter B: "); b = GetInteger(); printf("Enter C: "); c = GetInteger(); d = (b*b-4*a*c)^(1/2); x = ((-b - d)/(2.0*a)); y = ((-b + d)/(2.0*a)); if (x == y) printf("There is one solution: %f\n", x); else if (x != y) printf("There are two solutions: %f and %f\n", x, y); else if (d <= 0) printf("There are no solutions.\n"); else printf("There are no solutions.\n"); system("pause"); return 0; }
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:
C Syntax (Toggle Plain Text)
#include <iostream> 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
C Syntax (Toggle Plain Text)
#include "math.h"
with
C Syntax (Toggle Plain Text)
#include <math.h>
I don't know what these files are, but I imagine GetInteger is in one of them.
C Syntax (Toggle Plain Text)
#include "simpio.h" #include "strlib.h" #include "random.h"
Keep your main function as:
C Syntax (Toggle Plain Text)
int main ()
not
C Syntax (Toggle Plain Text)
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.
C Syntax (Toggle Plain Text)
int a, b, c; 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.
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...
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
![]() |
Similar Threads
- The Vending Machine Game (Posting Games)
- Help explain how to write algorithm (Computer Science)
- Quadratic probing insert function help (C++)
- some simple problems... (C++)
- What now? (VB.NET)
- cmath coding (C++)
- Can anyone help me with my c language homework please? (C)
- representing a quadratic equation (C)
- Desperate help needed (C++)
Other Threads in the C Forum
- Previous Thread: help needed to compute maclaurin series using c
- Next Thread: error C2447: '{' : missing function header (old-style formal list?)
| Thread Tools | Search this Thread |
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string strings structures suggestions systemcall test testautomation unix urboc user voidmain() wab win32api windows.h






