2: Variables and Math Expressions

Continuations

Housekeeping

Announcements

See Announcements link in Canvas to keep up with what is going on. Here are a few for review:

Questions from last class or reading?

Homework Questions?

2.1: Cooperative Quiz

Quiz Part 1: Individual Readiness Assessment

Quiz Part 2: Team Readiness Assessment (15m)

Quiz Appeals

2.2: Variables

CA 2.2: Coding Challenge Questions

Any problems with any of these Coding Challenges? Zybooks sign in

If so, list the numbers in Chat

Exercise 2.2: Variable Assignment Tracing (16m)

Understanding variables and assignment is critical to being able to program but is an easy thing to be confused about. Get over this hurdle and programming will be far easier.

In this exercise we complete a series of dry run exercises where we step through short fragments of code. This is an important activity to reinforce your understanding of variables. The instructor will step through the first exercise with you.

Specifications

  1. Start a text editor and create a file named variables.txt
  2. Open the Variable Assignment Tracing Exercises page.
  3. Click the first problem and follow the steps, filling in the boxes to get the final answers.
  4. After completing a problem and before continuing to the next, compare your answers with other team members.

    Discuss and resolve and discrepancies between answers.

  5. After verifying your answer, record the problem number and final values of all variables in your variables.txt file.

    As an example for problem 1:

    1. Final value of x is 7. Final value of y is 7.
    
  6. Repeat this process for the remaining exercise problems.
  7. After finishing all the exercise problems, answer the Check Understanding questions and record the question number and answer on your paper.

    As an example for question 10a:

    10a. false
    

When finished, please help your team members and then return to the class meeting.

2.3: Math Expressions

CA 2.3: Coding Challenge Questions

Any problems with any of these Coding Challenges? Zybooks sign in

If so, list the numbers in Chat

Exercise 2.3a: Calculator Basic (15m)

Through the miracles of computer science, we will now convert your $500 computer into a $5 calculator! Along the way, we learn how to work with arithmetic using C++.

For this exercise we break into teams. Within the team, work with each other to develop a solution. When the team has finished, Choose one member to show your solution to the class by sharing your screen. The instructor will ask one team to share their solution.

Specifications

  1. Start Repl.it, update the code in the text editor, and then compile and run the starter program to make sure you typed it correctly.
    #include <iostream>
    using namespace std;
    
    int main() {
        // Type your code here
    }
    
  2. Within the curly braces of the main() function, declare two double variables named a and b, and assign them a value of 5 and 2 respectively. For instance:
    double a = 5, b = 2;
    
  3. Add a line of code to display the arithmetic expression (a + b) and then recompile and run the program.
    cout << "a + b = " << a + b << endl;
    

    Notice that the last letter on endl is a lower-case "L", NOT a one. Verify the output of the program looks like this:

    a + b = 7
    

    If you do not see this output, please type a chat message asking for help.

  4. Add three more lines of code like the previous one that computes the expressions: a - b, a * b and a / b. Compile and run your program again and make sure your program now displays the following output:
    a + b = 7
    a - b = 3
    a * b = 10
    a / b = 2.5
    
  5. The order of operations matters in C++ just like it does in algebra. Multiplication and division are performed before addition and subtraction. Add the following two statements to your program:
    cout << "a + b / 2 = " << a + b / 2 << endl;
    cout << "(a + b) / 2 = " << (a + b) / 2 << endl;
    
  6. Compile and run your program again and compare the output. Your program should now display the following output:
    a + b = 7
    a - b = 3
    a * b = 10
    a / b = 2.5
    a + b / 2 = 6
    (a + b) / 2 = 3.5
    

    Note how the output of the two statements is different. You can change the order of operation using parenthesis, just like in algebra. For more information on the order of operations see section: 2.3.2: Arithmetic.

    As you can see, arithmetic in C++ works much like you would expect.

  7. Once satisfied with your code, copy your code into a text editor, save the file as "arithmetic.cpp", and to submit the file to Canvas with the rest of the exercise files for the week.

When finished developing your code click hereClick to show answer to verify. Code need not look exactly the same. After you have completed your own program, reviewing another is often helpful in learning how to improve your programming skills.

Exercise 2.3b: Calculator Deluxe (10m)

In this exercise we use integer division, modulus, and mathematical functions to create a deluxe calculator.

For this exercise we break into teams. Within the team, work with each other to develop a solution. When the team has finished, Choose one member to show your solution to the class by sharing your screen. The instructor will ask one team to share their solution.

Specifications

  1. In Repl.it, start with your arithmetic.cpp code from the last Exercise.
  2. Integer Division: Modify your arithmetic.cpp code from the last exercise by changing the data type of the two variables from double to int, like this:
    int a = 5, b = 2;
    
  3. Compile and run your program again and compare the output. Note how the result of the division operation changed. What happened to the decimal part of the result?

    In programming terms, we say that the decimal part is truncated (cut short). We have to watch out for this in C++ programming or we may get unexpected results in our calculations.

  4. Modulus (%) operator: Sometimes we want the integer remainder from an integer division. To see the integer remainder, we use the modulus (%) operator. Add the following statements after the other cout statements:
    cout << "a % b = " << a % b << endl;
    cout << "a / b % b = " << a / b % b << endl;
    cout << "a / (b * b) = " << a / (b * b) << endl;
    
  5. Compile and run your program again with this added statement. Your program should now display the following output:
    a + b = 7
    a - b = 3
    a * b = 10
    a / b = 2
    a + b / 2 = 6
    (a + b) / 2 = 3
    a % b = 1
    a / b % b = 0
    a / (b * b) = 1
    
  6. Mathematical functions: More complex mathematical operations require the use of a function in C++. One such function is sqrt(number) which calculates the square root of the number inside the parenthesis.
  7. Add the following statement to your program:
    cout << "sqrt(a + b) = " << sqrt(a + b) << endl;
    
  8. Your program will not compile with this new statement because you must include a library of the mathematical functions. Add the statement: #include <cmath> to the top of your program like this:
    #include <iostream>
    #include <cmath> // math function library
    using namespace std;
    
  9. Compile and run your program again with this added statement. Your program should now compile and display the following output when run:
    a + b = 7
    a - b = 3
    a * b = 10
    a / b = 2
    a + b / 2 = 6
    (a + b) / 2 = 3
    a % b = 1
    a / b % b = 0
    a / (b * b) = 1
    sqrt(a + b) = 2.64575
    
  10. Once satisfied with your code, copy your code into a text editor, save the file as "calculator.cpp", and to submit the file to Canvas with the rest of the exercise files for the week.

When finished developing your code click hereClick to show answer to verify. Code need not look exactly the same. After you have completed your own program, reviewing another is often helpful in learning how to improve your programming skills.