Stuck on algorithm...

Page 1 of 1

7 Replies - 164 Views - Last Post: 13 October 2013 - 10:48 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=331354&amp;s=169a557110bd57cfc9c17e7aab2710bf&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 ZenOne

Reputation: 0
• Posts: 21
• Joined: 15-September 13

Stuck on algorithm...

Posted 12 October 2013 - 08:36 AM

My homework assignment is the following:

 Display a welcome message
 Ask the user for the metro ID number
 Ask the user for 5 different station names
 At each station, using a random number generator (explanation on how to do this on page 4 of this handout) generate the number of people who will disembark the train, and the number of people who are waiting to embark on the train. Remember:
1 If the metro train is at the 1st station in any given direction, there will only be people waiting to get on as the train is empty.
2 If the metro train is at the last station, all passengers must get off and no one can get on until the train turns around.
3 Any other station, there are people waiting to get on (a max of 300), and some people will get off the train (can’t have more people getting off than there are people on the train).
4 If the number of places available on the train, after people leave is less than the number of people waiting to get on, some people will be left waiting on the platform for the next train.
Display the number of people who got off the train, who got on the train, and are left waiting on the platform, and how many people are on the metro before it departs for the next station. Include the station name and metro train id.
 Ask the user if they would like to continue following the metro
1 If yes, continue to next station and repeat the process
2 If no, end with a closing message.

Now, I don't want anyone to give me the answer but I do have 2 questions.

(1) How do I ensure that the random number generator does not give me numbers that produce a negative value? We never covered RNG in class.

(2) I think I understand how to code the algorithm for stations 2-4, however, I am stumped as to how to start with the first one and making the last one become the first one again (I hope that makes sense).

This post has been edited by ZenOne: 12 October 2013 - 08:37 AM

Is This A Good Question/Topic? 0

Replies To: Stuck on algorithm...

#2 GregBrannon

• D.I.C Lover

Reputation: 2174
• Posts: 5,178
• Joined: 10-September 10

Re: Stuck on algorithm...

Posted 12 October 2013 - 09:08 AM

1 - what is the explanation of the random number generator provided on page 4 of the handout?

2 - the train starts empty and finishes empty. not sure what you don't understand.

#3 Mylo

• Knows all, except most.

Reputation: 265
• Posts: 747
• Joined: 11-October 11

Re: Stuck on algorithm...

Posted 12 October 2013 - 09:09 AM

Quote

How do I ensure that the random number generator does not give me numbers that produce a negative value? We never covered RNG in class.

If you are referring to Math.random(), then that will only return a positive value of range [0, 1). If you are using a provided class, you may have to do something different. Generally this might involve adding to your result the most negative a number can be. i.e. if it can produce down to -5, then add 5 to your result to make the minimum 0.

Quote

I think I understand how to code the algorithm for stations 2-4, however, I am stumped as to how to start with the first one and making the last one become the first one again (I hope that makes sense)

That didn't make sense to me, from what I gather, you needn't do anything with the train at the last station.

This post has been edited by Mylo: 12 October 2013 - 09:10 AM

#4 ZenOne

Reputation: 0
• Posts: 21
• Joined: 15-September 13

Re: Stuck on algorithm...

Posted 13 October 2013 - 05:12 AM

GregBrannon, on 12 October 2013 - 09:08 AM, said:

1 - what is the explanation of the random number generator provided on page 4 of the handout?

2 - the train starts empty and finishes empty. not sure what you don't understand.

The part I don't understand is how to make the cycle start over but in the opposite direction. It's supposed to be a never-ending cycle--at least until the user says NO. Also, we are only allowed to use while, do-while and if-else.

#5 GregBrannon

• D.I.C Lover

Reputation: 2174
• Posts: 5,178
• Joined: 10-September 10

Re: Stuck on algorithm...

Posted 13 October 2013 - 05:46 AM

A loop can be constructed that can visit the stations from 1 to 4 and then switch directions and visit then from 4 to 1. You didn't mention that you are allowed to use a for loop, but a general for loop could be written for either direction that looks like:

for ( int i = countStart ; i != countEnd ; i += countChange )

Where countStart, countEnd, and countChange vary to count up or count down. If you're not allowed to use a for loop, the same construction can be made of any of the other loops.

#6 ZenOne

Reputation: 0
• Posts: 21
• Joined: 15-September 13

Re: Stuck on algorithm...

Posted 13 October 2013 - 05:55 AM

GregBrannon, on 13 October 2013 - 05:46 AM, said:

A loop can be constructed that can visit the stations from 1 to 4 and then switch directions and visit then from 4 to 1. You didn't mention that you are allowed to use a for loop, but a general for loop could be written for either direction that looks like:

for ( int i = countStart ; i != countEnd ; i += countChange )

Where countStart, countEnd, and countChange vary to count up or count down. If you're not allowed to use a for loop, the same construction can be made of any of the other loops.

Oh wow. Didn't think of it that way. Thanks.

Also, we aren't allowed a for loop.

#7 ZenOne

Reputation: 0
• Posts: 21
• Joined: 15-September 13

Re: Stuck on algorithm...

Posted 13 October 2013 - 10:31 AM

Can I make the RNG take the value of the previous variable? For example, I have a variable called passPlat that was the randomly generated number of people waiting at station 1; for station 2 I need the variable passOff (passengers getting off) to be a randomly generated number between 0 and the previous RNG value of passPlat.

Eclipse accepts:

```passOff = (int)Math.random()*(passPlat)
```

However, it never actually generates a number. It always uses the 0 that was the previous value of passOff (because it started at station 1 and had no one getting off).

#8 ZenOne

Reputation: 0
• Posts: 21
• Joined: 15-September 13

Re: Stuck on algorithm...

Posted 13 October 2013 - 10:48 AM

ZenOne, on 13 October 2013 - 10:31 AM, said:

Can I make the RNG take the value of the previous variable? For example, I have a variable called passPlat that was the randomly generated number of people waiting at station 1; for station 2 I need the variable passOff (passengers getting off) to be a randomly generated number between 0 and the previous RNG value of passPlat.

Eclipse accepts:

```passOff = (int)Math.random()*(passPlat)
```

However, it never actually generates a number. It always uses the 0 that was the previous value of passOff (because it started at station 1 and had no one getting off).

Never mind. Figured it out.