Can someone please help me complete the "add_song" function below. Thank you!
// Each song has this information: song's id, song's name, singer's name, genre of the song, and
its published year.
// The struct 'musicRepository' holds information of one song. Genre is enum type.
// An array of structs called 'list' is made to hold the list of songs.
// You should not modify any of the given code, the return types, or the parameters, you will risk of
getting compilation error.
// You are not allowed to modify main ().
// You can use string library functions.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable : 4996) // for Visual Studio Only
#define MAX_SONGS 20
#define MAX_SONG_NAME_LENGTH 40
#define MAX_SINGER_NAME_LENGTH 40
typedef enum
{
unclassified = 0,
Pop,
Rock,
Reggae,
Country,
Blues,
Balad,
} genreType; // enum type
struct musicRepository
{
// struct for song details
unsigned int ID;
char songName[MAX_SONG_NAME_LENGTH];
char singerName[MAX_SINGER_NAME_LENGTH];
genreType genre;
unsigned int year;
};
struct musicRepository list[MAX_SONGS]; // declare the list of songs
int numSongs = 0; // the number of songs currently stored in the list (initialized to 0)
// This function takes in an array of musicRepository structures and the size of the array as
parameters. It then loops through each index of the array and sets the values of id, year, genre,
name, and singer to their respective initial values.
// NOTE that strcpy is used to copy an empty string to name and singer, rather than setting them
equal to "". This is because arrays in C are not assignable, so the strcpy function must be used to
copy the empty string into the array.
void initializeRepository(struct musicRepository repo[], int size)
{
for (int i = 0; i < size; i++)
{
repo[i].ID = 0;
repo[i].year = 0;
repo[i].genre = 0;
strcpy(repo[i].songName, "");
strcpy(repo[i].singerName, "");
}
}
// This function takes in the musicRepository array and iterates through each element. If the ID of
the song is not 0, it prints the details of the song.
void printRepository(struct musicRepository repo[], int numSongs)
{
printf("n--- Song Repository ---n");
for (int i = 0; i < numSongs; i++)
{
if (repo[i].ID != 0)
{
printf("ID: %dn", repo[i].ID);
printf("Name: %sn", repo[i].songName);
printf("Singer: %sn", repo[i].singerName);
if (repo[i].genre == 0)
printf("Genre: unclassifiedn");
else if (repo[i].genre == 1)
printf("Genre: Popn");
else if (repo[i].genre == 2)
printf("Genre: Rockn");
else if (repo[i].genre == 3)
printf("Genre: Reggaen");
else if (repo[i].genre == 4)
printf("Genre: Countryn");
else if (repo[i].genre == 5)
printf("Genre: Bluesn");
else if (repo[i].genre == 6)
printf("Genre: Baladn");
printf("Year: %dn", repo[i].year);
printf("n");
}
}
}
// 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 declarati.
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Can someone please help me complete the add_song function .pdf
1. Can someone please help me complete the "add_song" function below. Thank you!
// Each song has this information: song's id, song's name, singer's name, genre of the song, and
its published year.
// The struct 'musicRepository' holds information of one song. Genre is enum type.
// An array of structs called 'list' is made to hold the list of songs.
// You should not modify any of the given code, the return types, or the parameters, you will risk of
getting compilation error.
// You are not allowed to modify main ().
// You can use string library functions.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable : 4996) // for Visual Studio Only
#define MAX_SONGS 20
#define MAX_SONG_NAME_LENGTH 40
#define MAX_SINGER_NAME_LENGTH 40
typedef enum
{
unclassified = 0,
Pop,
Rock,
Reggae,
Country,
Blues,
Balad,
} genreType; // enum type
struct musicRepository
{
// struct for song details
unsigned int ID;
char songName[MAX_SONG_NAME_LENGTH];
char singerName[MAX_SINGER_NAME_LENGTH];
genreType genre;
unsigned int year;
};
struct musicRepository list[MAX_SONGS]; // declare the list of songs
int numSongs = 0; // the number of songs currently stored in the list (initialized to 0)
// This function takes in an array of musicRepository structures and the size of the array as
parameters. It then loops through each index of the array and sets the values of id, year, genre,
name, and singer to their respective initial values.
// NOTE that strcpy is used to copy an empty string to name and singer, rather than setting them
equal to "". This is because arrays in C are not assignable, so the strcpy function must be used to
2. copy the empty string into the array.
void initializeRepository(struct musicRepository repo[], int size)
{
for (int i = 0; i < size; i++)
{
repo[i].ID = 0;
repo[i].year = 0;
repo[i].genre = 0;
strcpy(repo[i].songName, "");
strcpy(repo[i].singerName, "");
}
}
// This function takes in the musicRepository array and iterates through each element. If the ID of
the song is not 0, it prints the details of the song.
void printRepository(struct musicRepository repo[], int numSongs)
{
printf("n--- Song Repository ---n");
for (int i = 0; i < numSongs; i++)
{
if (repo[i].ID != 0)
{
printf("ID: %dn", repo[i].ID);
printf("Name: %sn", repo[i].songName);
printf("Singer: %sn", repo[i].singerName);
if (repo[i].genre == 0)
printf("Genre: unclassifiedn");
else if (repo[i].genre == 1)
printf("Genre: Popn");
else if (repo[i].genre == 2)
printf("Genre: Rockn");
else if (repo[i].genre == 3)
printf("Genre: Reggaen");
else if (repo[i].genre == 4)
printf("Genre: Countryn");
else if (repo[i].genre == 5)
printf("Genre: Bluesn");
else if (repo[i].genre == 6)
printf("Genre: Baladn");
printf("Year: %dn", repo[i].year);
printf("n");
}
}
3. }
// 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;
else if (strcmp(genre, "Balad") == 0)
newSong->genre = 6;
else
newSong->genre = 0;
newSong->year = year;
newSong->next = NULL;
4. 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
}
void flushSngIn()
{
char c;
do
c = getchar();
while (c != 'n' && c != EOF);
}
// Problem 2
// create some sample songs
struct Song* s1 = createSong(1, "Shape of You", "Ed Sheeran", "Pop", 2017);
struct Song* s2 = createSong(2, "Despacito", "Luis Fonsi", "Pop", 2017);
struct Song* s3 = createSong(3, "Uptown Funk", "Mark Ronson ft. Bruno Mars", "Pop", 2014);
// create the playlist and add the sample songs
struct Song* playList = NULL;
add_song(&playList, s1);
add_song(&playList, s2);
add_song(&playList, s3);
// print the playlist
printf("Initial Playlist:n");
printPlaylist(playList);
// add a new song to the playlist
struct Song* s4 = createSong(4, "Sorry", "Justin Bieber", "Pop", 2015);
printf("nAdding new song to playlist:n");
add_song(&playList, s4);
5. printPlaylist(playList);
// delete a song from the playlist
printf("nDeleting a song from the playlist:n");
struct Song* sToDelete = search_song(playList, "Despacito");
if (sToDelete != NULL)
{
delete_song(playList, sToDelete->name);
printf("Playlist after deletion:n");
printPlaylist(playList);
}
else
{
printf("Song not found in playlist.n");
}
// search for a song that doesn't exist in the playlist
printf("nSearching for a song that doesn't exist in the playlist:n");
struct Song* searchResult = search_song(playList, "Non-existent Song");
if (searchResult != NULL)
{
printf("Found song:n");
printf("Song ID: %d, Song Name: %s, Singer Name: %s, Genre: %s, Year: %d", searchResult->id,
searchResult->name, searchResult->singer, searchResult->genre, searchResult->year);
}
else
{
printf("Song not found in playlist.n");
}
// search for a song that exists in the playlist
printf("nSearching for a song that exists in the playlist:n");
searchResult = search_song(playList, "Uptown Funk");
if (searchResult != NULL)
{
// printf("Found song:n");
// printSong(searchResult);
}
else
{
printf("Song not found in playlist.n");
}
// edit a song in the playlist
printf("nEditing a song in the playlist:n");
int editResult = edit_song(playList, "Shape of You", "Ed Sheeran", "Pop", 2018);
6. if (editResult == -1)
{
printf("Song not found in playlist.n");
}
else
{
printf("Playlist after edit:n");
printPlaylist(playList);
}
return 0;
}