7 Replies - 1190 Views - Last Post: 06 September 2010 - 06:51 AM Rate Topic: -----

#1 buskus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-September 10

Debugging - Variables Have Garbage Values - How Fix?

Posted 05 September 2010 - 09:59 PM

I'm trying to debug this program but have relatively little experience with C++ and debugging. The code (not written very efficiently, I know) should print out a matrix and its inverse (if it has one) for two given data sets. It prints out the matrices, but zero values for the inverse. While debugging, the values for the determinant and the inverse matrices are garbage which causes the boolean function to note that the matrix has a determinant. Is the problem with the definition or declaration of the det function or the passing of the determ variable? Thanks for your help!

#include <iostream>
#include <iomanip>

using namespace std;

class Matrix
{
private:
	double value1;						// Matrix values
	double value2;
	double value3;
	double value4;
	double valIn1;
	double valIn2;
	double valIn3;
	double valIn4;
public:
	double determ;
	bool sing;
	Matrix (double = 1, double = 2, double = 3, double = 4);				// Constructor with defaults
	Matrix (const Matrix&);													// Copy Contributor
	double det ();							// Member function to calculate the determinant
	bool isSingular ();												// Member function to determine if the matrix is singular
	void inverse ();							// Member function to create inverse matrix
	void print_em ();				// Member function to print the matrix and its inverse
};

// Code for member functions defined outside the class
Matrix::Matrix (double v1, double v2, double v3, double v4)					// Copy constructor
	   {
		   value1 = v1;
	   	   value2 = v2;
		   value3 = v3;
		   value4 = v4;
	   }


Matrix::Matrix(const Matrix& oldValue)
	   {
		   value1 = oldValue.value1;
		   value2 = oldValue.value2;
		   value3 = oldValue.value3;
		   value4 = oldValue.value4;
	   }

double Matrix::det()				// Calculates the determinant 
{						
	double determ;
	determ = (value1 * value4) - (value2 * value3);
  return (determ);
}

bool Matrix::isSingular()							 //Determines if matrix is singular
{																	//i.e. Is the determinant equal to zero?
	bool sing;														//Declares boolean variable  
	if (determ == 0)
		sing = 0;
	else
		sing = 1;
   return (sing);
}
void Matrix::inverse() 
{																	// Calculates the inverse of the original matrix
	valIn1 = (value4 * (1/determ));
	valIn4 = (value1 * (1/determ));
	valIn2 = (-1 * value2)*(1/determ);
	valIn3 = (-1 * value3)*(1/determ);
}

void Matrix::print_em() 
{																	// Prints matrix and its inverse
	cout << setiosflags(ios::fixed) << setprecision(1);				// Sets outputs for one decimal
	if (sing = 1)
	{
		cout << "Matrix: " << endl;
		cout << "[ " << value1 << "  " << value2 << " ]" << endl;
		cout << "[ " << value3 << "  " << value4 << " ]" << endl << endl << endl;
		cout << "Inverse of Function: " << endl;
		cout << "[ " << valIn1 << "  " << valIn2 << " ]" << endl;
		cout << "[ " << valIn3 << "  " << valIn4 << " ]" << endl << endl << endl;
	}
	else
	{
		cout << "Matrix: " << endl;
		cout << "[ " << value1 << "  " << value2 << " ]" << endl;
		cout << "[ " << value3 << "  " << value4 << " ]" << endl << endl << endl;
		cout << "Matrix has no inverse; determinant = 0!" << endl;
		cout << "[ N/A  N/A ]" << endl;
		cout << "[ N/A  N/A ]" << endl << endl << endl;
	}

}

int main()
{
	Matrix mat1 (1, 2, 3, 4);
	Matrix mat2 (1, 2, 2, 4);						//Two class variables 
	
	mat1.det();											//First matrix's determinant is calculated
	if (mat1.isSingular()== 1)									//First matrix is checked for singularity
	{								//If matrix 1 is not singular, calculates inverse 
		mat1.inverse();									//Then prints matrix and its inverse
		mat1.print_em();
	}
	else
	{
		mat1.print_em();								//Otherwise, if first matrix is singular
	}													//Prints matrix plus note that determinant = 0, no inverse matrix
	mat2.det();											//Second matrix's determinant is calculated
	if (mat2.isSingular()== 1)									//Second matrix is checked for singularity
	{										//If matrix 2 is not singular, calculates inverse
		mat2.inverse();									//Then prints matrix and its inverse
		mat2.print_em();				
	}
	else
	{
		mat2.print_em();								//otherwise, if second matrix is singular
	}													//Prints matrix plus note that determinant = 0, no inverse matrix
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Debugging - Variables Have Garbage Values - How Fix?

#2 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5667
  • View blog
  • Posts: 22,511
  • Joined: 23-August 08

Re: Debugging - Variables Have Garbage Values - How Fix?

Posted 06 September 2010 - 04:47 AM

if (sing = 1)

Assignment, not a comparison. == is for comparison.
Was This Post Helpful? 1
  • +
  • -

#3 buskus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-September 10

Re: Debugging - Variables Have Garbage Values - How Fix?

Posted 06 September 2010 - 05:21 AM

Thanks, I missed that. I made the change, saved and rebuilt the file but now the output was as if both matrices were singular. Any other suggestions? I'll keep looking myself & update here if I figure it out

This post has been edited by buskus: 06 September 2010 - 05:42 AM

Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Debugging - Variables Have Garbage Values - How Fix?

Posted 06 September 2010 - 05:46 AM

View Postbuskus, on 06 September 2010 - 09:21 PM, said:

there was no change in the output.


Really?
Are you sure?
Post your output with the "=" in place and with it fixed to "=="
Here's my output with the error in place:
Matrix: 
[ 1.0  2.0 ]
[ 3.0  4.0 ]


Inverse of Function: 
[ 0.0  0.0 ]
[ 0.0  0.0 ]


valIn1 = infMatrix: 
[ 1.0  2.0 ]
[ 2.0  4.0 ]


Inverse of Function: 
[ inf  -inf ]
[ -inf  inf ]



And with it fixed:
Matrix: 
[ 1.0  2.0 ]
[ 3.0  4.0 ]


Matrix has no inverse; determinant = 0!
[ N/A  N/A ]
[ N/A  N/A ]


Matrix: 
[ 1.0  2.0 ]
[ 2.0  4.0 ]


Inverse of Function: 
[ inf  -inf ]
[ -inf  inf ]



Still wrong but not the same.
Are you sure it made no change for you?
Attention to detail is vitally important in coding so interested to see how carefully you are watching what is happening.
The point is that there is a big hint in the second output that you may have missed if you weren't paying attention.

Modify your code by adding output statements to the following methods as I have below:
double Matrix::det()				// Calculates the determinant 
{						
	double determ;
	determ = (value1 * value4) - (value2 * value3);
	cout << endl << "determ in det()= " << determ << endl;
  return (determ);
}

bool Matrix::isSingular()							 //Determines if matrix is singular
{																	//i.e. Is the determinant equal to zero?
	bool sing;	
	cout << endl << "determ in isSingular() = " << determ << endl;														//Declares boolean variable  
	if (determ == 0)
		sing = 0;
	else
		sing = 1;
   return (sing);
}
void Matrix::inverse() 
{																	// Calculates the inverse of the original matrix
	cout << endl << "determ in inverse() = " << determ << endl;
	valIn1 = (value4 * (1/determ));
	valIn4 = (value1 * (1/determ));
	valIn2 = (-1 * value2)*(1/determ);
	valIn3 = (-1 * value3)*(1/determ);
}


Now compile and run your code.
What did you learn?

BTW - Why are determ and sing public?
Do you want to access them outside the class?

This post has been edited by janotte: 06 September 2010 - 05:56 AM

Was This Post Helpful? 1
  • +
  • -

#5 buskus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-September 10

Re: Debugging - Variables Have Garbage Values - How Fix?

Posted 06 September 2010 - 05:57 AM

Sorry, I did edit my post above, however, my output for both inverse matrices was [N/A N/A], as if both matrices were singular. That tells me that the variable determ must be set to 0. Or am I off track?
Was This Post Helpful? 0
  • +
  • -

#6 buskus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-September 10

Re: Debugging - Variables Have Garbage Values - How Fix?

Posted 06 September 2010 - 06:17 AM

I made the additions you suggested and reran the program: the determinant was calculated correctly at first, but once it got to inSingular it was garbage for both matrices. However, I went back and both determ and sing private rather than public. I compiled and reran and got the same output so I obviously know I need to do something else. Is it a copy constructor for both? I'll try.
Was This Post Helpful? 0
  • +
  • -

#7 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Debugging - Variables Have Garbage Values - How Fix?

Posted 06 September 2010 - 06:32 AM

You are using local variables instead of using the instance variables:

double Matrix::det()				// Calculates the determinant 
{						
//	double determ; // local variable only.
	determ = (value1 * value4) - (value2 * value3);
	return (determ);
}

bool Matrix::isSingular()							 //Determines if matrix is singular
{																	//i.e. Is the determinant equal to zero?
//bool sing; // local variable only. 

	if (determ == 0)
		sing = false; // use instance sing variable
	else
		sing = true;

	return (sing);
}



Also, drop the tests for booleans being 1 or 0:

void Matrix::print_em() 
{																	// Prints matrix and its inverse
	cout << setiosflags(ios::fixed) << setprecision(1);				// Sets outputs for one decimal
	if (sing)  // sing is either true or false, no need to check for 1 or 0
	{
...
if (mat1.isSingular()) //First matrix is checked for singularity
...
if (mat2.isSingular()) //Second matrix is checked for singularity
...



Output with these changes:

Quote

Matrix:
[ 1.0 2.0 ]
[ 3.0 4.0 ]


Inverse of Function:
[ -2.0 1.0 ]
[ 1.5 -0.5 ]


Matrix:
[ 1.0 2.0 ]
[ 2.0 4.0 ]


Matrix has no inverse; determinant = 0!
[ N/A N/A ]
[ N/A N/A ]


Press any key to continue . . .

This post has been edited by n8wxs: 06 September 2010 - 06:38 AM

Was This Post Helpful? 0
  • +
  • -

#8 buskus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-September 10

Re: Debugging - Variables Have Garbage Values - How Fix?

Posted 06 September 2010 - 06:51 AM

I made the changes and the program ran and the output was correct. I was on the path to eliminating the local variables but had not thought of the redundancy of the boolean tests. Everyone's help was much appreciated!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1