Can someone please explain it saying that
warning C4700: uninitialized local variable 'd' used?

Enhance the Quadratic program in the following manner. If the user enters a
valid set of coefficients (the else” case compute the value )of the discriminant d, defined as:

d = b^2 - 4ac

If the value of the discriminant is negative, inform the user that the roots are complex. If the
value is non-negative, inform the user the roots are not complex. Compile and run the program
multiple times. Verify the program acts as it should.

/////////////////////////////////////////////////////////////////////////////
//
// Name: Quadratic.cpp
// Author: Jeffrey A. Stone
// Course: CMPSC 101/121
// Purpose: Prompts the user for the three coefficients of a quadratic
// equation, and determines if the user has entered sufficient
// values to represent a quadratic equation.
//
/////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

/////////////////////////////////////////////////////////////////////////////
//
// FUNCTION NAME: main
// PARAMETERS: None
// RETURN TYPE: int
// PURPOSE: Entry point for the application.
//
/////////////////////////////////////////////////////////////////////////////
int main()
{
double a, b, c, d; // Initalizes and declares variables 
	std::cout << " Enter value of a: " <<endl; // Asks user for input of a....
	cin >>  a;
	std::cout << " Enter value of b: " <<endl; // Ask user for input of b....
	cin >>  b;
	std::cout << " Enter value of c: " <<endl; // Asks user for input of c....
	cin >>  c;
	
	if ( a == 0 )
	{
   
      // not a quadratic equation, display an error message... 
   cout << "ERROR: the first coefficient must not be zero." << endl;
	}
  
   else
   {	   
	   // a valid quadratic equation, display an error message... 
   cout << "The coefficients are valid." << endl;
	}

	if ( d < 1 )
	   {
		   d = ((pow (b,2)) - (4*a*c));
		   cout << "The determinant is: " << d << " and the roots are complex" << endl;
	}
	else
	{
		   cout << "The determinant is: " << d << " and the roots are not complex" << endl;
	}

		// exit the program with success (0 == success) 
   return 0; 
}

Edited 5 Years Ago by rbduck09: n/a

On line 47, you are using the value of 'd'. Tell me, what is the value of 'd' at line 47? Where did you initialise it with a value?

d is what is computed in the equation.. So would I have to give it a value before the if statement

At the moment in your code, you are checking to see if d is less than one, and then you are calculating what the value of d is. This makes no sense. How can you check the value of d before you calculate it? You must calculate d first, and then you can check its value.

Alright so I moved the equation before the if statement, but in the instructions given how do make so that if user doesn't enter valid coefficients the equation will not run because the way that I have it when it runs it tells the user that they inputted invalid coefficients but the equation is still computed

/////////////////////////////////////////////////////////////////////////////
//
// Name: Quadratic.cpp
// Author: Jeffrey A. Stone
// Course: CMPSC 101/121
// Purpose: Prompts the user for the three coefficients of a quadratic
// equation, and determines if the user has entered sufficient
// values to represent a quadratic equation.
//
/////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

/////////////////////////////////////////////////////////////////////////////
//
// FUNCTION NAME: main
// PARAMETERS: None
// RETURN TYPE: int
// PURPOSE: Entry point for the application.
//
/////////////////////////////////////////////////////////////////////////////
int main()
{
double a, b, c, d; // Initalizes and declares variables 
	std::cout << " Enter value of a: " <<endl; // Asks user for input of a....
	cin >>  a;
	std::cout << " Enter value of b: " <<endl; // Ask user for input of b....
	cin >>  b;
	std::cout << " Enter value of c: " <<endl; // Asks user for input of c....
	cin >>  c;
	
	if ( a == 0 )
	{
   
      // not a quadratic equation, display an error message... 
   cout << "ERROR: the first coefficient must not be zero." << endl;
	}
  
   else
   {	   
	   // a valid quadratic equation, display an error message... 
   cout << "The coefficients are valid." << endl;
	}

	d = ((pow (b,2)) - (4*a*c));

	if ( d < 0 )
	   {
		   cout << "The determinant is " << d << " and the roots are complex" << endl;
	}
	else 
	{
		 cout << "The determinant is " << d << " and the roots are not complex" << endl;
	}


		// exit the program with success (0 == success) 
   return 0; 
}

Move the } on line 45 to line 57. This will enclose the calculation in the 'else' part of your if statement. Indenting properly would show you why your problem is happening, most IDEs will do that for you.

Edited 5 Years Ago by Momerath: n/a

Okay that part of the program works I have another question.... Still using this piece of code. If the roots are not complex, compute the two roots of the quadratic equation using the formula

My question is how do I write an if statement to know if the roots are complex or not

Edited 5 Years Ago by rbduck09: n/a

Isn't that what this code is already doing?

if ( d < 0 )
{
  cout << "The determinant is " << d << " and the roots are complex" << endl;
}
else 
{
  cout << "The determinant is " << d << " and the roots are not complex" << endl;
}

Yes, but that is for the first equation. There is another part to the problem.

Enhance the Quadratic program in the following manner. If the roots are not
complex, compute the two roots of the quadratic equation using the formula:

x = (-b + sqrt(( pow (b,2) - 4*a*c )))/ 2*a

x = (-b - sqrt(( pow (b,2) - 4*a*c )))/ 2*a

Once the two roots are calculated, display them to the screen with a descriptive message. Note:
At this point you should go back and change the PURPOSE comment at the top of the file – you
have significantly changed what this program does!

My question is how do I write an if statement to know if the roots are complex or not

The laws of maths are the same in the second part. The way you decided if the roots were complex in the first part holds true for the second part as well.

if ( d < 0 )
{
  // roots are complex. Do not do the calculations.
}
else 
{
  // roots are not complex. Do the calculations.
}

Edited 5 Years Ago by Moschops: n/a

No thats not what I am talking about I have to add onto the code that I already have I have to see if roots are complex then I have to compute another equation
x = (-b + sqrt(( pow (b,2) - 4*a*c )))/ 2*a

x = (-b - sqrt(( pow (b,2) - 4*a*c )))/ 2*a

into the program

What I have to do is in the picture.... I have the #include cmath. Yes I need to do the first equation

d = ((pow (b,2)) - (4*a*c));

Then I have to find the roots for

x1 = (-b + sqrt(( pow (b,2) - 4*a*c )))/ 2*a;
x2= (-b + sqrt(( pow (b,2) - 4*a*c )))/ 2*a;

But what I'm asking is how to include the two equations for x1 and x2 with the if and else statements to whether the roots are complex or not

Attachments Capture.JPG 28.98 KB

You see in this code sample where it says "do the calculations"? Do the calculations there.

if ( d < 0 )
{
  // roots are complex. Do not do the calculations.
}
else 
{
  // roots are not complex. Do the calculations.
}

When I compile and run.. It doesn't output anything

int main()
{
double a, b, c, d, x1, x2; // Initalizes and declares variables 
	std::cout << " Enter value of a: " <<endl; // Asks user for input of a....
	cin >>  a;
	std::cout << " Enter value of b: " <<endl; // Ask user for input of b....
	cin >>  b;
	std::cout << " Enter value of c: " <<endl; // Asks user for input of c....
	cin >>  c;
	
	if ( a == 0 )
	{
   
      // not a quadratic equation, display an error message... 
   cout << "ERROR: the first coefficient must not be zero." << endl;
	}
  
   else
   {	   
	   // a valid quadratic equation, display an error message... 
   cout << "The coefficients are valid." << endl;
	
	d = ((pow (b,2)) - (4*a*c));// computes for the value of d

	if ( d < 0 )
	   {
		   cout << "The determinant is " << d << " and the roots are complex" << endl;
	}
	else 
	{
		 cout << "The determinant is " << d << " and the roots are not complex" << endl;
	}
	if ( d < 0 )
	{
		// roots are complex. Do not do the calculations.

	}
	else
	{
		//roots are not complex. 
		x1 = (-b + sqrt(( pow (b,2) - 4*a*c )))/ 2*a;
		x2= (-b + sqrt(( pow (b,2) - 4*a*c )))/ 2*a;
	}
	}




		// exit the program with success (0 == success) 
   return 0; 
}

Does the compilation succeed? The code as above will not compile as you've included no libraries.

I included the libraries and I see all the output I would expect to see from your code.

Edited 5 Years Ago by Moschops: n/a

I have the include libraries I just didnt post them with the code... But when I run the program it doesn't output the x1 and x2 equations it just gives back

if ( d < 0 )
	   {
		   cout << "The determinant is " << d << " and the roots are complex" << endl;
	}
	else 
	{
		 cout << "The determinant is " << d << " and the roots are not complex" << endl;
	}

Do I have to include a cout in the last if else statment?

Calculating a value is just that; calculation. You, the programmer, must make the decision about what to output to screen and do the actual programming to make that happen.

I understand what your saying but the code sample that you gave me is not very specific when I entered that into my code and compiled and ran. Everything works but it is not output the value of x1 and x2

It is not outputting the values of x1 and x2 because you have not put in any code that will output x1 and x2.

Here is some example code that will output x1:

cout << x1;

How did you write all the code in the first place if you can't figure out where to put code to output a value that has just been calculated?

Put it after you calculate x1, somewhere that x1 will always have been calculated.

Edited 5 Years Ago by Moschops: n/a

I Got It... Thanks for the help.

I Know how to code. It was when you replied a couple post back about something and I doing two different things at a time and I was all over the placed and got confused for a bit.. Thanks anyway though

This question has already been answered. Start a new discussion instead.