2.16. Activecode ExercisesΒΆ

Answer the following Activecode questions to assess what you have learned in this chapter.

Q1

Question

Fix the code below so that it compiles and runs without errors. Hint: you might need to change the names of some variables.

Example VARS_a1q
1#include <iostream>
2
3int main () {
4    char true = 't';
5    char false = 'F';
6    std::cout << "Program complete.\n";
7}

Answer

Below is one way to fix the program. true and false are keywords, so they cannot be used as variable names.

Example VARS_a1a
1#include <iostream>
2
3int main () {
4    char t = 't';
5    char f = 'F';
6    std::cout << "Program complete.\n";
7}

Q2

Finish the code below so that it prints "I drive a 2014 Buick Regal".

This code does not need to include a main. It is already provided as part of the test.

Example VARS_a2
1#include <string>
2
3std::string my_car;
4my_car = "Buick"

Q3

Question

Fix the code below so that it prints "Cady scored 90% on the exam."

Example VARS_a3q
1#include <iostream>
2
3int main() {
4    // Modify the next line so that Cady = 0.9.
5    int Cady = 3 * 5 * (6 / 100);
6
7    // DO NOT MODIFY ANYTHING BELOW THIS LINE.
8    std::cout << "Cady scored " << Cady * 100 << "% on the exam.";
9}

Answer

Below is one way to fix the program. We want to use doubles so that our result isn't truncated to 0 through integer division.

Example VARS_a3a
1#include <iostream>
2
3int main() {
4    double Cady = (3 * 5) * 6 / 100.0;
5    std::cout << "Cady scored " << Cady * 100 << "% on the exam.";
6}

Q4

Finish the code below so that it shows the correct volume of a sphere. Hint: watch out for integer division.

This code does not need to include a main. It is already provided as part of the test.

Example VARS_a4
1int radius = 5;
2double pi = 3.14159;
3
4// Use these variables and the formula for volume to complete the next line.
5volume =

Q5

Question

Fix the code below so that assigns a its correct value of 'a'. Hint: use character operations!

Example VARS_a5q
 1#include <iostream>
 2
 3int main () {
 4   char a = 's';
 5
 6   // Fix the line below.  Do NOT change the numbers!  Instead,
 7   // change the location of the parentheses.
 8   a = a - 3 * 4 + (1 + 3);
 9
10   // DO NOT MODIFY ANYTHING BELOW THIS LINE.
11   std::cout << a;
12}

Answer

Below is one way to complete the program. There are many creative ways that you could use the order of operations to come up with a complex expression that will bring you to 'a', here is one way.

Example VARS_a5a
1#include <iostream>
2
3int main () {
4   char a = 's';
5   a = a - (3 * (4 + 1) + 3);
6   std::cout << a;
7}

Q6

Finish this program so that it assigns "apples" to the variable oranges, and "oranges" to the variable apples, then swaps their values. Be sure to inclue any necessary headers.

Avoid 'hardcoding' your solution.

Example VARS_a6
1int main () {
2
3    // DO NOT MODIFY ANYTHING BELOW THIS LINE.
4    cout << "Your solution had apples = " << apples
5         << " and oranges = " << oranges << ".\n";
6    cout << "The correct solution has apples = apples, and oranges = oranges.";
7}

Q7

Question

Write code that prints "Live", "Laugh", and "Love" on 3 consecutive lines. Be sure to inclue any necessary headers.

Example VARS_a7q
1int main () {
2
3}

Answer

Below is one way to implement the solution.

Example VARS_a7a
1#include <iostream>
2
3int main () {
4    std::cout << "Live\n_laugh\n_love";
5}

Q8

Write code that calculates how much you you will spend after tipping 20% on your $36.25 dinner. Store the tip in a variable tip. Save the final result of this calculation in plus_tip.

This code does not need to include a main. It is already provided as part of the test.

Example VARS_a8
1tip = 0;
2plus_tip = 0;

Q9

Question

You have about three hours and fifteen minutes of homework to do today. Rather than starting it right away, you choose to procrastinate by calculating how many seconds you'll be spending on your work. Convert the time to seconds and store the result in seconds.

This code does not need to include a main. It is already provided as part of the test.

Example VARS_a9q
1seconds = 0;

Answer

Below is one way to implement the solution.

Example VARS_a9a
1int hours = 3;
2int minutes = 15;
3int total_minutes = minutes + 60 * hours;
4int seconds = total_minutes * 60;

Q10

Write code that calculates and prints the average of a and b if a = 3.14, and b = 1.59. You may only use one line of code. Be sure to inclue any necessary headers.

Example VARS_a10
1int main () {
2
3    // DO NOT MODIFY ANYTHING BELOW THIS LINE.
4    std::cout << "\n_your program should have printed 2.365\n";
5}