9.13. Mixed Up Code Practice¶
Q1
Let's write the code for the struct definition of movie. The movie structure will have the instance variables title, director, and release_year in that order. Put the necessary blocks of code in the correct order.
-
) #distractor -
char title; #distractor -
int release_year; -
string director; -
string release_year; #distractor -
string title; -
struct movie ( #distractor -
struct movie { -
struct movie { #distractor -
} #distractor -
};
Q2
Let's write the code for the print_movie function. print_movie should print the information about a movie in the following format: "title" directed by director (release_year). Put the necessary blocks of code in the correct order.
-
cout << """ << m.title << "" directed by "; #distractor -
cout << '"' << m.title << "\" directed by "; -
cout << '"' << title << "\" directed by "; #distractor -
cout << director << " (" << release_year << ')' << '\n'; #distractor -
cout << m.director << " (" << m.release_year << ')' << '\n'; -
cout << title << director << release_year; #distractor -
movie print_movie (movie m) { -
void print_movie (&movie m const) { -
void print_movie (const movie& m) { -
}
Q3
Let's write the code for the movie_age function. movie_age should take a movie and current_year as a parameter and return how many years it has been since the release_year. Put the necessary blocks of code in the correct order.
-
int movie_age (const movie& m, int current_year) { -
return current_year - m.release_year; -
return current_year - release_year; #distractor -
return m.release_year - current_year; #distractor; -
void movie_age (const movie& m, int current_year) { #distractor -
}
Q4
Let's write the code for the struct definition of date. The date structure will have three integer instance variables: day, month, and year in that order. Put the necessary blocks of code in the correct order.
-
) #distractor -
int day; -
int month; -
int year; -
string day; #distractor -
string month; #distractor -
string year; #distractor -
struct date ( #distractor -
struct date { -
struct date { #distractor -
} #distractor -
};
Q5
Let's write the code for the print_date function. print_date should print the date in the following format: month/date/year. Put the necessary blocks of code in the correct order.
-
cout << d.day << '/' << d.month << '/' << d.year << '\n'; #distractor -
cout << d.month << '/' << d.day << '/' << d.year << '\n'; -
cout << month << '/' << day << '/' << year << '\n'; #distractor -
date print_date (date d) { -
void print_date (&date d) { -
void print_date (const date& d) { -
}
Q6
Let's write the code for the next_month function. next_month should change the date to one month later. For example, 3/4/2020 gets modified to 4/4/2020, and 12/3/2020 gets modified to 1/3/2021. Put the necessary blocks of code in the correct order.
-
d.month = 1; -
d.month++; -
d.year = 1; -
d.year++; -
date next_month (date& d) { -
else { -
if (d.month = 12) { #distractor -
if (d.month == 1) { #distractor -
if (d.month == 12) { -
void next_month (const date d) { -
void next_month (date& d) { -
} -
} -
}
Q7
Let's write the code for the struct definition of length. length should have the instance variables inches, feet, and yard. Put the necessary blocks of code in the correct order.
-
) #distractor -
double feet; -
double inches; -
double yards; -
int inches; #distractor -
int yards; #distractor -
string feet; #distractor -
struct length ( #distractor -
struct length { -
struct length { #distractor -
} #distractor -
};
Q8
Let's write the code for the print_length function. print_length should print the date in the following format: yards yds, feet ft, inches in. Put the necessary blocks of code in the correct order.
-
cout << l.inches << " in, " << l.feet << " feet, " << l.yards << " yds" '\n'; #distractor -
cout << l.yards << " yds, " << l.feet << " feet, " << l.inches << " in" '\n'; -
cout << yards << " yds, " << feet << " feet, " << inches << " in" '\n'; #distractor -
string print_length (length l) { -
void print_length (const length& l) { -
void print_length (length l) { -
}
Q9
Let's write the code for the all_inches function. print_length should modify a length object to convert all feet and yards to inches. For example, a length with 1 yard, 2 feet, and 3 inches is converted into a length with 0 yards, 0 feet, and 63 inches. Put the necessary blocks of code in the correct order.
-
double yard_to_inches = 36 * yards; #distractor -
int feet_to_inches = 12 * l.feet; #distractor -
l.inches += 36 * l.yards + 12 * l.feet; -
l.inches = 36 * l.yards + 12 * l.feet; #distractor -
length all_inches (length l const) { -
void all_inches (const length l) { -
void all_inches (length& l) { -
}
Q10
Let's write the code for the add_lengths function. add_lengths should take three Lengths as parameters. It should then add the first two Lengths and store the result in the third length. If there is over 12 inches or over 3 feet, convert it to the proper amound of feet and yards (13 inches becomes 1 foot and 1 inch). Put the necessary blocks of code in the correct order.
-
} } -
if (total.feet >= 3) { -
if (total.inches >= 12) { -
int add_feet = total.inches % 12; -
int add_yards = total.feet % 3; -
length add_lengths (length& first, length& second, length& total) { -
total.feet += add_feet; -
total.feet = first.feet + second.feet; -
total.feet = total.feet - add_yards * 3; -
total.inches = first.inches + second.inches; -
total.inches = total.inches - add_feet * 12; -
total.yards += add_yards; -
total.yards = first.yards + second.yards; -
void add_lengths (const length& first, const length& second, length& total) { -
void add_lengths (length first, length second, length total) { -
void add_lengths (length& first, length& second, const length& total) { -
}