Write a program to demonstrate Gauss Elimination Method in C language.

Tested on: Turbo C++ Version 4.0Time Complexity: O (n3)Space Complexity: O (n2)Open Source: MIT
1
2
            
3
    /*Program to execute Gauss Elimination method */
4
    #include <stdio.h>
5
    #include <math.h>
6
    #include <conio.h>
7
    #define max 10
8
    void main()
9
    {
10
        int i, j, k, n;
11
        float t = 0, sol, a[max][max + 1], x[max];
12
        clrscr();
13
        printf("Enter number of equations\n");
14
        scanf("%d", &n);
15
        printf("Input data for arugement matrix: \n");
16
        for (i = 0; i < n; i++)
17
        {
18
            for (j = 0; j <= n; j++)
19
            {
20
                scanf("%f", &a[i][j]);
21
            }
22
        }
23
        for (j = 0; j < n - 1; j++)
24
        {
25
            for (i = j + 1; i < n; i++)
26
            {
27
                t = a[i][j] / a[j][j];
28
                for (k = 0;
29
                     k <= n;
30
                     k++)
31
                {
32
                    a[i][k] = a[i][k] - a[j][k] * t;
33
                }
34
            }
35
        }
36
        printf("\n The upper triangular matrix is: \n");
37
        for (i = 0; i < n; i++)
38
        {
39
            for (j = 0; j <= n; j++)
40
            {
41
                printf("%f\t", a[i][j]);
42
            }
43
            printf("\n");
44
        }
45
        for (i = 0; i < n; i++)
46
            x[i] = 0;
47
        for (i = n - 1; i >= 0; i--)
48
        {
49
            sol = 0;
50
            for (j = 0; j < n; j++)
51
            {
52
                sol = sol + a[i][j] * x[j];
53
            }
54
            x[i] = (a[i][n] - sol) / a[i][i];
55
        }
56
        printf("\n\n The solution is: ");
57
        for (i = 0; i < n; i++)
58
        {
59
            printf("\nElement %d: %f: ", i + 1, x[i]);
60
        }
61
        getch();
62
    }
63
64

Output

1
2
3
    Enter number of equations
4
    4
5
    Input data for augmented matrix:
6
    10 -7 3 5 6
7
    -6 8 -1 -4 5
8
    3 1 4 11 2
9
    5 -9 -2 4 7
10
11
    The upper triangular matrix is:
12
    10.000000      -7.000000        3.000000        5.000000        6.000000
13
    0.000000       3.800000         0.800000        -1.000000       8.600000
14
    -0.000000      -0.000000        2.447368        10.315789       -6.815791
15
    0.000000       -0 .000000       -0.000000       9.924731        9.924738
16
17
    The solutions is:
18
    Element 1: 5.000001
19
    Element 2: 4.000001
20
    Element 1: -7.000001
21
    Element 1: 1.000001
22
            
23
    

Rate the Code

Reviews are published after a quick safety check (usually within 24 hours).

Reviews are public. Developers can see basic device specifications to respond. Learn more

How to run Gauss Elimination Method program in C-Language?

To run Gauss Elimination Method program in C-Language, follow the below mentioned steps:

  1. Copy the Code: Press the copy button in the code block to copy the complete code.
  2. Open Turbo C++ Version 4: Open the Turbo C++ (Version 4) software on your system.
  3. Paste the code: Paste the copied code into Turbo C++’s editor.
  4. Compile the Code: Compile the program by pressing the Alt+F9 keys. This shows you the errors and warnings, if any.
  5. Run the code: Press the Ctrl+F9 keys to run the program if the program is successfully compiled.
  6. Enter the Input: Provide the required inputs or your desired inputs, if applicable.
  7. Receive the Result: Get the result of the program on the screen based on the provided inputs.

More About the Gauss Elimination Method

Discovered by: Chinese Mathematicians in 179 AD. Modified by: Carl Friedrich Gauss , 19thCentury

It is the most popular method to solve linear equations with many unknown variables (Ax=b).

How does it work?

  1. First, equations are converted to a matrix known as an augmented matrix.
  2. With the help of elementary row operations, the augmented matrix is converted into an upper triangular matrix (which has all the values zero below the diagonal. This process is known as forward elimination.
  3. Then the values are calculated from the last equation at the upper side of the diagonal. This process is known as back-substitution.

Why is it used?

When you have multiple linear equations and want to solve all of them simultaneously.

Real-Life Applications:

  1. Structural Engineering (Trusses & Bridges): To calculate the different tensions and other physical force factors for the bridge or roof trusses.
  2. Electrical Circuit Analysis (Kirchhoff’s Laws): To solve the multiple linear equations in various loop currents in a big electronic circuit. (This is correct as Kirchhoff’s Voltage or Current Laws)
  3. Economics (Input-Output Models): To balance the goods and services flow in different industries.

Limitations of Gauss Elimination Method

  1. Slow for big Equations with O(n3) complexity: if the number of equations is very large, then it will be slow due to many calculations.
  2. Round-Off Errors & division by zero: if a diagonal element value is zero or very close to zero, then it might cause a division error, and as a result, the precision will be wrong.

Alternate Methods

  1. Partial/Full Pivoting: to avoid division by zero or precision problems, you can use partial pivoting (which swaps rows).
  2. LU Decomposition: If you want to save the same matrix with different values, then you can use LU Decomposition.
  3. Iterative Methods (Jacobi or Gauss-Seidel Method ): If the system is very big and sparse (which contains more zero values), then you can use these iterative methods.
License: This program's source code is open-sourced under the MIT License, while the rest of the program's text content and overall dhimanp platform and brand content remain proprietary under dhimanp.

Submit your modified code

Your submissions are published after a quick safety check (usually within 7 days).

Submissions are public. Developers can see basic device specifications to respond. Learn more

Disclaimer

The information that you shared with us will be publicly visible, and everyone can see your information. And we (dhimanp) are not responsible if anyone misuses this information. We can remove any submitted code at any time for better transparency.

Privacy Policy

If you share your data with us by submitting your code for credit, then we may show your full name, school or college name, and city name on our website.

We are not using this data to track you and are not selling this data to anyone.

If you want to remove your name from our website, you can contact us, and we will remove your name from our website. In the removal process, full credit, including your full name, school or college, city, and code, will be removed from our website.