Consider the following questions about structures a 5 Points


Consider the following questions about structures. (a) (5 Points) Declare a struct called song that stores a song\'s title, artist, year, genre, and the cost (of either a CD or a tape). Use 2. Oldies. Use a union to allow the cost to be for either a CD or tape. Use an additional enumeration to keep track of what media type the cost is for. (b) (2 Points) Declare the variable songPtr that points to a song structure. Also, allocate memory for the structure. (c) (3 Points) Initialize the structure pointed to by songPtr to have the title Jaded\", the artist \"Aerosmith\", the release year 2001, the genre Rock, and the cost to $17.99 for a CD. Don\'t forget to set the enumeration variables.

Solution

struct Song{ // Struct for song

string title;

string artist;

int year;

enum genre{Rock, Pop, Country, Oldies};

union media{

float cost;

enum type{CD, tape};

}music;

};

int main() {

struct Song *songPtr = malloc(sizeof (struct Song));

songPtr->title=\"Jaded\";

songPtr->artist=\"Aerosmith\";

songPtr->year=2001;

songPtr->genre=Rock;

songPtr->music.cost=17.99;

songPtr->music.type=CD;

cout<<songPtr->title;

}


Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site