Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem A: Not Perfectly Random Jukebox You and your roommate just got an old mi

ID: 3683120 • Letter: P

Question

Problem A: Not Perfectly Random Jukebox

You and your roommate just got an old mini juke box from a friend. You want to program it so that it plays random songs, but never plays the same song twice in a row. Luckily, your roommate, who is also in COP 3223, has written a main function for this program and just needs a tiny bit of help from you. Help your roommate by writing a function that takes in two integer parameters - n, the number of songs in the jukebox and last, the number of the last song played by the jukebox. Your function should generate random integers in between 0 and n-1 and return the first one of these not equal to last. The function prototype is given to you below and you can download the program to add your function to:

// Pre-condition: n > 1, 0 <= last < n

// Post-condition: Returns a random integer in between 0 and

// n-1, not equal to last.

int nextSong(int n, int last);

jukebox-scaffold.c

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

static const char* const SONGS[] = {"See You Again", "Want To Want Me", "Walk The Moon", "Talking Body", "Bad Blood"};
const int TOTAL = 5;

int nextSong(int n, int last);

int main() {

srand(time(0));

int i, numSongs, prev = 0;
printf("How many selections do you want from the jukebox? ");
scanf("%d", &numSongs);

for (i=1; i<=numSongs; i++) {
int curSong = nextSong(TOTAL, prev);
printf("%d. Now playing: %s ", i, SONGS[curSong]);
prev = curSong;
}

return 0;
}

// Pre-condition: n > 1, 0 <= last < n
// Post-condition: Returns a random integer in between 0 and
// n-1, not equal to last.
int nextSong(int n, int last) {

int i;

if(n>1&&last>=0&&last<n)
{
do
{
i = rand()%n;
}while(i==last);
}
// Replace this with your code.
return i;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote