By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,257 Members | 2,812 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
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
"S0UL" writes:
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


Sheez! That sounds totally unrealistic to me. The first thing to do is
shake it around until it makes sense to you. A small white board you can
set on your lap and draw things might be helpful. I would adopt a
convention so that cars and slots were obviously different things. Car 6
can be, as time goes on, be waiting for any one of 20 different slots. Even
so, if slots were numbered 1001 to 1020 it might be helpful in keeping
things straight during the analysis phase.

These moronic drivers are advancing willy-nilly for no reason that I can
deduce. A car has two properties: original position and "currently waiting
at slot n". The original position is needed to identify the car when you
make the listing. The currently waiting property gets modified for all cars
every time any slot is vacated. Do the addition modulo 20. Model it with
a linked list and remove each car from the list as soon as it is parked and
put in a "history" vector (probably). Populate the queue (linked list) as
you read the part 1 data, and remove (most of) it as you read the part 2
data.

Oops. That ignores the requirement that the listing must be in a certain
order. So some fixup is needed, probably another datum is the structure
representing waiting cars, ordinal number of car in input data. And a sort
prior to making the listing. Don't take any of this too seriously, there
may be glitches I haven't thought about. There are a great many ways to
accomplish the end result and none of them are guaranteed the "best" way.
For example, the major data structure could be a linked list, a vector or an
array. Probably in that order.

Apr 22 '06 #3

P: n/a
On 2006-04-22, S0UL <so*******@yahoo.com.hk> 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 <stdio.h>
#define size 20

/*closest car*/
int close( int car[] )
{
int min=20,i,j,mincar;
for (i=0; i<size; i++)
{
for (j=0; j<size; j++)
{
if ( min > (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<size; i++ )
{
for( j=0; j<size; j++)
{
if ( car[j]==car[i] )
printf("Original position %d parked in %d\n", car[i], car[j]);
else if ( car[j]!=car[i] )
printf("Original position %d parked in %d\n", close(mincar), car[j]);

move(car[i-1], car[j-1]);
}
}
}

void main()
{
int car[size];
int i=0,j=0,x,y;
x=0,y=0;

while (x != 99)
{
scanf("%d\n", &x);
if (x != 99)
car[i] = x;
}
while (y != 99)
{
scanf("%d", &y);
if (y != 99)
car[i] = y;
}
park(car[i-1],car[j-1]);
}

Apr 23 '06 #5

P: n/a

"S0UL" <so*******@yahoo.com.hk> 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 <stdio.h>
#define size 20

/*closest car*/
int close( int car[] )
{
int min=20,i,j,mincar;
for (i=0; i<size; i++)
{
for (j=0; j<size; j++)
{
if ( min > (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<size; i++ )
{
for( j=0; j<size; j++)
{
if ( car[j]==car[i] )
printf("Original position %d parked in %d\n", car[i], car[j]);
else if ( car[j]!=car[i] )
printf("Original position %d parked in %d\n", close(mincar), car[j]);

move(car[i-1], car[j-1]);
}
}
}

void main()
{
int car[size];
int i=0,j=0,x,y;
x=0,y=0;

while (x != 99)
{
scanf("%d\n", &x);
if (x != 99)
car[i] = x;
}
What does it mean to have a value in car[i]? You don't provide any initial
values for car except the ones you read from a file. And that is a small
number compared to 20.
while (y != 99)
{
scanf("%d", &y);
if (y != 99)
car[i] = y;
}
You read the first data set, did nothing whatsoever and now read a second
data set overlaying some of the values if the first data set. Why bother
with the first read?
park(car[i-1],car[j-1]);
}


There was a strong suggestion in my earlier post that you needed something
like this: (Pidgin C)

struct Car
{
int ordinal number in list 1
int initial slot queued for
int current slot queued for
}

And then:

struct Car car[50] (to emphasize the fact that there are not 20 cars. There
are 20 *spaces*)
Apr 23 '06 #6

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.

--

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

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.