-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcf.cpp
More file actions
127 lines (73 loc) · 1.9 KB
/
cf.cpp
File metadata and controls
127 lines (73 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <math.h>
using namespace std;
int i, j, coeff[10][10], n, fx[10], nmax, k;
float x[10];
printf("**Program to find solution of system of linear equation using Gauss Seidal Method**\n\n");
// Entering the number of equations
printf("Enter the number of equations:");
scanf("%d", &n);
// Entering the coefficients of the equations
for (i = 1; i <= n; i++)
{
printf("Enter the coefficients of equation %d :", i);
for (j = 1; j <= n; j++)
{
scanf("%d", &coeff[i][j]);
}
}
printf("-------------------------------------------------------\n");
// Enter the value of f(x) equivalent to the equation
for (i = 1; i <= n; i++)
{
printf("Enter the value of f(x) for equation %d :", i);
scanf("%d", &fx[i]);
}
printf("-------------------------------------------------------\n");
// Entering the maximum number of iterations
printf("Enter the maximum number of iterations :");
scanf("%d", &nmax);
printf("\n");
printf("Iter\t");
for (i = 1; i <= n; i++)
{
printf(" x%d\t\t", i);
}
printf("\n");
// Initialization of Gauss Seidal Method
// Calculating the value of the variables
for (i = 1; i <= n; i++)
{
x[i] = 0;
}
for (k = 1; k <= nmax; k++)
{
printf("%d\t", k);
for (i = 1; i <= n; i++)
{
x[i] = fx[i];
for (j = 1; j <= n; j++)
{
if (j != i)
{
x[i] = x[i] - coeff[i][j] * x[j];
}
}
x[i] = x[i] / coeff[i][i];
printf("%f\t", x[i]);
}
printf("\n");
}
// Printing the solution
printf("-------------------------------------------------------\n");
printf("The Solution of linear equations using Gauss Seidal Method\n ");
for (i = 1; i <= n; i++)
{
printf("-------------------------------------------------------\n");
printf("\t X%d = %f\n", i, x[i]);
}
getch();
return 0;
}