determinants

Alright i'm writing a program using 2 seperate functions det2()and det3(). The function det2() is supposed to calculate the determinant of a 2X2 matrix. Then i'm supposed to be using det3() to calculate the determinant of a 3X3 matrix which will call upon the det2 function to solve the required 2X2 matrices. I finally got the program to run but i'm getting a value of -1#QNAN, with any inputs I use. Not even sure what the heck that QNAN is and wondering if someone might know where i'm going wrong here. Thank You.

here is the code.


#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
void det2(double,double,double,double,double&);
void det3(double,double,double,double,double,double,double,double,double,double,double,double,double,double&);
int main()
{
double a1,a2,a3,a4,a5,a6,a7,a8,a9,dReturn,dAone,dAfour,dAseven,dThree;
cout<<endl;
cout<<" Enter the nine values of the 3X3 matrix, left to right top to bottom. "<<endl;
cout<<endl;
cout<<" Enter first value. ";
cin>>a1;
cout<<" Enter second value. ";
cin>>a2;
cout<<" Enter third value. ";
cin>>a3;
cout<<" Enter fourth value. ";
cin>>a4;
cout<<" Enter fifth value. ";
cin>>a5;
cout<<" Enter sixth value. ";
cin>>a6;
cout<<" Enter seventh value. ";
cin>>a7;
cout<<" Enter eigth value. ";
cin>>a8;
cout<<" Enter ninth value. ";
cin>>a9;
cout<<endl;

det3(a1,a2,a3,a4,a5,a6,a7,a8,a9,dReturn,dAone,dAfour,dAseven,dThree);

cout<<" The determinant of this 3X3 matrix is "<<dThree<<"."<<endl;
cout<<endl;
system("pause");
return 0;
}
void det2(double a,double b,double c,double d,double& dReturn)
{
dReturn=(a*d)-(c*b);
return;
}
void det3(double a1,double a2,double a3,double a4,double a5,double a6,double a7,double a8,double a9,double dReturn,double dAone,double dAfour,double dAseven,double& dThree)
{
det2(a5,a6,a8,a9,dReturn);
dReturn=dAone;
det2(a5,a6,a8,a9,dReturn);
dReturn=dAfour;
det2(a5,a6,a8,a9,dReturn);
dReturn=dAseven;

dThree=(a1*dAone)-(a4*dAfour)+(a7*dAseven);
return;
}

In your main you declare unitialized vars double dAone,dAfour,dAseven,dThree

you then call det3 using those vars. What you get is garbage.

With what intent do you expect to use those vars?

Edit: next time you post code please use code tags, it improves readability a lot.
Last edited on
QNAN = Quiet Not a Number

NaN = the value of a calculation couldn't be represented by a double. Either an overflow, and underflow, etc.
Some NaNs aren't quite; they cause exceptions (e.g. divide by zero). = (signalled) NaN.

Andy

P.S. Do you know what a struct is? If not, laying out your code a bit better might help you spot problems.
Topic archived. No new replies allowed.