435,257 Members | 2,812 Online
Need help? Post your question and get tips & solutions from a community of 435,257 IT Pros & Developers. It's quick & easy.

# car park question!

 P: n/a I have some difficulties about this que, anyone could help me to solve it...you can use a number of functions and may make use of arrays, structures and enumeration types as you see fit. At a certain college, a small parking lot is arranged in a rectangular shape, with 20 spaces numbered 1, 2, 3..... 19, 20. Traffic flow is one way in a counter- clockwise direction. Note that the first position encountered upon entering is 1 and the last is 20. Cars may exit or continue to drive in a counter-clockwise direction. The following assumptions apply to this problem:  At the start, the lot is full (all 20 spaces are occupied by parked cars).  In addition to the (20) cars already parked in the lot, there are K autos in the lot waiting for positions to become available.  Each waiting auto is positioned behind one of the occupied spaces. When a position becomes empty, the space is filled either by the car waiting at that position or, if no car is waiting at that position, by the closest car, bearing in mind that the traffic flow is one way. (There is sufficient room at each position for the car parked in that position to leave and the car waiting at that position to then park.)  When an auto advances N positions to a free spot, all other cars advance N positions. Since the lot is circular, advancing 4 positions from position 18 means advancing to position 2.  None of the waiting cars exits. Sample Input Input consist of a dataset which is in two parts. The first part consists of integers, one per line beginning in column 1, representing initial positions of waiting autos. An integer 99 signals the end of this part of the data. The second part consists of integers, in the same format, representing positions vacated. INPUT: 6 19 17 13 1 99 1 3 20 16 99 Sample Output The output of each dataset should consist a series of lines giving, for each initial (waiting) car position, the initial position and the final position of that car based on the description and assumptions stated above. The output lines must appear in the same order as the order of the initial positions given in the input. OUTPUT: Original position 6 parked in 16 Original position 19 parked in 3 Original position 17 did not park Original position 13 parked in 20 Original position 1 parked in 1 thx ! Apr 22 '06 #1
9 Replies

 P: n/a S0UL said: I have some difficulties about this que, anyone could help me to solve it...you can use a number of functions and may make use of arrays, structures and enumeration types as you see fit. Consider the concept of a circular list. Please note that we are not a homework service. We can help you to understand C better, and that certainly can include helping you with your homework at times, but please note that it is /your/ homework, not ours. Give it your best shot, and ask for help when you get stuck. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at above domain (but drop the www, obviously) Apr 22 '06 #2

 P: n/a On 2006-04-22, S0UL wrote: I have some difficulties about this que, anyone could help me to solve it...you can use a number of functions and may make use of arrays, structures and enumeration types as you see fit. At a certain college, a small parking lot is arranged in a rectangular shape, with 20 spaces numbered 1, 2, 3..... 19, 20. Traffic flow is one way in a counter- clockwise direction. Note that the first position encountered upon entering is 1 and the last is 20. Cars may exit or continue to drive in a counter-clockwise direction. The following assumptions apply to this problem:  At the start, the lot is full (all 20 spaces are occupied by parked cars).  In addition to the (20) cars already parked in the lot, there are K autos in the lot waiting for positions to become available.  Each waiting auto is positioned behind one of the occupied spaces. When a position becomes empty, the space is filled either by the car waiting at that position or, if no car is waiting at that position, by the closest car, bearing in mind that the traffic flow is one way. (There is sufficient room at each position for the car parked in that position to leave and the car waiting at that position to then park.)  When an auto advances N positions to a free spot, all other cars advance N positions. Since the lot is circular, advancing 4 positions from position 18 means advancing to position 2.  None of the waiting cars exits. Sample Input Input consist of a dataset which is in two parts. The first part consists of integers, one per line beginning in column 1, representing initial positions of waiting autos. An integer 99 signals the end of this part of the data. The second part consists of integers, in the same format, representing positions vacated. INPUT: 6 19 17 13 1 99 1 3 20 16 99 Sample Output The output of each dataset should consist a series of lines giving, for each initial (waiting) car position, the initial position and the final position of that car based on the description and assumptions stated above. The output lines must appear in the same order as the order of the initial positions given in the input. OUTPUT: Original position 6 parked in 16 Original position 19 parked in 3 Original position 17 did not park Original position 13 parked in 20 Original position 1 parked in 1 thx ! Nice question, do you have more? -- Linux is only free if your time has no value Apr 22 '06 #4

 P: n/a I just made a rough program but find that there are many problem....whats wrong?... #include #define size 20 /*closest car*/ int close( int car[] ) { int min=20,i,j,mincar; for (i=0; i (19-i+j) ) mincar=i+1; } } return mincar; } /*move space*/ int move( int w,int v ) /*w=waiting car, v=vacated*/ { int m=0; if (w>v) m =(20-w)+v; else if (v>=w) m = v-w; return m; } /*assign a place to car*/ void park( int i, int j ) { int car[size],mincar=0; for( i=0; i

 P: n/a "S0UL" wrote in message news:a8******************************@localhost.ta lkaboutprogramming.com...I just made a rough program but find that there are many problem....whats wrong?... #include #define size 20 /*closest car*/ int close( int car[] ) { int min=20,i,j,mincar; for (i=0; i (19-i+j) ) mincar=i+1; } } return mincar; } /*move space*/ int move( int w,int v ) /*w=waiting car, v=vacated*/ { int m=0; if (w>v) m =(20-w)+v; else if (v>=w) m = v-w; return m; } /*assign a place to car*/ void park( int i, int j ) { int car[size],mincar=0; for( i=0; i

 P: n/a can the program just use several function instead of structure?cuz I have some difficulties in writing structure.(actually Im a beginner of C) Apr 23 '06 #7

 P: n/a S0UL opined: can the program just use several function instead of structure?cuz I have some difficulties in writing structure.(actually Im a beginner of C) What are you talking about? Please quote some context, even when replying to your own posts. Usenet is not a message board, and not all people can see all the messages. Read the link in my sig. -- Apr 23 '06 #8

 P: n/a "S0UL" writes: can the program just use several function instead of structure?cuz I have some difficulties in writing structure.(actually Im a beginner of C) That's a very bad idea and doing it would just make your problems worse. Yu are taking the course to learn about the tools offered, one fo the *key* tools is a struct. Struct is just argot for what a civilian would call a record. The time to learn how to use them is now, not later when you have more time or something. Something like this, probably has errors. Using scanf is not the preferred way to read data, ignore that for now. You have much bigger problems than that. struct Car { int ord; int slot_i; int slot_c; int parked; }; Note the new field. A car with parked ==1 is parked in slot_c. struct Car car[50]; /* allow up to 50 cars */ //You fill the array as you read the part 1 data. for(i=0; i<50; i++) { int n; scanf("%d", n); if(n == 99) break; car[i].parked = 0; car[i].ord = i; car[i].slot_i = n; car[i].slot_c = n; } ---- When you move a car you only change slot_c, the current slot. You need the other two variables to identify the car when you make the listing at the end of the simulation. Apr 23 '06 #9

 P: n/a "osmium" wrote: struct Car { int ord; int slot_i; int slot_c; int parked; }; Note the new field. A car with parked ==1 is parked in slot_c. No need for parked. I forgot what I had yesterday, Remove each car from the car array and put it in a different array when it is parked. Set slot_c to the parking slot actually used. struct Car car[50]; /* allow up to 50 cars */ struct Car parked[50]; //You fill the array as you read the part 1 data. for(i=0; i<50; i++) { int n; scanf("%d", n); if(n == 99) break; //> car[i].parked = 0; car[i].ord = i; car[i].slot_i = n; car[i].slot_c = n; } ---- When you move a car you only change slot_c, the current slot. You need the other two variables to identify the car when you make the listing at the end of the simulation. Apr 23 '06 #10

### This discussion thread is closed

Replies have been disabled for this discussion.