Using for loop in this equation using c++

Pages: 12
closed account (D80DSL3A)
Nice code for finding the inverse. It seems you missed one step - transpose the matrix of minors. This explains your result. I found that by reversing the roles of i and j in the for loops on lines 39 and 41 the correct result is printed.

I wrote some global functions for working with 3x3 arrays based on your code, though I made the array elements type double instead of type int.

Here's what I got.
The code in main declares your test case matrix, displays it and the determinant, calculates the inverse and shows its determinant then finds the product and displays it. I get the identity matrix as expected (hoped for).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
using namespace std;

void displayMatrix( double a[][3] );
double determinant( double a[][3] );
bool inverse( double a[][3], double aInv[][3] );// find inverse of a and store it in aInv.  return true if inverse exists
void multiply( double A[][3], double B[][3], double AtimesB[][3] );// find product of A and B. Store it in AtimesB

int main()
{
    double M[3][3] =  {{4,3,2} , {1,2,3,} , {6,5,3}};
    displayMatrix(M);
    cout << "determinant of M = " << determinant(M) << "\n\n";

    double Minv[3][3];
    if( inverse( M, Minv ) )
    {
        displayMatrix(Minv);
        cout << "determinant of Minv = " << determinant(Minv) << "\n\n";

        double I[3][3];
        multiply( M, Minv, I );
        displayMatrix(I);
    }
    else
        cout<<"Inverse does not exist (Determinant=0).\n";



    cout << endl;
    return 0;
}

void displayMatrix( double a[][3] )
{
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            cout<<a[i][j]<<"  ";
        }
        cout<<"\n";
    }
}

double determinant( double a[][3] )
{
    double det = 0.0;
    for(int i=0;i<3;i++)
        det = det + (a[0][i]*(a[1][(i+1)%3]* a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));

    return det;
}

// returns true if inverse exists
bool inverse( double a[][3], double aInv[][3] )
{
    double deter = determinant(a);

    if( deter == 0.0 ) return false;

    for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
            aInv[j][i] = ((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3])) / deter;

    return true;
}

void multiply( double A[][3], double B[][3], double AtimesB[][3] )
{
    for(int r=0; r<3; ++r)
    {
        for(int c=0; c<3; ++c)
        {
            AtimesB[r][c] = 0.0;
            for(int i=0; i<3; ++i)
            {
                AtimesB[r][c] += A[r][i]*B[i][c];
            }
        }
    }
}

Are these the kind of global functions you have in mind?
You could write functions for adding or subtracting matrices. They would be similar to multiply.

EDIT: If you run the above code in the shell here you'll notice the identity matrix has one element equal to -8.88178e-16. This looks funky, but that number is basically zero.
If you find a nice way to make numbers which are that close to zero appear as 0, please share it!
Last edited on
Hey good morning, I just saw your message but I sent the question again as a pm, sorry about that, I will give this code a go and check it out.

I had another question, how can I improve my knowledge for writing functions like you did, cause I cannot wrap my head around creating functions with arrays. Any book or website that I can read will be really helpful!

Thank you once again
Hey there I had a quick question, it is simple but I cannot wrap my head around it.

For example, if I want to call an element of the Identity matrix say I[0][0] = 1, and use 1 in an equation say:

C = 1 (element) * whatever how would i call that element cause its still in an array.

Thank you
closed account (D80DSL3A)
Call an element? Do you mean obtain its value? I[0][0] will give the value 1. I don't understand the question really. As long as 1.0* something is sensible then you can do it.

Please be a bit more specific about what you wish to do, or provide a fuller case.

I can't recommend any specific source for learning about arrays. This site has some tutorials which include that. Any text on c++ would explain how to work with them, pass them to functions, etc.
Hey there,

Hope all is well with you today! I had a question in creating a structure for a .STL traingle . For back ground information I have attached the link, the first two paragraphs and the ASCII STL part is what is revelant.

http://en.wikipedia.org/wiki/STL_%28file_format%29

My question here is, I have to create a structure for the representation of the triangle below. Mathematically I understand what to do, put the pseudo code below is confusing me as I do not know how to represent it in c++.

For example, the pseudo code below represents one triangle but multiple triangles are needed to be created using the structure below so am guessing the "outer loop part" does that. But am confused how to represent this structure in c++. Any suggestions from you will be extremely helpful to me. Thank you once again for you time and if you do not understand anything do let me know.


1
2
3
4
5
6
7
 facet normal ni nj nk
    outer loop
        vertex v1x v1y v1z
        vertex v2x v2y v2z
        vertex v3x v3y v3z
    endloop
endfacet
That is one triangle. A file can have many such "facet" entries. A facet is a triangle, because its "outer loop" contains three vertices.

The data structure that you have to create depends on what the data will be used for.
Topic archived. No new replies allowed.
Pages: 12