A silly mistake is crashing my code. Please help me.

I want to display, whether its reachable from a node to another or not.
Here is my code:

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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <iostream>
#include <ctime>
using namespace std;

class Queue{
    private:
        int *queue;
	int q;
        int front;
	int rear;
	bool empty;
    public:
	Queue();
        Queue(int);
	int que_size();
	int curr_size();
        void enqueue(int);
        void dequeue(int&);
        bool is_empty();
	bool is_full();
	bool not_exist(int[], int, int &);
};

Queue::Queue()
{
    q = 30;
    queue=new int[q];
    front=0;
    rear=0;
    empty = true;
}

Queue::Queue(int x)
{
    q=x;
    queue=new int[q];
    front=0;
    rear=0;
    empty = true;
}

void Queue::enqueue(int x)
{
	if(!is_full()){
	queue[rear] = x;
	rear = (rear+1)%q;

	if(empty)
	empty = false;
	}
}

void Queue::dequeue(int &x)
{
	if(!is_empty()){
		x = queue[front];
		front = (front+1)%q;

		if(front == rear)
			empty = false;
	}
}

bool Queue::is_empty()
{
	return empty;
}

bool Queue::is_full()
{
	if(!empty && front == rear)
		return true;
	return false;
}

bool Queue::not_exist(int b[], int n, int &y)
{
	int i;
	for(i=0; i<n; i++){
		if(b[i] != y)
			return true;
		return false;
	}
}


int main()
{
	int a[50][50];
	int b[50];
	int n, s, d;
	int i, j, k=0;
	int x;
	int distance, p, row, col;
	
	cout<<"How many nodes do you have?\n";
	cin>>n;

	cout<<"Enter percentage: ";
	cin>>p;

	Queue q1(n);


	for(i=0; i<n; ++i){	
		a[i][i] = 1;
		for(j=i+1; j<n; ++j){
			a[i][j] = rand()%2;
			a[j][i] = a[i][j];
		}
		cout<<endl;
	}

	distance = n - (n*((float)p/100));
	
	for(i=0; i<distance; ++i){
        row=rand()%n;
        col=rand()%n;
        if(row==col)
            a[row][col]=1;
            else{
		a[row][col]=0;
		a[col][row]=0;
	    }
    }

	cout<<"Your matrix is:\n"<<endl;
	for(i=0; i<n; ++i){
		for(j=0; j<n; j++)
			cout<<a[i][j]<<"  ";
		cout<<endl<<endl;
	}

	cout<<"Enter your source & destination: ";
	cin>>s>>d;

	q1.enqueue(s);

	while(!q1.is_empty()){ 

		q1.dequeue(x);
		b[k] = x;

		if(x == d)
		cout<<s<<" -> "<<d<<" is reachable.\n";
		else{
		for(j=0; j<n; ++j){
			if(a[x][j] == 1){
				if(q1.not_exist(b, 50, j) == true){
				k++;
				q1.enqueue(j);
			        }
		        }
		break;
		}
	        }
        }

	cout<<endl;

	system ("Pause");
	return 0;
}


Why my code crashes? Plz help me.
1
2
3
4
5
6
7
8
9
10
11
else{
		for(j=0; j<n; ++j){
			if(a[x][j] == 1){
				if(q1.not_exist(b, 50, j) == true){
				k++;
				q1.enqueue(j);
			        }
		        }
		break;
		}
	        }


The problem in my code is in here. But I cant fix the specific error.
Have you tried fixing your indentation first?
Actually, I think the problem is on line 60.

Where it manifests is on line 148. (Uninitialized memory is accessed to return the value of x leading to an out of bounds array access on a.)

Also, Queue::not_exist should not be a method of Queue, nor is it correct.

Equivalent to current not_exist:

1
2
3
4
5
bool not_exist(int b[], int n, int &y)
{
    if ( n )
        return b[0] == y ;
}
Last edited on
So, What should I change?
Do I have anything to add?
So, What should I change?

The code. Do you have a more specific question?
Ya.
My code is almost complete. Can you plz solve the problem?
He already did solve your problem: he found what was causing the error and explained it to you.
Can you plz solve the problem?

No. That task is yours.
Topic archived. No new replies allowed.