### My code is betraying with me!

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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147`` ``````#include 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>s>>d; q1.enqueue(s); while(!q1.is_empty()){ q1.dequeue(x); b[++k] = x; if(x == d){ cout< "< "<

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):
 ``1234`` ``````if(false){ cout< "<

Can anyone fix this?
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:

 ``12345678910111213141516`` `````` if(x == d) { cout << s << " -> " << d << " is reachable.\n"; break; } else { for(j=0; j "<

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.

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.