Web & Social Media Analytics Previous Year Question Paper.pdf
Problem 2 struct to hold information about a song struct So.pdf
1. // Problem 2
// struct to hold information about a song
struct Song
{
int id;
char name[MAX_SONG_NAME_LENGTH];
char singer[MAX_SINGER_NAME_LENGTH];
genreType genre;
int year;
struct Song* next;
};
// Function declarations
struct Song* createSong(int id, char* name, char* singer, char* genre, int year);
void printPlaylist(struct Song* playlist);
void add_song(struct Song** playlist, struct Song* newSong);
struct Song* search_song(struct Song* playlist, char* name);
int edit_song(struct Song* playlist, char* name, char* singer, char* genre, int year);
int delete_song(struct Song* playlist, char* name);
// function to create a new song
struct Song* createSong(int id, char* name, char* singer, char* genre, int year)
{
struct Song* newSong = (struct Song*)malloc(sizeof(struct Song));
newSong->id = id;
strcpy(newSong->name, name);
strcpy(newSong->singer, singer);
if (strcmp(genre, "Pop") == 0)
newSong->genre = 1;
else if (strcmp(genre, "Rock") == 0)
newSong->genre = 2;
else if (strcmp(genre, "Reggae") == 0)
newSong->genre = 3;
else if (strcmp(genre, "Country") == 0)
newSong->genre = 4;
else if (strcmp(genre, "Blues") == 0)
newSong->genre = 5;
2. else if (strcmp(genre, "Balad") == 0)
newSong->genre = 6;
else
newSong->genre = 0;
newSong->year = year;
newSong->next = NULL;
return newSong;
}
// function to print the playlist
void printPlaylist(struct Song* playlist)
{
struct Song* current = playlist;
while (current != NULL)
{
printf("%dt%st%st%dt%dn", current->id, current->name, current->singer, current-
>genre, current->year);
current = current->next;
}
}
// function to add a song to the end of the playlist
void add_song(struct Song** playlist, struct Song* newSong)
{
// TODO: Implement function
}
// function to search for a song by name
struct Song* search_song(struct Song* playlist, char* name)
{
// TODO: Implement function
}
// function to edit a song by name
int edit_song(struct Song* playlist, char* name, char* singer, char* genre, int year)
{
// TODO: Implement function
}
// function to delete a song by name
int delete_song(struct Song* playlist, char* name)
3. {
// TODO: Implement function
}
void flushSngIn()
{
char c;
do
c = getchar();
while (c != 'n' && c != EOF);
}
int main()
{
// Problem 1
int choice = 0;
int songCount = 0;
char songName_input[MAX_SONG_NAME_LENGTH],
singerName_input[MAX_SINGER_NAME_LENGTH];
char genre_input[20];
unsigned int year_input, add_result = 0;
struct musicRepository music[20];
struct musicRepository* song = NULL;
initializeRepository(music, 20);
music[0].ID = numSongs + 1; numSongs++;
strcpy(music[0].songName, "Shape of You");
strcpy(music[0].singerName, "Ed Sheeran");
music[0].genre = 1;
music[0].year = 2017;
songCount++;
music[1].ID = numSongs + 1; numSongs++;
strcpy(music[1].songName, "Despacito");
strcpy(music[1].singerName, "Luis Fonsi");
music[1].genre = 1;
music[1].year = 2017;
songCount++;
music[2].ID = numSongs + 1; numSongs++;
strcpy(music[2].songName, "Uptown Funk");
4. strcpy(music[2].singerName, "Mark Ronson ft. Bruno Mars");
music[2].genre = 1;
music[2].year = 2014;
songCount++;
printf("Number of Songs: %d", songCount);
do
{
printf("n");
printf("1. Add a song to the repositoryn");
printf("2. Search song by namen");
printf("3. Print a songn");
printf("4. Edit a songn");
printf("5. Delete a songn");
printf("6. Print full list of songsn");
printf("7. Exitn");
printf("Enter your choice: ");
scanf("%d", &choice);
flushSngIn();
switch (choice)
{
case 1:
printf("nEnter song name: ");
fgets(songName_input, sizeof(songName_input), stdin);
songName_input[strlen(songName_input) - 1] = '0';
// flushSngIn();
// printf("Song name: %sn", songName_input);
printf("Enter singer name: ");
fgets(singerName_input, sizeof(singerName_input), stdin);
singerName_input[strlen(singerName_input) - 1] = '0';
// flushSngIn();
printf("Enter genre ( Pop , Rock , Reggae , Country , Blues , Balad or unclassified ):");
fgets(genre_input, sizeof(genre_input), stdin);
genre_input[strlen(genre_input) - 1] = '0';
// flushSngIn();
printf("Enter release year: ");
scanf("%d", &year_input);
5. flushSngIn();
add_result = addSong(music, songName_input, singerName_input, genre_input,
year_input, songCount);
if (add_result == 1)
{
printf("Song added successfullyn");
songCount++;
// printf("Number of Songs: %d", songCount);
// flushSngIn();
printRepository(music, songCount);
break;
}
case 2:
printf("Enter song name: ");
fgets(songName_input, sizeof(songName_input), stdin);
songName_input[strlen(songName_input) - 1] = '0';
flushSngIn();
song = searchSong(music, songName_input, songCount);
printf("Song found: %s, %s, %d, %d", song->songName, song->singerName, song-
>genre, song->year);
flushSngIn();
break;
case 3:
if (songCount > 0)
{
int songIndex;
printf("Enter the ID of the song to print (0 to %d): ", songCount - 1);
scanf("%d", &songIndex);
printSong(music, songIndex);
flushSngIn();
break;
}
case 4:
printf("Enter song name: ");
fgets(songName_input, sizeof(songName_input), stdin);
songName_input[strlen(songName_input) - 1] = '0';
6. flushSngIn();
editSong(music, songCount, songName_input);
break;
case 5:
printf("Enter song name: ");
fgets(songName_input, sizeof(songName_input), stdin);
songName_input[strlen(songName_input) - 1] = '0';
flushSngIn();
deleteSongByName(music, songName_input, songCount);
songCount--;
printRepository(music, songCount);
break;
case 6:
printRepository(music, songCount);
break;
case 7:
printf("Exiting the programn");
break;
default:
printf("Invalid choicen");
break;
}
} while (choice != 7);
********************************************
Please help me to finish this code (//TO DO) + Problem.