1

I'm trying to do the gaussian elimination with a dynamically created array in C but every time I run it, it stops at the gaussian elimination part. I don't know what's wrong, the IDE says no error but the program stops in the nested for loop part. I got the gaussian elimination code from online and tried to implement it into my program but it doesn't work any help?

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

double *createDynamicArray(int n);
double **createDynamicArray2D(int n, int m);
void displayArray(double **a, int n, int m);
void inputElements(double **a, int n, int m);
double gaussianElimination(double **a, int n, int m);



int main() {
    double **matrix,x[20], c, matrix2[20][20], sum;
    int n,m,i,j;
    printf("Gaussian Elimination for Linear Equations\n\n");
    printf("Enter Number of Equations for Matrix: ");
    scanf("%d",&n);
    m = n + 1;
    
    matrix = createDynamicArray2D(n,m);
    
    inputElements(matrix,n,m);

    printf("\nMatrix:\n");
    displayArray(matrix,n,m);
    
    
    printf("\nGaussian Elimination\n");
    gaussianElimination(matrix,n,m);
    displayArray(matrix,n,m);
    
    
    for (int x=0 ; x<n; x++){
        for (int y=0; y<n; y++){
            matrix2[x][y]==matrix[x][y];
        }
    }
    
    x[n] = matrix2[n][m]/matrix2[n][n];
    
     for(i=n-1;i>=1;i--)
     {
          x[i] = matrix2[i][n+1];
          for(j=i+1;j<=n;j++)
          {
                x[i] = x[i] - matrix2[i][j]*x[j];
          }
          x[i] = x[i]/matrix2[i][i];
     }

    for(i=n-1; i>=1; i--)
    {
        sum=0;
        for(j=i+1; j<=n; j++)
        {
            sum=sum+matrix[i][j]*x[j];
        }
        x[i]=(matrix[i][n+1]-sum)/matrix[i][i];
    }
    printf("\nThe Values are: \n");
    for(i=1; i<=n; i++)
    {
        printf("\nx%d = %.0f\t",i,x[i]);
    }
}

    

void displayArray(double **a, int n, int m) {
    int i,j;
    for (i=0; i<n; i++) {
        for (j=0; j<m; j++) {
            printf("%.0f\t",a[i][j]);
        }
        printf("\n");
    }
}

double *createDynamicArray(int n) {
    return (double *) calloc(n,sizeof(double));
}


double **createDynamicArray2D(int n, int m) {
    int i;
    double **y;
    y = (double **) calloc(n,sizeof(double **));
    for (i=0; i<n; i++) y[i] = createDynamicArray(m);
    return y;
}

void inputElements(double **a, int n, int m) {
    int i,j;
    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            printf("Element [%d][%d] : ", i,j);
            scanf("%lf",&a[i][j]);
        }
    }
}


double gaussianElimination(double **a, int m, int n){
    int i,j,k;
    double term;
    for(i=0;i<m-1;i++){
        for(k=i+1;k<m;k++){
            term=a[k][i]/a[i][i];
            for(j=0;j<n;j++){
                a[k][j]=a[k][j]-term*a[i][j];
            }
        }
    }
}

ok update i got it to run but the output is wrong:

Gaussian Elimination for Linear Equations

Enter Number of Equations for Matrix: 3
Element [0][0] : 1
Element [0][1] : 2
Element [0][2] : 3
Element [0][3] : 4
Element [1][0] : 5
Element [1][1] : 6
Element [1][2] : 7
Element [1][3] : 8
Element [2][0] : 9
Element [2][1] : 10
Element [2][2] : 1
Element [2][3] : 2

Matrix:
1       2       3       4
5       6       7       8
9       10      1       2

Gaussian Elimination
1       2       3       4
0       -4      -8      -12
0       0       -10     -10

The Values are:

x1 = -57718
x2 = -57718
x3 = 57718

any help?

KeenEm
  • 27
  • 4
  • 1
    Array indice in C starts from 0. Fix the range of `for` loops. – MikeCAT May 18 '21 at 13:47
  • 1
    Also `y = (double **) calloc(n,sizeof(double **));` should be `y = calloc(n,sizeof(*y));` or `y = calloc(n,sizeof(double*));`. Fix the type to allocate, and casting results of `malloc()` family is [considered as a bad practice](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – MikeCAT May 18 '21 at 13:48
  • Voting to close as simple typo since it's yet another wrong type passed to malloc question. – Lundin May 18 '21 at 13:58
  • Rid your self of the presumption that code that compiles without error is without error. The compiler (not the IDE) will detect all syntax errors and may warn of some obvious semantic errors (possibly more so if you enable optimisation). Syntactically correct code is not semantically correct code. You mention an IDE - presumably it includes a debugger? Try that before using SO as a debugger - it will be a lot quicker. – Clifford May 18 '21 at 14:00
  • `sum = sum + matrix[i][j] * x[j];` is a type mismatch; `sum` has type `int`, but `matrix` and `x` are `double`. A clear semantic error (that in my case the compiler warned about). Don't ignore the warnings, and do sot switch them off. – Clifford May 18 '21 at 14:02
  • 1
    Threw an exception at `c = matrix[i][j] / matrix[j][j];` in my debugger. Your array indexing starting from 1 (only in this function where elsewhere you got it right - presumably this is not all your code?). If you fix that, it fails at `x[n] = matrix[n][n + 1] / matrix[n][n];` because `n` is out of bounds. You need to fix your code, but you also need to learn how to debug your code. I'm done. The point is my debugger (Visual Studio 2019) trapped the exceptions at exactly the point of occurrence. – Clifford May 18 '21 at 14:09

0 Answers0