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

I need help simplifying this code: This initial version of the program relies on

ID: 3667497 • Letter: I

Question

I need help simplifying this code:

This initial version of the program relies on treating each attribute of a song as a distinct, separate entity. Look at songcollection.h, both the structure and the parameter lists of the functions declared there, to see how awkward this gets.

Rewrite songcollection.h and songcollection.cpp to take advantage of the new Song type. You will need to replace the multiple arrays currently in a SongCollection by a single array of Songs. Then rewrite the functions for manipulating song collections accordingly. (Hopefully, you will see that this simplifies the code overall.)

songcollection.h:

#ifndef SONGCOLLECTION_H

#define SONGCOLLECTION_H

#include <iostream>

#include <string>

#include "playlistitem.h"

#include "song.h"

#include "time.h"

const int MAXSONGS = 1000;

struct SongCollection {

int numSongs;

std::string titles[MAXSONGS];

std::string artists[MAXSONGS];

std::string albums[MAXSONGS];

int tracks[MAXSONGS];

int lengths[MAXSONGS];

Time totalTime;

};

/**

* Set up a new collection;

*/

void initialize (SongCollection& collection);

/**

* Add a single song to a collection.

*/

void addSong (SongCollection& toCollection,

       std::string title,

       std::string artist,

       std::string album,

       int track,

       Time length);

/**

* Add into one collection all songs from another collection that

* match a given artist and/or title.

*/

void addMatchesFor (const SongCollection& fromCollection,

      SongCollection& toCollection,

      std::string artist,

      std::string title);

/**

* Print a collection to the indicated output stream */ void print (std::ostream& out, const SongCollection& collection);

/**

* Attempt to read a collection of songs from an input stream,

* adding it to the collection if possible, */ bool read (std::istream& in, SongCollection& collection);

#endif

songcollection.cpp:

#include <iostream>

#include <string>

#include "songcollection.h"

using namespace std;

/**

* Set up a new collection;

*/

void initialize (SongCollection& collection) { collection.numSongs = 0; collection.totalTime = time(0,0,0); }

/**

* Add a single song to a collection.

*/

void addSong (SongCollection& toCollection,

std::string title,

std::string artist,

std::string album,

int track,

Time length)

{

int n = toCollection.numSongs;

toCollection.titles[n] = title;

toCollection.artists[n] = artist;

toCollection.albums[n] = album;

toCollection.tracks[n] = track;

toCollection.lengths[n] = length;

++toCollection.numSongs;

add(toCollection.totalTime, length);

}

/**

* Add into one collection all songs from another collection that

* match a given artist and/or title.

*/

void addMatchesFor (const SongCollection& fromCollection,

      SongCollection& toCollection,

      std::string artist,

      std::string title)

{

for (int i = 0; i < fromCollection.numSongs; ++i)

    {

      if (matches(artist, title,

    fromCollection.artists[i],

    fromCollection.titles[i]))

{

   addSong (toCollection, fromCollection.artists[i],

        fromCollection.titles[i], fromCollection.albums[i],

        fromCollection.tracks[i], fromCollection.lengths[i]); }

    }

}

void print (std::ostream& out, const SongCollection& collection) { for (int i = 0; i < collection.numSongs; ++i)

    {

      print (out, collection.titles[i], collection.artists[i],

      collection.albums[i], collection.tracks[i],

      collection.lengths[i]);

      out << endl;

    }

out << "Total: ";

print(out, collection.totalTime);

out << endl;

}

/**

* Attempt to read a song from an input stream, adding it

* to the collection if possible,

*/

bool read (std::istream& in, SongCollection& collection) { initialize(collection); string title, artist, album; int track; Time length; while (read(in, title, artist, album, track, length))

    {

      addSong(collection, title, artist, album, track, length);

    }

return in;

}

Explanation / Answer

//songcollection.h

#ifndef SONGCOLLECTION_H
#define SONGCOLLECTION_H

#include <iostream>
#include <string>
//#include "playlistitem.h"

//#include "song.h"
#include <time.h>

const int MAXSONGS = 1000;
struct Song
{
std::string title;
std::string artist;
std::string album;
int track;
int length;
};

struct SongCollection {
int numSongs;
struct Song Songs[MAXSONGS];
Time totalTime;
};


/**
* Set up a new collection;
*/
void initialize (SongCollection& collection);


/**
* Add a single song to a collection.
*/
void addSong (SongCollection& toCollection,Song Song);

/**
* Add into one collection all songs from another collection that
* match a given artist and/or title.
*/
void addMatchesFor (const SongCollection& fromCollection,
SongCollection& toCollection,
std::string artist,
std::string title);

/**
* Print a collection to the indicated output stream */
void print (std::ostream& out, const SongCollection& collection);

/**
* Attempt to read a collection of songs from an input stream,
* adding it to the collection if possible, */
bool read (std::istream& in, SongCollection& collection);


#endif

///////////////////////////////////////////////////////////////////////

//songcollection.cpp

#include <iostream>
#include <string>
#include "songcollection.h"


using namespace std;

/**
* Set up a new collection;
*/
void initialize (SongCollection& collection)
{ collection.numSongs = 0;
collection.totalTime = time(0,0,0); }

/**
* Add a single song to a collection.
*/
void addSong (SongCollection& toCollection,
Song song)
{
int n = toCollection.numSongs;
toCollection.Songs[n]=song;
++toCollection.numSongs;
add(toCollection.totalTime, song.length);
}


/**
* Add into one collection all songs from another collection that
* match a given artist and/or title.
*/
void addMatchesFor (const SongCollection& fromCollection,
SongCollection& toCollection,
Song song)
{
for (int i = 0; i < fromCollection.numSongs; ++i)
{
if (matches(song.artist,song.title,
fromCollection.Songs[i].artist,
fromCollection.Songs[i].title))
{
addSong (toCollection, fromCollection.Songs[i]); }
}
}


void print (std::ostream& out, const SongCollection& collection)
{ for (int i = 0; i < collection.numSongs; ++i)
{
print (out, collection);
out << endl;
}
out << "Total: ";
print(out, collection.totalTime);
out << endl;
}


/**
* Attempt to read a song from an input stream, adding it
* to the collection if possible,
*/
bool read (std::istream& in, SongCollection& collection)
{
initialize(collection);
Song song;
while (read(in,song))
{
addSong(collection, song);
}
return in;
}

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