Case Study1
Case Study1
Explanation
Key Components
1. Structures:
o struct song: Represents a song with the following attributes:
title: The title of the song.
artist: The artist of the song.
genre: The genre of the song.
duration: The duration of the song in seconds.
o struct playlist: Represents a playlist, containing:
An array of Song structures (songs).
count: The number of songs currently in the playlist.
current_index: The index of the currently playing song.
2. Typedefs:
o typedef struct song Song; and typedef struct playlist Playlist;
provide shorthand references to these structures.
Functions
Main Function
Program
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define GENRE_LENGTH 50
struct song{
char title[TITLE_LENGTH];
char artist[ARTIST_LENGTH];
char genre[GENRE_LENGTH];
int duration;
};
struct playlist{
Song songs[MAX_SONGS];
int count;
int current_index;
};
void add_song(Playlist* playlist, char* title, char* artist, char* genre, int duration);
int main() {
current = play_next(&my_playlist);
printf("\nRock Songs:\n");
display_songs_by_genre(&my_playlist, "Rock");
printf("\nPop Songs:\n");
display_songs_by_genre(&my_playlist, "Pop");
return 0;
void add_song(Playlist* playlist, char* title, char* artist, char* genre, int duration) {
new_song->duration = duration;
playlist->count++;
} else {
playlist->current_index++;
return &playlist->songs[playlist->current_index];
return NULL;
if (playlist->current_index > 0) {
playlist->current_index--;
return &playlist->songs[playlist->current_index];
return NULL;
playlist->current_index = index;
return &playlist->songs[playlist->current_index];
return NULL;
int i;
if (strcmp(playlist->songs[i].genre, genre) == 0) {
playlist->songs[i].title, playlist->songs[i].artist,
playlist->songs[i].duration);
if (playlist->current_index >= 0) {
} else {
Output