Thread: gauss elimination method not working

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    26

    gauss elimination method not working

    the gauss elimination function seems to be not working, I cannot find the mistake.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    void inverse(int r, int c, int* matrix[]);
    void vectorb(int r);
    void gauss(int r, int c, int *A[]);
    void gauss(int r, int c, int *a[]);
    
    
    int main()
    {
    	srand(time(NULL));
    	int r;
    	int c;
    	printf("define the size of the matrix\n number of rows\n");
    	scanf_s("%d", &r);
    	printf("number of columns\n");
    	scanf_s("%d", &c);
    	int **matrix = (int**)malloc(r * sizeof(*matrix));
    	for (int i = 0; i < r; i++)
    	{
    		matrix[i] = (int*)malloc(c * sizeof(*matrix[r]));
    	}
    
    
    	for (int i = 0; i < r; ++i)
    	{
    		for (int j = 0; j < c; ++j)
    		{
    			matrix[i][j] = rand() % 21 - 10;
    			printf("%d     ", matrix[i][j]);
    
    
    
    
    		}
    
    
    		printf("\n");
    
    
    	}
    	inverse(r, c, matrix);
    	printf("\nvector is equal to \n");
    	vectorb(r);
    	gauss(r, c, matrix);
    	
    }
    
    
    
    
    void inverse(int r, int c, int* matrix[])
    {
    
    
    
    
    	double determinant = 0;
    	for (int i = 0; i<r; i++)
    		determinant = determinant + (matrix[0][i] * (matrix[1][(i + 1) % r] * matrix[2][(i + 2) % r] - matrix[1][(i + 2) % r] * matrix[2][(i + 1) % r]));
    
    
    	printf("\nInverse of matrix is: \n\n");
    	for (int j = 0; j<r; j++){
    		for (int i = 0; i<r; i++)
    
    
    			printf("%.2f\t", (((matrix[(i + 1) % r][(j + 1) % r] * matrix[(i + 2) % r][(j + 2) % r]) - (matrix[(i + 1) % r][(j + 2) % r] * matrix[(i + 2) % r][(j + 1) % r])) / determinant));
    		printf("\n");
    
    
    	}
    
    
    }
    void vectorb(int r)
    {
    	int *vector = (int*)malloc(r * sizeof(*vector));
    	for (int i = 0; i < r; i++)
    	{
    		vector[i] = (int)malloc(r * sizeof(*vector));
    
    
    	}
    
    
    	for (int i = 0; i < r; ++i)
    	{
    
    
    		vector[i] = rand() % 21 - 10;
    		printf("%d     ", vector[i]);
    		printf("\n");
    	}
    
    
    }
    void gauss(int r, int c, int *a[])
    {
    	for (int k = 0; k<r - 1; k++)
    	{
    		for (int i = k + 1; i<r; i++)
    		{
    [I]			double p = a[k] / a[k][k];
    			for (int j = k; j<r + 1; j++)
    [I]				a[i][j] = a[j] - p*a[k][j];
    		}
    	}
    	double *x = (double*)malloc(r * sizeof(*x));
    	for (int i = 0; i < r; i++)
    	{
    [I]		x = (int)malloc(r * sizeof(*x));
    
    	}
    
    	x[r - 1] = a[r - 1][r] / a[r - 1][r - 1];
    	for (int i = r - 2; i >= 0; i--)
    	{
    		int s = 0;
    		for (int j = i + 1; j<r; j++)
    		{
    [I]			s += (a[j] * x[j]);
    [I]			x[i] = (a[i][r] - s) / a[i];
    		}
    	}
    	printf("\nThe result is:\n");
    	for (int i = 0; i<r; i++)
    [I]		printf("\nx%d=%lf", i + 1, x);
    }
    

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > [I] x = (int)malloc(r * sizeof(*x));
    What exactly were you hoping to achieve with this line (and the enclosing loop)?

    x is a single pointer, so there is only need for a single malloc.

    vectorb() contains the same error as well, not to mention that vectorb() has no side effects except to print some things and return.

    Also, you're casting malloc in a C program.
    FAQ > Casting malloc - Cprogramming.com
    Check that you're not compiling your C code using a C++ compiler (did you name your source code as prog.cpp perchance?)

    Memory leaks are everywhere.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2015
    Posts
    26
    you are right, I think instead of using the matrix variable in the gauss function I should create another function that creates the augmented matrix (adding the vector b to the matrix) and then use it in the gauss function. Any idea how can I create this augmented matrix?

  4. #4
    Registered User
    Join Date
    Jan 2015
    Posts
    26
    I made this modification in my code, I added the new function and I now I just need to use the function matrix_augmented into the gauss function, is that right?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    void inverse(int r, int c, int* matrix[]);
    void matrix_augmented(int *a[], int *v, int r, int c);
    void gauss(int r, int c, int *a[]);
    
    
    int main()
    {
    	srand(time(NULL));
    	int r;
    	int c;
    	printf("define the size of the matrix\n number of rows\n");
    	scanf_s("%d", &r);
    	printf("number of columns\n");
    	scanf_s("%d", &c);
    	int **matrix = (int**)malloc(r * sizeof(*matrix));
    	int* vector = (int*)malloc(r*sizeof(*vector));
    	for (int i = 0; i < r; i++)
    	{
    		matrix[i] = (int*)malloc(c * sizeof(*matrix[r]));
    
    
    	}
    
    
    	matrix_augmented(matrix, vector, r, c);
    	
    	inverse(r, c, matrix);
    	gauss(r, c, matrix);
    	
    }
    
    
    
    
    void inverse(int r, int c, int* matrix[])
    {
    
    
    
    
    	double determinant = 0;
    	for (int i = 0; i<r; i++)
    		determinant = determinant + (matrix[0][i] * (matrix[1][(i + 1) % r] * matrix[2][(i + 2) % r] - matrix[1][(i + 2) % r] * matrix[2][(i + 1) % r]));
    
    
    	printf("\nInverse of matrix is: \n\n");
    	for (int j = 0; j<r; j++){
    		for (int i = 0; i<r; i++)
    
    
    			printf("%.2f\t", (((matrix[(i + 1) % r][(j + 1) % r] * matrix[(i + 2) % r][(j + 2) % r]) - (matrix[(i + 1) % r][(j + 2) % r] * matrix[(i + 2) % r][(j + 1) % r])) / determinant));
    		printf("\n");
    
    
    	}
    
    
    }
    
    
    void matrix_augmented(int *a[],int *v,int r,int c)
    {
    for (int i = 0; i < r; ++i)
    {
    for (int j = 0; j < c; ++j)
    {
    a[i][j] = rand() % 21 - 10;
    printf("%d     ", a[i][j]);
    v[i] = rand() % 21 - 10;
    
    
    
    
    }
    printf("%d", v[i]);
    printf("\n");
    
    
    }
    }
    void gauss(int r, int c, int *a[])
    {
    	for (int k = 0; k<r - 1; k++)
    	{
    		for (int i = k + 1; i<r; i++)
    		{
    			double p = a[i][k] / a[k][k];
    			for (int j = k; j<r + 1; j++)
    				a[i][j] = a[i][j] - p*a[k][j];
    		}
    	}
    	double *x = (double*)malloc(r * sizeof(*x));
    	
    	x[r - 1] = a[r - 1][r] / a[r - 1][r - 1];
    	for (int i = r - 2; i >= 0; i--)
    	{
    		int s = 0;
    		for (int j = i + 1; j<r; j++)
    		{
    			s += (a[i][j] * x[j]);
    			x[i] = (a[i][r] - s) / a[i][i];
    		}
    	}
    	printf("\nThe result is:\n");
    	for (int i = 0; i<r; i++)
    		printf("\nx%d=%lf", i + 1, x[i]);
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You need to check all the +/-1 at the limits of your array accesses, because there seem to be several places where out of bound accesses are possible.
    > for (int j = k; j<r + 1; j++)
    > a[i][j] = a[i][j] - p*a[k][j];
    and
    > x[r - 1] = a[r - 1][r] / a[r - 1][r - 1];

    Accessing [r] or [r+1] subscripts is out of bounds.

    > I added the new function and I now I just need to use the function matrix_augmented into the gauss function, is that right?
    I've no idea - but perhaps
    matrix_augmented(matrix, vector, r, c);
    inverse(r, c, matrix, vector);
    gauss(r, c, matrix, vector);

    I guess you had an idea....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jan 2015
    Posts
    26
    I think I did not understand what I am supposed to do with the +/-1.

  7. #7
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    My advice on coding numerics is to first do it in an interactive language like MATLAB, then port to C.

  8. #8
    Registered User taazz's Avatar
    Join Date
    May 2016
    Posts
    50
    Quote Originally Posted by Xavi Camps View Post
    I think I did not understand what I am supposed to do with the +/-1.
    What Salem said is that you need to check all the places where you add or subtract one from the variable that represents cells in the array especially around the boundaries of the array (ee 0 and max) to make sure that they are not creating an index outside the memory of the array. For example x[r - 1] is the result of the r-1 between 0 and max cell of X?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. facutly method isn't working
    By Matts in forum C Programming
    Replies: 3
    Last Post: 05-20-2011, 04:14 AM
  2. int to string - found method not working
    By epb in forum C++ Programming
    Replies: 12
    Last Post: 10-26-2009, 08:34 AM
  3. Replies: 1
    Last Post: 11-15-2001, 08:23 AM

Tags for this Thread