Calculating Inverse of a Function

Hello, I am trying to calculate the inverse of a matrix using C++.

I used to have the functions in the actual code, but that was creating infinite loops by redefining the variables inside the for loops, however I'm not sure if by moving them into functions if this problem was fixed by moving the variables?

At the moment the compiler gets to the function part of the code and then windows says "this program has stopped working".
This leads me to believe there is another infinite loop in my code, but I am thoroughly confused at the moment so I could be very wrong. Any help would be appreciated.

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
83
84
85
86
87
#include <iostream>
using namespace std;
typedef double* DArray;
void forward_sub(DArray *pie, int d1, int d2);
void backward_sub(DArray *pie, int d1, int d2);
int main()
{
    int d1, d2, i, j, k;
    labelstart: ;
    cout << "Enter the row and column dimensions of the augmented matrix:\n";
    cin >> d1 >> d2;
    DArray *pie= new DArray[d1];
    for(i = 0; i < d1; i++) {
        pie[i] = new double[d2];
        }
    cout << "Enter the values of the " << d1 << " x " << d2 
	 << " augmented matrix.\n";
    for (i=0;i<d1;i++){
        for (j=0;j<d2;j++){
            cin >> pie[i][j];
        }
    }
    int val;
    double x[d1];
    for(i=0;i<d1-1;i++){
        for(j=0;j<d2-1;j++){
            if (i=j){
            pie[d1-1][j]=1;
            }
            else{ 
            pie[d1-1][j]=0;
            }
        }
       forward_sub(pie, d1, d2);
       backward_sub(pie, d1, d2);
       for (j=0;j<d2-1;j++)
            {
            pie[j][i] = x[j];
        }   
        }
	for(i=0;i<d1-1;i++){
        for(j=0;j<d2-1;j++){
            cout << pie[i][j] << " ";
		}}
    cout << endl;
        for (i=0;i<d1;i++){
        delete[] pie[i];
    }
    delete[] pie;
    cout << ".\nWould you like to run process again? If 'YES' press 1, if 'NO' press 0. ";
    cin >> val;
    if (val == 1) goto labelstart;
    else goto exit;
    exit: ;
    return 0;
}
void forward_sub(DArray *pie, int d1, int d2){
     double factor;
     int i3, j3, k3;
     for(k3=0; k3 < d1-1; k3++){
            for (i3=k3+1;i3 < d2-1;i3++){
                factor = pie[i3][k3]/pie[k3][k3];
                for (j3=k3+1; j3 < d2; j3++){
                    pie[i3][j3] = pie[i3][j3] - factor * pie[k3][j3];
                }
                pie[i3][k3] = factor;
            }
        }
}
void backward_sub(DArray *pie, int d1, int d2)
{
     double x[3];
     x[3] = pie[2][3]/pie[2][2];
     int i2, j2, k2;
     double s;
     for (i2 = d1-1; i2 >=0; i2--)
     {
            s = pie[i2][d2-1];
            
            for (j2 = i2+1; j2 < d1; j2++){
                
                s = s -pie[i2][j2]*x[j2];
                }
            x[i2] = s/pie[i2][i2];
            
}
}
Last edited on
Topic archived. No new replies allowed.