8.14. Mixed Up Code Practice

Q1

Let's write the code for the struct definition of song. The song structure will have the instance variables string title, string artist, string album, and int year in that order. Put the necessary blocks of code in the correct order.

  1. ) #distractor
  2. int year;
  3. string album;
  4. string artist;
  5. string title;
  6. string year;  #distractor
  7. struct song (  #distractor
  8. struct song {
  9. struct song {  #distractor
  10. } #distractor
  11. };

Q2

In main, create a song object called fly which holds the data for Frank Sinatra's "Fly Me to the Moon" from his 1964 album "It Might as Well Be Swing". Put the necessary blocks of code in the correct order.

  1. artist.fly = "Frank Sinatra";  #distractor
  2. fly.album = "It Might as Well Be Swing";
  3. fly.artist = "Frank Sinatra";
  4. fly.title = "Fly Me to the Moon";
  5. fly.year = "1964";  #distractor
  6. fly.year = 1964;
  7. int main() {
  8. song fly;
  9. song fly;
  10. title = "Fly Me to the Moon";  #distractor
  11. }

Q3

Let's write the code for the print_song function. print_song takes a song as a parameter and prints out the instance variables in the following format: "title" by artist (album, year). Put the necessary blocks of code in the correct order.

  1. cout << " (" << album << ", " << year << ")\n";  #distractor
  2. cout << " (" << s.album << ", " << s.year << ")\n";
  3. cout << """ << s.title << "" by " << s.artist;  #distractor
  4. cout << '"' << s.title << "\" by " << s.artist;
  5. cout << '"' << title << "\" by " << artist;  #distractor
  6. cout << title << artist << album << year;  #distractor
  7. struct print_song (song s) {
  8. using std::cout;
  9. void print_song (song s) {
  10. }

Q4

Let's write the code for the struct definition of unicorn. The unicorn structure will have the instance variables name, age, horn_length, hair_color, and is_sparkly in that order. A unicorn's horn length is measured to the nearest tenth of a unit. Put the necessary blocks of code in the correct order.

  1. bool is_sparkly;
  2. double horn_length;
  3. int age;
  4. int horn_length;  #distractor
  5. string hair_color;
  6. string name;
  7. struct unicorn {
  8. Struct Unicorn {  #distractor
  9. } #distractor
  10. };

Q5

Let's write the code for the convert_to_human_age function. convert_to_human_age takes a unicorn as a parameter and returns the equivalent human age. If a unicorn is sparkly, then its equivalent human age is three times its age in unicorn years plus the length of its horn. If a unicorn is not sparkly, then its equivalent human age is four times its age in unicorn years plus twice the length of its horn. Put the necessary blocks of code in the correct order.

  1. else {
  2. if (is_sparkly) {
  3. if (u.is_sparkly) {
  4. int convert_to_human_age (unicorn u) {
  5. int human_years;  #distractor
  6. return 3 * age + horn_length;
  7. return 3 * u.age + u.horn_length;
  8. return 4 * age + 2 * horn_length;  #distractor
  9. return 4 * u.age + 2 * u.horn_length;
  10. void convert_to_human_age (unicorn u) {
  11. }
  12. }
  13. }

Q6

Let's write the code for the unicorn_power function. unicorn_power takes a unicorn as a parameter and sets is_sparkly to true and changes the color to rainbow. Put the necessary blocks of code in the correct order.

  1. u.color = "rainbow";
  2. u.color = rainbow;
  3. u.is_sparkly = true;
  4. u.is_sparkly == true;
  5. void &unicorn_power (unicorn u) {  #distractor
  6. void unicorn_power (unicorn u) {  #distractor
  7. void unicorn_power (unicorn& u) {
  8. }

Q7

Let's write the code for the struct definitions of address and employee. The address structure will have the instance variables house_number, state (abbreviation), and postal_address in that order. The employee structure will be a nested structure with the instance variables name and address address in that order. Put the necessary blocks of code in the correct order, with address defined before employee.

  1. address address;
  2. address;  #distractor
  3. employee employee;  #distractor
  4. int house_number;
  5. int postal_address;
  6. string address;  #distractor
  7. string name;
  8. string state;
  9. struct address {
  10. Struct address {  #distractor
  11. struct employee {
  12. Struct employee {  #distractor
  13. }  #distractor
  14. };
  15. };

Q8

Let's write the code for the print_address function. print_address takes an employee as a parameter and should print out the information of the employee in the following format: name (id) lives at house_number in state, postal_address. Put the necessary blocks of code in the correct order.

  1. cout << e.address.house_number << " in " << e.address.state << ", " << e.address.postal_address << '\n';
  2. cout << e.address.name << " (" << e.address.id << ") lives at ";  #distractor
  3. cout << e.house_number << " in " << e.state << ", " << e.postal_address << '\n';  #distractor
  4. cout << e.name << " (" << e.id << ") lives at ";
  5. cout << e.name << '(' << e.address.id << ") lives at";  #distractor
  6. string print_address (employee& e) {
  7. void print_address (employee e) {
  8. }

Q9

Sometimes employees will move around and thus we'll need to update their addresses. Let's write the code for the update_address function. update_address takes an employee and a new address as parameters and sets the employee's address to the new address. Put the necessary blocks of code in the correct order.

  1. e.address = a;
  2. e.address = address;  #distractor
  3. e.address.house_number = a.house_number;  #distractor
  4. e.address.house_number = a.house_number;  #distractor
  5. e.address.postal_address = a.postal_address;  #distractor
  6. e.address.state = a.state;  #distractor
  7. employee update_address (employee e, address a) {  #distractor
  8. void update_address (employee e, address a) {  #distractor
  9. void update_address (employee e, address& a) {  #distractor
  10. void update_address (employee& e, address a) {
  11. }
  12. };  #distractor

Q10

Let's write the code for the store_employee_data function. store_employee_data doesn't take any parameters and prompts the user for information regarding their name, id, salary, and address in that order. It then returns an employee object with the stored data. Declare all variables before prompting the user. Put the necessary blocks of code in the correct order.

  1. cin >> address.state;
  2. cin >> e.address.house_number;
  3. cin >> e.address.postal_address;
  4. cin >> e.address.state;
  5. cin >> e.house_number;
  6. cin >> e.name;
  7. cout << "What is your full name? ";
  8. cout << "What is your house number? ";
  9. cout << "What is your postal address? ";
  10. cout << "What state do you live in? ";
  11. employee e;
  12. employee store_employee_data () {
  13. employee store_employee_data (employee e) {
  14. getline(cin, e.name);
  15. return e;
  16. return employee e;
  17. void store_employee_data () {
  18. }