Q:

A CD changer allows you to load more than one CD. Many of these have random buttons, which allow you to play random tracks from a specified CD, or play random tracks from random CDs

0

 A CD changer allows you to load more than one CD. Many of these

have random buttons, which allow you to play random tracks from a 

specified CD, or play random tracks from random CDs. You are to 

simulate a play list from such a CD changer using the randi function. 

The CD changer that we are going to simulate can load 3 different CDs.

You are to assume that three CDs have been loaded. To begin with, the

program should “decide” how many tracks there are on each of the 

three CDs, by generating random integers in the range from MIN to 

MAX. You decide on the values of MIN and MAX (look at some CDs; 

how many tracks do they have? What’s a reasonable range?). The 

program will print the number of tracks on each CD. Next, the program

will ask the user for his or her favorite track; the user must specify 

which track and which CD it’s on. Next, the program will generate a 

“playlist” of the N random tracks that it will play, where N is an integer.

For each of the N songs, the program will first randomly pick one of the

3 CDs, and then randomly pick one of the tracks from that CD. Finally, 

the program will print whether the user’s favorite track was played or 

not. The output from the program will look something like this 

depending on the random integers generated and the user’s input:

There are 15 tracks on CD 1.

There are 22 tracks on CD 2.

There are 13 tracks on CD 3.

What’s your favorite track? 

Please enter the number of the CD: 4

Sorry, that’s not a valid CD.

Please enter the number of the CD: 1

Please enter the track number: 17

Sorry, that’s not a valid track on CD 1.

Please enter the track number: -5

Sorry, that’s not a valid track on CD 1.

Please enter the track number: 11

Play List:

CD 2 Track 20

CD 3 Track 11

CD 3 Track 8

CD 2 Track 1

CD 1 Track 7

CD 3 Track 8

CD 1 Track 3

CD 1 Track 15

CD 3 Track 12

CD 1 Track 6

Sorry, your favorite track was not played.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Ch5Ex42.m

MIN = 7;

MAX = 14;

noCDs = 3;

N = 5;

cdtracks = randi([MIN, MAX], 1,3);

for i = 1:noCDs

 fprintf('There are %d tracks on CD %d\n', cdtracks(i), i)

end

fprintf('\nWhat''s your favorite track?\n')

favcd = input('Please enter the number of the CD: ');

while favcd < 1 || favcd > noCDs

 fprintf('Sorry, that''s not a valid CD.\n')

 favcd = input('Please enter the number of the CD: ');

end

favtrack = input('Please enter the track number: ');

while favtrack < 1 || favtrack > cdtracks(favcd)

 fprintf('Sorry, that''s not a valid track on CD %d\n', ...

 favcd)

 favtrack = input('Please enter the track number: ');

end

played = false;

disp('Play List: ')

for i = 1:N

 rancd = randi([1, noCDs]);

 rantrack = randi([1, cdtracks(rancd)]);

 fprintf('CD %d Track %d\n', rancd, rantrack)

 played = rancd == favcd && rantrack == favtrack;

end

if played

 disp('Your favorite track was played!!')

else

 disp('Your favorite track was not played.')

end

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now