The Bus Ride

Your bus ride to school takes 15 minutes, and that's in a new variable, rideMinutes. When you get on, the playlist starts with the first song and plays the songs in order. Which of them will you hear to the end before you get off?

Print a heading, Songs for the ride:, and below it, the songs that finish before the bus arrives. A song that ends just as the bus arrives counts too. The first song that doesn't finish in time ends the list: you get off in the middle of it, so the songs after it don't play at all, even if they are short. Finally, print how much of the ride is left after the last song on the list. For our 15-minute ride, the output ends like this:

Songs over 4 minutes: 3
Songs for the ride:
Pixel Heart
Rainy Tuesday
Dancing Robots
Time left: 2:09

The first three songs play 771 seconds, which is 12 minutes and 51 seconds. Moon Bike would end after 958 seconds, but the ride takes only 900, so the list ends with Dancing Robots, and 129 seconds, 2:09, are left.

A longer ride may fit the whole playlist, and then all the songs are on the list. Make sure that your program doesn't read past the end of the vectors then.

That's the whole project. Your program goes through the playlist, adds up the lengths, finds the longest song, counts the long ones and plans your ride, all with vectors, loops and conditions. Did you notice that it prints a time in minutes and seconds in several places, with the same code each time? In the next lesson, on functions, you'll learn to give a piece of code a name and use it again whenever you need it. One function could then print any time in minutes and seconds.

Instructions

Print the heading and the songs you'll hear to the end on the way.

Below them, print how much of the ride is left after the last of them.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

3/3