GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Nov-2009, 15:42
sl02ggp sl02ggp is offline
New Member
 
Join Date: Oct 2009
Posts: 25
sl02ggp is on a distinguished road
Thumbs up

Quadratic Equation Program


I have my program below (it runs and compiles as is) , but having difficulty implement these conditions and outputs:

conditions:
1. rejecting certain coefficients if the discriminat (B^(2) -4AC) <0.
2. also having no division by zero in my program (do i use setPrecision)?

outputs:
having at least 15 spaces or so for each of the root ouput and outputting the roots in the following format:
-right justified, four decimal places accuracy, fixed point format
-right justified, two decimal places accuracy, floating point (scientific) format
- left justified, four decimal places accuracy, fixed point format
- left justified, two decimal places accuracy, floating point (scientific) format

CPP / C++ / C Code:

// Quadratic Equation.cpp : main project file.
#include<stdio.h>
#include<math.h>

int main()
{
      int A, B, C;
      double disc, deno, x1, x2;
      printf("\n\n\t ENTER THE VALUES OF A,B,C...");
      scanf("%d,%d,%d", &A, &B, &C);
      disc = (B * B) - (4 * A * C);
      deno = 2 * A;
      if(disc > 0)
      {
             printf("\n\t THE ROOTS ARE REAL ROOTS");
             x1 = (-B/deno) + (sqrt(disc) / deno);
             x2 = (-B/deno) - (sqrt(disc) / deno);
             printf("\n\n\t THE ROOTS ARE...: %f and %f\n", x1, x2);
      }
      else if(disc == 0)
      {
             printf("\n\t THE ROOTS ARE REPEATED ROOTS");
             x1 = -B/deno;
             printf("\n\n\t THE ROOT IS...: %f\n", x1);
      }
      else
             printf("\n\t THE ROOTS ARE IMAGINARY ROOTS\n");
}

Last edited by LuciWiz : 02-Nov-2009 at 15:46. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 02-Nov-2009, 16:07
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Quadratic Equation Program


Quote:
Originally Posted by sl02ggp
...
-right justified, four decimal places accuracy, fixed point format
-right justified, two decimal places accuracy, floating point (scientific) format
- left justified, four decimal places accuracy, fixed point format
- left justified, two decimal places accuracy, floating point (scientific) format
...
printf() can do all of these things.
  #3  
Old 02-Nov-2009, 16:09
sl02ggp sl02ggp is offline
New Member
 
Join Date: Oct 2009
Posts: 25
sl02ggp is on a distinguished road

Re: Quadratic Equation Program


lol...yes i know..... but can you be more specific please :]

also how do i implement the conditions too! actually want to use cout and cin instead..how would i implement this into my code.


Thank You!
  #4  
Old 02-Nov-2009, 16:31
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Quadratic Equation Program


Look at this example for using fixed point or scientific notation. Look at this example for justifying right or left.

As for implementing the conditions, what is the question/problem?
  #5  
Old 02-Nov-2009, 16:37
sl02ggp sl02ggp is offline
New Member
 
Join Date: Oct 2009
Posts: 25
sl02ggp is on a distinguished road

Re: Quadratic Equation Program


here is the full problem that im trying to do:
trying to make a a program that finds the roots of a quadratic equation. (form: Ax^(2) + Bx + C = 0). Going to prompt the user to enter the coefficients A, B, and C. Also the program can only handle real roots, thus like i said before reject the coefficients if the discriminant (B^(2)-4AC)<0. Also, trying to make the program treat the quadratic equation as a special case, a linear equation, when A is zero. So...like i said before...no division by zero will occur.
  #6  
Old 02-Nov-2009, 16:39
sl02ggp sl02ggp is offline
New Member
 
Join Date: Oct 2009
Posts: 25
sl02ggp is on a distinguished road

Re: Quadratic Equation Program


still kind of confused after reading over the two links you gave me

changed my code:

CPP / C++ / C Code:

// Quadratic Equation.cpp : main project file.
#include <cmath>  // defines the sqrt() function
#include <iostream>
using namespace std;

int main()
{ // implements the quadratic formula
  float a, b, c;
 
  cout << "Enter the coefficients of a quadratic equation:" << endl;
  cout << "a: ";
  cin >> a;
  cout << "b: ";
  cin >> b;
  cout << "c: ";
  cin >> c;

  cout << "The roots of " << a << "*x*x + " << b
       << "*x + " << c << " = 0" << endl;
  
  float d = b*b - 4*a*c;  // discriminant
  float sqrtd = sqrt(d);
  float x1 = (-b + sqrtd)/(2*a);
  float x2 = (-b - sqrtd)/(2*a);
  
  cout << "are:" << endl;
  cout << "x1 = " << x1 << endl;
  cout << "x2 = " << x2 << endl;

}


  #7  
Old 02-Nov-2009, 17:39
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Quadratic Equation Program


CPP / C++ / C Code:
float number = 1.23;

// output in fixed-point notation
cout << fixed << number; 

// output in scientific notation
cout << scientific << number;
  #8  
Old 02-Nov-2009, 21:24
sl02ggp sl02ggp is offline
New Member
 
Join Date: Oct 2009
Posts: 25
sl02ggp is on a distinguished road

Re: Quadratic Equation Program


here is my updated code with the justification, scientific/fixed, and spaces

still don't know how to set my code: where it rejects coefficients if the discriminant (B^(2)-4AC) <0. Also having no division of zero in my program (do i use setPrecision) and how ?

CPP / C++ / C Code:

// Quadratic Equation.cpp : main project file.
#include <cmath>  // defines the sqrt() function
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{ // implements the quadratic formula
  float a, b, c;
 
  cout << "Enter the coefficients of a quadratic equation:" << endl;
  cout << "a: ";
  cin >> a;
  cout << "b: ";
  cin >> b;
  cout << "c: ";
  cin >> c;

  cout << "The roots of " << a << "*x*x + " << b
	   << "*x + " << c << " = 0" << " are:" <<endl;
  
  float d = b*b - 4*a*c;  // discriminant
  float sqrtd = sqrt(d);
  float x1 = (-b + sqrtd)/(2*a);
  float x2 = (-b - sqrtd)/(2*a);
   
  cout.precision(4);
  cout << "x1 = " << endl;
  cout.width(12); cout << right << fixed << x1 << endl;
	
  cout.precision(4);
  cout << "x2 = " << endl;
  cout.width(12); cout << right << fixed << x2 << endl;

  cout << setw (15)<< " "<<endl;

  cout.precision(2);
  cout << "x1 = " << endl;
  cout.width(12); cout << right << scientific << x1 << endl;
	
  cout.precision(2);
  cout << "x2 = " << endl;
  cout.width(12); cout << right << scientific << x2 << endl;

  cout << setw (15)<< " "<<endl;

  cout.precision(4);
  cout << "x1 = " << endl;
  cout.width(12); cout << left << fixed << x1 << endl;
	
  cout.precision(4);
  cout << "x2 = " << endl;
  cout.width(12); cout << left << fixed << x2 << endl;

  cout << setw (15)<< " "<<endl;
  
  cout.precision(2);
  cout << "x1 = " << endl;
  cout.width(12); cout << left << scientific << x1 << endl;
	
  cout.precision(2);
  cout << "x2 = " << endl;
  cout.width(12); cout << left << scientific << x2 << endl;

}



  #9  
Old 03-Nov-2009, 08:43
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Quadratic Equation Program


Since you know what you are going to divide by, simply check that it is not zero after you have collected the user input. For instance:
CPP / C++ / C Code:
cout << "Enter the coefficients of a quadratic equation:" << endl;
cout << "a: ";
cin >> a;
cout << "b: ";
cin >> b;
cout << "c: ";
cin >> c;

// Make sure we are not trying to divide by zero
if(a == 0.0)
{
  // Show error message and exit....or handle it however you wish
}

float d = b*b - 4*a*c;  // discriminant

// Makes sure the discriminant is not less than zero
if(d < 0.0)
{
  // Show error message and exit....or handle it however you wish
}

...
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 11:13
BOOKEEPING program, HELP!! yabud C Programming Language 10 17-Nov-2006 04:48
Help with a complex program lordfuoco C++ Forum 5 24-Jun-2006 07:03
::Need help Please:: using If & Switch in Quadratic equation. internetbug C Programming Language 6 07-Apr-2005 18:36

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:06.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.