Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Explain each step of the code step by step QUESTION 1 0001 0002 #include 0003 0004 #define NAME_LEN 20 0005 #define MAX_ITEMS 3 0006 0007

Explain each step of the code step by step QUESTION 1

0001 0002 #include 0003 0004 #define NAME_LEN 20 0005 #define MAX_ITEMS 3 0006 0007 struct Song 0008 { 0009 int songID; 0010 int artistID; 0011 char name[NAME_LEN + 1]; 0012 int durationSec; 0013 }; 0014 0015 struct Album 0016 { 0017 int albumID; 0018 char name[NAME_LEN + 1]; 0019 struct Song songs[MAX_ITEMS]; 0020 }; 0021 0022 void displayDuration(int duration) 0023 { 0024 printf("%02d:%02d", duration / 60, duration % 60); 0025 } 0026 0027 int searchArtists(const struct Song* song, const int artists[]) 0028 { 0029 int i, duration = 0; 0030 0031 for (i = 0; !duration && i < MAX_ITEMS; i++) 0032 { 0033 if (song->artistID == artists[i]) 0034 { 0035 displayDuration(song->durationSec); 0036 printf(" -> %03d %03d %s ", 0037 song->songID, song->artistID, song->name); 0038 duration = song->durationSec; 0039 } 0040 } 0041 0042 return duration; 0043 } 0044 0045 int searchSongs(const struct Album* album, const int artists[]) 0046 { 0047 int i, duration = 0; 0048 0049 printf("Album: %s ", album->name); 0050 0051 for (i = 0; i < MAX_ITEMS; i++) 0052 { 0053 duration += searchArtists(&album->songs[i], artists); 0054 } 0055 0056 return duration; 0057 } 0058 0059 int main(void) 0060 { 0061 0062 struct Album album = { 11111, "Album Name", { 0063 {100, 174, "Song1", 432 }, 0064 {200, 632, "Song2", 263 }, 0065 {300, 241, "Song3", 311 } } }; 0066 0067 0068 const int artistIDs[MAX_ITEMS] = { 241, 958, 632 }; 0069 0070 int time = searchSongs(&album, artistIDs); 0071 0072 printf("Total Time: "); 0073 displayDuration(time); 0074 putchar(' '); 0075 0076 return 0; 0077 } 0078 }

For the toolbar, press ALT+F10 (PC) or ALT+FN+F10 (Mac).

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions