infinite loop

these are codes for gauss siedel iteration...
seems the loops are running for infinite times..please help me finding the fault.

the moment i used k loop, things messed up i think because the A matrix prints correctly...

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
// GAUSS SIEDEL ITERATION TUT3.1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
double A[100][100],X[100][32767];
	int n=2,i;
	double w = 0.80;
	int ns=(n*n);

int print(int h)                                    // function to print the X matrix
{
	for(int t=1; t<=(ns); t++)
	{
		cout << X[t][h] << endl ;
	}
	return 0;
}

int main()
{
	for(i=1;  i<=(ns); i++)                       
	{
		X[i][1]=0;
	}
	
	for(i=1;  i<=(ns); i++)                       // entry of A
	{
		A[i][i] = -4;
	}
	for(i=1; i<=(ns-n); i++)
	{
		A[i][i+n] = 1;
	}
	for(i=(n+1); i<=(ns); i++)
	{
			A[i][i-n] = 1;
	}
	for(i=2; i<=(ns); i++)
	{
		A[i][i-1] = 1;
	}
	for(i=1; i<=((ns)-1); i++)
	{
		A[i][i+1] = 1;
	}                                                    


	for(i=1; i<=(ns); i++)                              // entry of constants in A
	{
		if (i==1)
		A[i][ns+1]=1;
		else
		A[i][ns+1]=0;
	}

	for(i=1; i<=ns; i++)                                 //divide by diagonal elements
	{
		double p=A[i][i];
		for(int j=1; j<=(ns+1); j++)
		{
			A[i][j] = A[i][j]/p ;
		}
	}
	

	for(i=1; i<=ns; i++)                                //putting minus sign to make it look like transposed equations
	{
		for(int j=1; j<=(ns); j++)
		{
			A[i][j] = (-1) * A[i][j];
		}
	}

	

	for(i=1; i<=ns; i++)                                // diagonal elements = 0
		A[i][i]=0;


	for(i=1; i<=ns; i++)                                
	{
		for(int j=1; j<=(ns+1); j++)
		{
			cout << A[i][j] << '\t' ;
		}
		cout << endl;
	}

	for(int k=2; k<=32767; k++)                             //iteration loop
	{
		for(i=1; i<=ns; i++)                            // row loop
		{
			double sum = 0;
			for(int j=1; j<=ns; j++)                        //column loop
			{
				sum = sum + w*(A[i][j]*X[j][k-1]);            // X[i][j] in place of X[k][j] 
				X[i][k+1] = sum;                             // X[i+1][i] in place of X[k+1][i]
			}
		X[i][k+1] +=  w * A[i][n+1]+(1-w)*(X[i][k]);
		
		}
		for(int e=1; e<=ns; e++)
		{
		if ((floorf((( X[e][k] - X[e][k-1]) / X[e][k] )*1000 ) / 1000 ) == 0.000005)     //chk %error = 0.055 and stop
			{
				print(k);
				break;
			}
		}
	}	
	return 0;
}
Last edited on
I did not check if your program is doing what is supposed to be doing, but I already found a big error. Suppose your loop over k reaches k=32767. In several places (line 100,102) you have X[i][32768] that is out of bounds
i actually have the answer to this question and it gives me the ans in only 6-7 iterations ie k=6 or 7...actually when i compile it, it just shows press any blah blah ....when i wrote a cout after line 100 just inside the loop it ran for infinity..was not stopping but i am not able to figure out why is this happening, coz on paper it is working for me...
Topic archived. No new replies allowed.