Thread: problems with functions

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

    problems with functions

    I have problems using my augmented_matrix function in my gauss function.. can anybody help me?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    double inverse(int r, int c, int* matrix[]);
    int **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_augmented(matrix, vector, r, c));
    
    
    	
    	
    
    
    
    
    }
    	
    	
    
    
    
    
    
    
    double 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("%.4f\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");
    
    
    	}
    	return (0);
    }
    int **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");
    	}
    	return 0;
    
    
    }
    
    
    
    
    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]);
    	}

  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
    matrix_augmented already writes it's results into a and v (ie matrix and vector in main).

    Which can be passed to gauss as (perhaps - depends how you define gauss)
    gauss(r, c, matrix, vector);


    How you had things in your previous thread was much better.
    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
    Hello,
    I made some changes to my code, now everything seems to work except some of the solutions given by the gauss method. (the first solution x[1] is always wrong). Is it normal that there is no error between multiplying inverse matrix with vector v and the gauss method? (becuase part of my task is to create a graph showing solution vs error).
    Here is the new code I am using.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    
    
    void gauss(int r, int c, double *A[], double B[]);
    
    
    int main()
    {
        srand(time(NULL));
        int r, c, n;
        double Amax = 10, Amin = -10, Bmax = 4, Bmin = -3;
        printf("Enter n :\n");
        scanf("%d", &n);
        printf("\n\n");
        r = n;
        c = n;
    
    
        double **A = (double**)malloc(r * sizeof(*A));
        double **Ainv = (double**)malloc(r * sizeof(*Ainv));
        double *B = (double*)malloc(r*sizeof(*B));
        double *X = (double*)malloc(r*sizeof(*X));
    
    
        for (int i = 0; i < r; i++)
        {
            A[i] = (double*)malloc(c * sizeof(*A[r]));
        }
    
    
        printf("Matrix A:\n");
        for (int i = 0; i < r; ++i)
        {
            for (int j = 0; j < c; ++j)
            {
                A[i][j] = (Amax - Amin) * rand() / RAND_MAX + Amin;
                printf("%lf\t", A[i][j]);
            }
            printf("\n");
        }
    
    
        printf("\nMatrix B:\n");
        for (int i = 0; i < r; ++i)
        {
            B[i] = (Bmax - Bmin) * rand() / RAND_MAX + Bmin;
            printf("%lf\n", B[i]);
        }
    
    
        double determinant = 0;
        double sum = 0;
        for (int i = 0; i < r; i++)
        {
            Ainv[i] = (double*)malloc(c * sizeof(*Ainv[r]));
        }
        for (int i = 0; i < r; i++)
        {
            determinant = determinant + (A[0][i] * (A[1][(i + 1) % r] * A[2][(i + 2) % r] - A[1][(i + 2) % r] * A[2][(i + 1) % r]));
        }
        printf("\nThe determinanat of A is:    %lf\n", determinant);
        printf("\nInverse of A is: \n\n");
        for (int j = 0; j < r; j++)
        {
            for (int i = 0; i < r; i++)
            {
                Ainv[i][j]=((A[(i + 1) % r][(j + 1) % r] * A[(i + 2) % r][(j + 2) % r]) - (A[(i + 1) % r][(j + 2) % r] * A[(i + 2) % r][(j + 1) % r])) / determinant;
                printf("%lf\t", Ainv[i][j]);
            }
            printf("\n");
        }
    
    
    
    
        printf("\nResults X: \n");
        for (int i = 0; i < r; i++)
        {
            for (int j = 0; j < r; j++)
            {
                sum = sum + Ainv[j][i] * B[j];
            }
            X[i] = sum;
            printf("X%d=%lf\n", i + 1, X[i]);
            sum = 0;
    
    
        }
    
    
        gauss(r, c, A, B);
        printf("\n\n");
    }
    
    
    
    
    void gauss(int r, int c, double *A[], double B[])
    {
        double p, s = 0;
        double **augmented = (double**)malloc(r * sizeof(*augmented));
    
    
        for (int i = 0; i < r; i++)
        {
            augmented[i] = (double*)malloc((c+1) * sizeof(*A[r]));
        }
    
    
        printf("\naugmented matrix:\n");
        for (int i = 0; i < r; ++i)
        {
            for (int j = 0; j < c; ++j)
            {
                augmented[i][j] = A[i][j];
                printf("%lf\t", augmented[i][j]);
    
    
            }
                for (int j = c; j < c + 1; j++)
                {
                    augmented[i][j] = B[i];
                    printf("%lf\t", augmented[i][j]);
                }
    
    
            printf("\n");
        }
    
    
        for (int k = 0; k < r - 1; k++)
        {
            for (int i = k + 1; i < r; i++)
            {
                p = augmented[i][k] / augmented[k][k];
                for (int j = k; j < r + 1; j++)
                {
                    augmented[i][j] = augmented[i][j] - p*augmented[k][j];
                }
            }
        }
    
    
            double *Xgauss = (double*)malloc(r * sizeof(*Xgauss));
    
    
            Xgauss[r - 1] = augmented[r - 1][r] / augmented[r - 1][r - 1];
            for (int i = r - 2; i >= 0; i--)
            {
                for (int j = i + 1; j<r; j++)
                {
                    s = s + (augmented[i][j] * Xgauss[j]);
                    Xgauss[i] = (augmented[i][r] - s) / augmented[i][i];
                }
            }
    
    
            printf("\n\nX for gauss method:\n");
    
    
            for (int i = 0; i < r; i++)
            {
                printf("X%d=%lf\n", i + 1, Xgauss[i]);
            }
            printf("\n\n");
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with Pointers in functions.
    By patrink in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2011, 10:46 PM
  2. Problems with functions
    By Poincare in forum C Programming
    Replies: 4
    Last Post: 04-19-2009, 05:52 PM
  3. Problems with functions and for loops
    By slava in forum C Programming
    Replies: 1
    Last Post: 11-08-2008, 01:30 PM
  4. Functions problems!
    By Paninaro in forum C Programming
    Replies: 2
    Last Post: 06-18-2002, 06:03 AM
  5. problems with functions
    By catalyst in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 06:55 AM

Tags for this Thread