My code is betraying with me!

I made this code & took almost 10 hours. But there is a problem in the output.

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
#include <iostream>
using namespace std;

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

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 = true;
	}
}

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

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

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

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

	Queue q1(n);

	a = new int *[n];
	for(i=0; i<n; ++i)
		a[i] = new int [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];
			}
		}
	

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

	cout<<"Your graph 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:\n";
	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";
			break;
			}
			
			else{
				for(j=0; j<n; j++)
					if(a[x][j] == 1)
						if(b[k] != j)
						q1.enqueue(j);
			}

			if(false){
			cout<<s<<" -> "<<d<<" is not reachable.\n";
			break;
			}
		}

	cout<<endl;

	system ("Pause");
	return 0;
}


The problem is, the program gives the same output for any case; i.e., it shows
reachable
from a node to another for both case (0 & 1). Conversely, it shows
not reachable
from a node to another for both case (0 & 1).
But I want the output as
reachable
for 1(one) &
not reachable
for 0(zero).

I think, the problem is in here(line 138):
1
2
3
4
if(false){
	cout<<s<<" -> "<<d<<" is not reachable.\n";
	break;
}


Can anyone fix this?
Thanks in advance. :)
Last edited on
false? what is false? this statement always false so it skips it always.

I didnt understand what do you want to do with tat code but it would be better if you did it like that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
			if(x == d)
                        {
			         cout << s << " -> "  << d << " is reachable.\n";
			         break;
			}
			
			else
                        {
			  for(j=0; j<n; j++)
			  {
                                  if(a[x][j] == 1 && b[k] != j)
					q1.enqueue(j);
                          }
                          cout<<s<<" -> "<<d<<" is not reachable.\n";
			  break;
			}


Btw dont be lazy and give your variables meaningful names, write more readable code and use that curly brackets when necessary
Its not working with your solution. @senhor
(Shows
not reachable
for both case)
Last edited on
Ok, it looks like you have no idea what you're doing, or you're trying to be a robot and write C++ by the book (which actually isn't a good idea, since most books don't have the best examples to explain things).

- Give your vaiables and objects meaningfull names. Instead of 'p', name it 'input' or 'user_input'. Instead of 'n', name it 'user_defined_node'. What you anme them is up to you, but you shouldn't have to read the entire thing to get a feel for what it's used for.

- Look up what an lvalue and rvalue is. ++an_int is not the same as an_int++.

- I think you need to brush up on your conditionals. if(booloean == true) is what an if statement does, so: if(!is_right) equates to if(is_right != true). if(false) will always return false because false != true.

- finally, break it down. Stop trying to construct the whole thing. Put all the little tiny steps into functions. Example: bool is_node_reachable(const node&);, void display_graph(const graph&);, void fill_graph(graph&);. These are just prototypes, but you should get what I mean. It will make it much easier to pinpoint what's being done, and where, when there is only a single place the instructions can occur.
I made this code & took almost 10 hours. But there is a problem in the output.


I've mentioned this earlier. And you saying, I've no idea what i did? You think it was a waste of time??
And what if there is no meaning with variables? Although that is good programming practice, but nothing else.

And yaah, ++an_int is not the same as an_int++. But that doesn't make any major change there.

And thanks for your afford.
Last edited on
Look at line 42 ... rear = (rear+1)%q; ... and consider what happens if the enqueue() function gets called more than once.
My suggestion would be to start a new project and copy your queue class over to it. Do some simple tests there to ensure that the class is working as expected before trying to use it in more complicated code.
Last edited on
LTP, you can always give vairables meaningful names, or names that better clarify what they're used for.

I also don't think you understand what the difference between an rvalue and lvalue are: one is a constant, the other is not. You seem to switch between the two with no change in operation: aka you don't use rvalues when you aren't going to change the variable in some cases, which makes using lvalues pointless in the other, similar, scenarios. It's about consistency, not how it works (although it may be in some cases).

It would seem to me that what you're lacking is study. Somthing like this shouldn't take 10 hours to write. That being said, I can only assume you're throwing code at the wall and hoping it sticks. That will never work.
Last edited on
Topic archived. No new replies allowed.