switch problem
Nov 6, 2012 at 3:17pm UTC
this is part of the codes for my matrix problem.
when i run the codes, only Inverse matrix is working, but the button for Upper Triangular, and determinant are not working. i think it's because i make them in switch, and there is flag for each cases. but i don't know how to write the right codes to display the results based on the flags.
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
void CA5::OnInverse()
{
flag=1;
Compute();
}
void CA5::OnUpperTri()
{
flag=2;
Compute();
}
void CA5::OnDeterminant()
{
flag=3;
Compute();
}
void CA5::Compute()
{
GetInput();
CString s;
double B[N+1][N+1], X[N+1][N+1], g[N+1][N+1];
double Sum,m,q,p,product;
switch (flag)
{
case 1:
{
GetInput();
CString s;
//identity matrix
for (i=1; i<=N; i++)
for (j=1; j<=N; j++)
{
if (i==j)
B[i][j]=1;
else
B[i][j]=0;
}
// row operations
for (k=1;k<=N-1;k++)
for (i=k+1;i<=N;i++)
{
m=a[i][k]/a[k][k];
for (j=1;j<=N;j++)
{
a[i][j] -= m*a[k][j];
B[i][j] -= m*B[k][j];
}
}
// backward substitutions
for (i=N;i>=1;i--)
for (j=1;j<=N;j++)
{
Sum=0;
for (k=i+1;k<=N;k++)
Sum += a[i][k]*X[k][j];
X[i][j]=(B[i][j]-Sum)/a[i][i];
}
}
break ;
case 2:
{
//uppertringular
//row operations
for (k=1; k<=N-1; i++)
for (i=k+1; i<=N; i++)
{
q=a[i][k]/a[k][k];
for (j=1;j<=N;j++)
g[i][j] -=q*a[k][j];
}
}
break ;
case 3:
{
//determinant
//row operations
for (k=1; k<=N-1; k++)
for (i=k+1; i<=N; i++)
{
p=a[i][k]/a[k][k];
for (j=1; j<=2; j++)
a[i][j]-=p*a[k][j];
}
product=1;
for (i=1; i<=N; i++)
product *=a[i][i];
}
break ;
}
//displayresult for all 3 cases
// case 1
for (i=1;i<=N;i++)
{
for (j=1;j<=N;j++)
{
s.Format(L"%lf" ,X[i][j]);
sa1[i][j].SetWindowText(s);
}
}
// case 2
for (i=1;i<=N;i++)
{
for (j=1;j<=N;j++)
{
s.Format(L"%lf" ,g[i][j]);
sa2[i][j].SetWindowText(s);
}
}
//case 3
s.Format(L"%lf" ,product); sa3.SetWindowText(s);
}
Topic archived. No new replies allowed.