Can Anyone Help with Simple C Programming in Ax=B Homework?

In summary,The author creates an array of 3×3 floating point values, A[][][]. Each column of A[][][] corresponds to a row of a 3×3 matrix, U1[][][], U2[][][], and U3[][][].The author creates an array of 2×2 floating point values, d1[][]. Each row of d1[][] corresponds to a column of A[][][].The author calculates the dot product of two arrays, d1[][] and L1[][], and stores the result in U1[][].The author calculates the dot product of two arrays, d1[][
  • #1
darwinharianto
42
0

Homework Statement


Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    int i,j,k,n;
    float A[3][3]={{3,0,6},{4,1,3},{6,3,0}};
    float U1[3][4]={{3,0,6,0},{4,1,3,0},{6,3,0,0}};
    float U2[3][4]={{3,0,6,0},{4,1,3,0},{6,3,0,0}};
    float U3[3][4]={{3,0,6,0},{4,1,3,0},{6,3,0,0}};
    float L[3][3]={{1,0,0},{0,1,0},{0,0,1}};
    float L1[3][4]={{1,0,0,1},{0,1,0,0},{0,0,1,0}};
    float L2[3][4]={{1,0,0,0},{0,1,0,1},{0,0,1,0}};
    float L3[3][4]={{1,0,0,0},{0,1,0,0},{0,0,1,1}};
    float d1[3];
    float d2[3];
    float d3[3];
    float x1[3];
    float x2[3];
        float x[3][3]={{0,0,0},{0,0,0},{0,0,0}};
    float x3[3],c,sum=0;
        n=2;
    for(j=0; j<=n; j++)
    {
        for(i=0; i<=n; i++)
        {
            if(i>j)
            {
                c=A[i][j]/A[j][j];
                L[i][j]=c;
                L1[i][j]=c;
                L2[i][j]=c;
                L3[i][j]=c;
                for(k=0; k<=n+1; k++)
                {
                    A[i][k]=A[i][k]-c*A[j][k];
                    U1[i][k]=A[i][k]-c*A[j][k];
                    U2[i][k]=A[i][k]-c*A[j][k];
                    U3[i][k]=A[i][k]-c*A[j][k];
                }
            }
        }
    }
    d1[2-n]=L1[2-n][n+1]/L1[2-n][2-n];
    U1[0][3]=d1[2-n];
    for(i=1; i<=n; i++)
        {
               sum=0;
               for(j=i-1; j>=n-2; j--)
                 {
                 sum=sum+L1[i][j]*d1[j];
                 }
                  d1[i]=(L1[i][n+1]-sum)/L1[i][i];
        U1[i][3]=d1[i];
        }
   
    d2[2-n]=L2[2-n][n+1]/L2[2-n][2-n];
    U2[0][3]=d2[2-n];
    for(i=1; i<=n; i++)
        {
               sum=0;
               for(j=i-1; j>=n-2; j--)
                 {
                 sum=sum+L2[i][j]*d2[j];
                  }
                  d2[i]=(L2[i][n+1]-sum)/L2[i][i];
        U2[i][3]=d2[i];
         }
    d3[2-n]=L3[2-n][n+1]/L3[2-n][2-n];
    U3[0][3]=d3[2-n];
    for(i=1; i<=n; i++)
        {
               sum=0;
               for(j=i-1; j>=n-2; j--)
                 {
                 sum=sum+L3[i][j]*d3[j];
                  }
                  d3[i]=(L3[i][n+1]-sum)/L3[i][i];
        U3[i][3]=d3[i];
         }
    x1[n]=U1[n][n+1]/U1[n][n];
    for(i=n-1; i>=0; i--)
        {
               sum=0;
               for(j=i+1; j<=n; j++)
                 {
                 sum=sum+U1[i][j]*x1[j];
                  }
              x1[i]=(U1[i][n+1]-sum)/U1[i][i];
         }
    x2[n]=U2[n][n+1]/U2[n][n];
    for(i=n-1; i>=0; i--)
        {
               sum=0;
               for(j=i+1; j<=n; j++)
                 {
                 sum=sum+U2[i][j]*x2[j];
                 }
              x2[i]=(U2[i][n+1]-sum)/U2[i][i];
         }
    x3[n]=U3[n][n+1]/U3[n][n];
    for(i=n-1; i>=0; i--)
        {
               sum=0;
               for(j=i+1; j<=n; j++)
                 {
                 sum=sum+U3[i][j]*x3[j];
                 }
              x3[i]=(U3[i][n+1]-sum)/U3[i][i];
        }
        x[0][0]=x1[0][0];
        x[1][0]=x1[1][0];
        x[2][0]=x1[2][0];
        x[0][1]=x2[0][0];
        x[1][1]=x2[1][0];
        x[2][1]=x2[2][0];
        x[0][2]=x3[0][0];
        x[1][2]=x3[1][0];
        x[2][2]=x3[2][0];
    printf("nilai matriks u \n");
    printf("%f \t",A[0][0]);
    printf("%f \t",A[0][1]);
        printf("%f \n",A[0][2]);
    printf("%f \t",A[1][0]);
    printf("%f \t",A[1][1]);
        printf("%f \n",A[1][2]);
    printf("%f \t",A[2][0]);
    printf("%f \t",A[2][1]);
        printf("%f \n\n\n",A[2][2]);
    printf("nilai L \n");
    printf("%f \t",L[0][0]);
    printf("%f \t",L[0][1]);
        printf("%f \n",L[0][2]);
    printf("%f \t",L[1][0]);
    printf("%f \t",L[1][1]);
        printf("%f \n",L[1][2]);
    printf("%f \t",L[2][0]);
    printf("%f \t",L[2][1]);
        printf("%f \n\n\n",L[2][2]);    
        printf("x pembuat invers \n");
    printf("%f \t",x[0][0]);
    printf("%f \t",x[0][1]);
        printf("%f \n",x[0][2]);
    printf("%f \t",x[1][0]);
    printf("%f \t",x[1][1]);
        printf("%f \n",x[1][2]);
    printf("%f \t",x[2][0]);
    printf("%f \t",x[2][1]);
        printf("%f \n",x[2][2]);
      return(0);
}
and the results is

/var/www/wwwcourse/cache/darwinharianto.c: In function 'int main()':

/var/www/wwwcourse/cache/darwinharianto.c:111:24: error: invalid types 'float[int]' for array subscript

/var/www/wwwcourse/cache/darwinharianto.c:112:24: error: invalid types 'float[int]' for array subscript

/var/www/wwwcourse/cache/darwinharianto.c:113:24: error: invalid types 'float[int]' for array subscript

/var/www/wwwcourse/cache/darwinharianto.c:114:24: error: invalid types 'float[int]' for array subscript

/var/www/wwwcourse/cache/darwinharianto.c:115:24: error: invalid types 'float[int]' for array subscript

/var/www/wwwcourse/cache/darwinharianto.c:116:24: error: invalid types 'float[int]' for array subscript

/var/www/wwwcourse/cache/darwinharianto.c:117:24: error: invalid types 'float[int]' for array subscript

/var/www/wwwcourse/cache/darwinharianto.c:118:24: error: invalid types 'float[int]' for array subscript

/var/www/wwwcourse/cache/darwinharianto.c:119:24: error: invalid types 'float[int]' for array subscript

Homework Equations

The Attempt at a Solution


i don't know what to do
i did declare x and the other
anyone know how to solve this?
thankyou for replying
 
Physics news on Phys.org
  • #2
Line 111 should be this: x[0][0]=x1[0][0];

You defined float x1[3];
There is no second index for x1. The other errors have the same origin.

I also think line 87 refers to indices of x1 you did not initialize before, so you might get unexpected results.
 
  • #3
what is second index?

i tried to display d
and i got the same result i think the problem comes from the forward substitution to find d
is there anything wrong in the debugging to find d?

thank you for the reply
 
  • #4
x1[0][0]
This is the wrong second index.
darwinharianto said:
i tried to display d
and i got the same result i think the problem comes from the forward substitution to find d
is there anything wrong in the debugging to find d?
What is d and how is it related to the syntax error?
 
  • Like
Likes darwinharianto
  • #5
oh now i understand
at top i wrote x1[0]
so i can't make x1[0][1]

thanks
i wrote another one and made a wrong deduction
thanks
 

1. What is Simple C programming in Ax=B?

Simple C programming in Ax=B is a method used to solve systems of linear equations with multiple variables. The "A" represents the matrix of coefficients, "x" represents the vector of unknown variables, and "B" represents the vector of constants. This method involves using C programming to manipulate the matrix and solve for the unknown variables.

2. How is Simple C programming in Ax=B different from other methods of solving linear equations?

Simple C programming in Ax=B is different from other methods of solving linear equations because it involves using computer programming to solve the equations, rather than solving them by hand. This makes it a more efficient and accurate way of solving systems of linear equations with many variables.

3. Can any system of linear equations be solved using Simple C programming in Ax=B?

Yes, Simple C programming in Ax=B can be used to solve any system of linear equations, regardless of the number of variables or equations. However, it is most commonly used for systems with a large number of variables, as it is more efficient than solving by hand.

4. Are there any prerequisites for learning Simple C programming in Ax=B?

Yes, in order to learn Simple C programming in Ax=B, you should have a basic understanding of linear algebra and C programming language. It is also helpful to have some knowledge of matrices and systems of linear equations.

5. Can Simple C programming in Ax=B be used for other types of equations?

Yes, Simple C programming in Ax=B can be used for other types of equations, such as quadratic equations. However, it is most commonly used for solving systems of linear equations with multiple variables.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
893
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
Back
Top