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

AI Thread Summary
The discussion centers around a C programming homework problem involving matrix operations for solving the equation Ax = B. The user encounters multiple syntax errors related to array indexing, specifically with the declaration of one-dimensional arrays being incorrectly accessed as two-dimensional arrays. Key suggestions include correcting the indexing of the arrays and ensuring proper initialization before use. The user acknowledges a misunderstanding regarding the dimensions of the arrays, which contributes to the errors. The conversation highlights the importance of understanding array structures in C programming to avoid similar issues in future coding tasks.
darwinharianto
Messages
42
Reaction score
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
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.
 
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
 
x1[0][0][/color]
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
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
 

Similar threads

Replies
8
Views
2K
Replies
7
Views
1K
Replies
9
Views
4K
Replies
3
Views
1K
Replies
1
Views
4K
Replies
3
Views
2K
Replies
10
Views
3K
Back
Top