Computing.Net > Forums > Programming > C++ help please

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ help please

Reply to Message Icon

Name: Sikh
Date: January 14, 2004 at 13:38:38 Pacific
OS: XP
CPU/Ram: No Idea
Comment:

I am having trouble using complex numbers to find the determinant of a square matrix. This is what I have so far, but my deter function is having trouble. I tried tracing it by hand, but I can't find anything. If someone would help I would really appreciate it.

#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
const int maxsize=10;
struct compl{
double r;
double i;
};

compl add(compl m, compl n)
{
compl sum;
sum.r=m.r + n.r;
sum.i=m.i + n.i;
return sum;
}

compl subtr(compl m, compl n)
{
compl diff;
diff.r=m.r - n.r;
diff.i=m.i - n.i;
return diff;
}

compl mult(compl m, compl n)
{
compl prod;
prod.r=(m.r * n.r)-(m.i * n.i);
prod.i = (m.r * n.r)+(n.r * n.r);
return prod;
}

compl divide(compl m, compl n)
{
compl quot;
quot.r=(m.r * n.r + m.i * n.i)/(m.r * n.r + n.i * n.r);
quot.i=(n.r * m.i - m.r* n.i)/(n.r * n.r + n.i * n.r);
return quot;
}

void matrix(compl source[maxsize][maxsize], compl dest[maxsize][maxsize],
int xexclude, int yexclude, int currsize)
/*
Input:
Output:
Purpose/Method:
*/
{
int row, //rows
c, //columns
cr; //current row
for (row=0; row<currsize; row++)
{
if(row<yexclude)
{cr=row;}
else
{cr=row-1;}
for (c=0; c<currsize; c++)
{
if (row!=yexclude&&c!=xexclude)
{
if(c<xexclude)
{dest[cr][c]=source[row][c];}
else
{dest[cr][c-1]=source[row][c];}
}
}
}
}

compl deter(compl source[maxsize][maxsize],int currsize)
/*
Input: User input from main pertaining to matrix size.
Output: Determinant returned.
Purpose/Method: Recursion
*/

{
compl dest[maxsize][maxsize];
compl total;
int yexclude;
total.r=0;
total.i=0;
for(yexclude = 0; yexclude<currsize; yexclude++)
{
int xexclude = 0;
if (currsize==1)
{
total=source[0][0];
return total;
}
matrix(source, dest, xexclude, yexclude, currsize);
if (yexclude%2==0)
{
total= add( total, mult(source[0][xexclude], deter(dest, currsize-1)));
}
else
{
total= subtr(total, mult(source[0][xexclude], deter(dest, currsize-1)));
}
}
return total;
}

void main(void)
/*
Input: User Input of Currsize, which determines matrix size and
numbers in columns of matrix.
Output: Returned value of deter function.
Purpose/Method: Using the 2 dimensional arrays to store the values of the
matrix.
*/
{
int cn, //column number
currsize, //current size
rn, //row number
row, //row
c; //column

cout<<"Enter Matrix size"<<" : "<<'\n';
cin>>currsize;
compl source[maxsize][maxsize];
for(rn=0; rn<currsize; rn++)
{
cout<<"Enter a column: "<<rn<<" : "<<'\n';
for(cn=0; cn<currsize;cn++)
{
cin>>deter(source, currsize).r
>>deter(source, currsize).i;
}
cout<<"Your Determinant"
<<" : "
<<deter(source, currsize).r
<<"+"
<<deter(source, currsize).i
<<"i";
}
}




Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


rename file n multiple di... Bat - write to txt



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ help please

C help www.computing.net/answers/programming/c-help/6263.html

Binary Search in C. Help Please! www.computing.net/answers/programming/binary-search-in-c-help-please/6847.html

Assembly Language Help please? www.computing.net/answers/programming/assembly-language-help-please/4705.html