Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Actually i took this question from a site called codechef.com
Could you please read my code and tell me if my logic is correct.

After reading the question

Question:
It's the 1st of the month again, which means it's time for Dave to pay his bills.
Dave needs to go to the post office to mail his rent cheque, and wants to take the
shortest route possible. Dave wonders how many different shortest routes he can take.

Dave lives at the southwest corner of town (0,0) and the post office is at the northeast corner of town (W,H).
It takes Dave exactly one minute to move from (x, y) to (x, y+1), (x, y-1), (x+1, y) or (x-1, y).
Dave cannot move further south or west than his house, nor can he move further north or east than the post office
(that is, Dave's x coordinate must stay between 0 and W, and his y coordinate must stay between 0 and H at all times).
Additionally, there are N intersections that are being worked on by construction crews,
and Dave must avoid these intersections.

The number of shortest routes may be very large, so print the result modulo 1000000037.
If it is impossible for Dave to reach the post office, the answer is 0.

My code:

C++
#include<iostream>
using namespace std;
/*int count=0;
int w,h;
int n;
int arr[w][h];
int row,col;
char ch;*/
int main()
{
int w,h;
int n;
int arr[w][h];
int row,col;
char ch;
cout<<"Enter the intersection points  :";
while(ch=='n' || ch=='N')
	{	
	 cin>>row>>col;
	arr[row][col]==2;
	cout<<"\nDo you want to continue ";
	cin>>ch;
	}

void select()
{
int x=0,y=0;
int count;
int num;
a:
num=rand(4);
if((num==1) || (arr[x][y]==arr[w][h]))
	{
	goto x;
	}
else if(num==2)
	{
	goto y;
	}
else if(num==3)
	{
	goto z;
	}
else if(num==4)
	{
	goto w;
	}
else
	goto d;
x:
if(arr[x+1][y]!=2)
        {
        arr[x+1][y]=1;
        arr[x][y]=0;
        count++;
        }
else
        {
        cout<<"\nYou have no option remaining ";
        goto a;
        }

;
y:
if(arr[x][y+1]!=2)
        {
        arr[x][y+1]=1;
        arr[x][y]=0;
        count++;
        }
else
        {
        cout<<"\nYou have no option remaining ";
        goto a;
        }

;
z:
if(arr[x-1][y]!=2)
        {
        arr[x-1][y]=1;
        arr[x][y]=0;
        count++;
        }
else
        {
        cout<<"\nYou have no option remaining ";
        goto a;
        }

;
w:
if(arr[x][y-1]!=2)
        {
        arr[x][y-1]=1;
        arr[x][y]=0;
        count++;
        }
else 
	{
	cout<<"\nYou have no option remaining ";
	goto a;
	}
 
;
d:
else
        cout<<"\nThere is no option in the present case ";
	cout<<"The count for this case was "<<count;
	count=0;
        select();
;
}
return 0;
}

I would be really thakfull if you help me with this code

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 1-Sep-14 19:51pm
v2
Comments
Mohibur Rashid 1-Sep-14 20:44pm    
I didn't read your question or code. After reading your title I am going to ask you, do you think its logic flow is correct? How about debugging? did you try? If not why don't you try?
Sergey Alexandrovich Kryukov 1-Sep-14 23:10pm    
This problem text contains so many untold assumptions! I guess, the city is assumed to represent a rectilinear grid, or even a grid made of squares. How can it possibly be considered obvious? And if this is not the case, pretty much nothing can be considered defined. And so on... You need to formulate the problems in a real comprehensive way, otherwise they makes no sense.

Besides, this is not a fully defined question. To know your logic, we would need to know a comprehensively defined problem and see mathematical reasoning, not just your code dump, something not properly commented and not motivated. Unfortunately, we don't have resource for just "help". In this forum, we only help through answering questions or giving advice in response to questions...

—SA

I've added formatting code to you post to make it easier to read, but...

Well - I'm not even going to look at the "logic" and try to work it out, for a number of reasons.
1) It won't compile. You can't nest functions in C++.
2) You are using goto and creating "spaghetti code" as a result.
3) It's uncommented, and the variable names are meaningless.

This all means that it's hard to read, and thus difficult to work out exactly what it is doing. As a result, I woudl have to invest a considerable amount of time in working out what it does and how it does it before I could be sure if it even came close to doing what your task calls for.

Stop using goto and forget it even exists for a couple of years - until you have enough experience to understand when it is a good time to use it - and I have been using C, C++ and C# since the PC was invented and I haven't used a goto outside assembler code in all that time.
The presence of goto in a beginners code is a red flag: it says "this code is wrong" becasue if the code needs it, it's been designed wrong!
 
Share this answer
 
Comments
Stefan_Lang 2-Sep-14 3:25am    
You forgot to mention that the nested function isn't even called from main, but it *is* called within itself, making it recursive spaghetti code! :omg:

Have a 5 anyway for making a strong point about goto ;-)
In addition to OriginalGriffs points I'd like to add:

4. Don't use recursion until you know how it works and understand the repercussions on runtime behaviour. (You call select() withtin the function itself recursively)

5. Your array arr hasn't been defined properly, if defined this way, the dimensions must be constants, not variables. (and the compiler will tell you that!) Moreover, you never initialized the dimensions. You either must allocate the array dynamically, or - better - use std::vector instead.

6. You are using the variables x and y to access the array, but you never modify these values.

7. Nor do you ever check them for valid values: in several places you're accessing array elements at x-1, y-1, x+1, or y+1, either of which may be outside the bounds of the array.

7.1 talking about that - you have a comparison against arr[w][h] which is outside the bounds - or would be if the variables w and h were (a) initialized) and (b) known inside the function select() (hint: both (a) and (b) are not true!)

8. I don't see any code responsible for enumerating all possible routes. You're supposed to count them. I only see you counting steps (i. e. the length of one route)

9. There's nothing random in the task description. Don't use rand()!

10. The task specifically mentions there may be a very high number of routes. Given that, using standard output and input is a bad idea! Use files instead!




Having said all that, I suggest you forget about this particular site for now, and instead learn to program in C/C++ using proper tutorials, e. g. http://www.cplusplus.com/doc/tutorial/[^] or http://www.cprogramming.com/tutorial/c++-tutorial.html[^]. That should help you avoid most of these issues, and may give you a better idea on how to structure a program.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900