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

Click For Summary

Discussion Overview

The discussion revolves around a homework problem involving C programming for solving a system of linear equations represented in the form Ax = B. Participants are examining a provided code snippet that encounters compilation errors and discussing potential solutions and debugging strategies.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • Post 1 presents a C code snippet intended to solve a system of equations but encounters multiple compilation errors related to array indexing.
  • Some participants suggest that the errors stem from incorrect array indexing, specifically pointing out that x1 is defined as a one-dimensional array, which leads to invalid subscripting attempts.
  • One participant expresses confusion about the term "second index" and seeks clarification on the debugging process related to variable 'd' and its connection to the syntax errors.
  • Another participant identifies that the variable 'd' is related to the forward substitution process and questions whether there are issues in that part of the code.
  • A later reply acknowledges the misunderstanding regarding the dimensions of the array x1 and confirms that it cannot have a second index, indicating a realization of the mistake in the code.

Areas of Agreement / Disagreement

Participants generally agree that the errors are due to incorrect array indexing, particularly with the variable x1. However, there is some uncertainty regarding the implications of the forward substitution process and its debugging.

Contextual Notes

Limitations include the potential for unresolved issues in the forward substitution logic and the need for further clarification on how the variable 'd' is computed and utilized in the code.

Who May Find This Useful

Students learning C programming, particularly those focused on numerical methods and solving systems of equations, may find this discussion relevant.

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   Reactions: 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 ·
Replies
8
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 21 ·
Replies
21
Views
4K
  • · Replies 1 ·
Replies
1
Views
5K
Replies
14
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K