A2-Math and Memory

Table of Contents


Objectives

  • Start practicing C++ commands
  • Start declaring and using numerical variables
  • Obtain and store user input
  • Perform arithmetic operations to solve problems using C++.
  • Make use of modulus operators
  • Work with mathematical functions.
  • Debug errors in your code
  • Write your first complete C++ programs!

Academic Honesty

Read the Scholastic Honesty Policy and Assignment Integrity policies of the syllabus. Here are some clarifications for this particular assignment:

  • You may not show your project work to another person or look at another person's project work until you complete and submit this assignment.
  • You may get help from others if you get stuck, but only if they do not show or tell you what to type.
  • Remember that the instructor performs similarity tests on programming project submissions, and copied or plagiarized code is very easy to detect.

This is a solo assignment.

Preparation

  1. Make sure you have completed the exercises from lesson 2.
  2. Complete the Review Exercises in CodeLab 2. These exercises will help prepare you for the problem-solving program and should be completed first.

Project Specifications

Note that these are solo programming projects and not pair-programming projects. Your solutions to these project must only use techniques we have covered so far.

Programming Style

For all programs, remember to follow all the style rules we covered including:

  1. File block comments.
  2. Placement of curly braces and indentation within curly braces.
  3. Limiting line length to about 80 characters.

    TextPad, and most other text editors, tell you both the line and column location, which lets you check the line length.

  4. Spaces before and after operators.

Image
Image source: Ed Parrish

Project 1: Math Worksheet

Use this worksheet to improve your understanding of how to convert math equations to C++ code. Refer to lesson 2 for more information on C++ maths.

For this worksheet, the user enters three numbers. You write code to display the value of the equations listed below.

Project Specifications
  1. Start by downloading the worksheet: mathwork.cpp.

    Add to the existing code to complete the project. Leave the existing code unchanged, except for comments.

  2. You must name the source code file that you turn in mathwork.cpp and include all your code in this single file.

    Be careful of the spelling, including capitalization, as you will lose points for a misspelled name. Naming is important in programming.

  3. Add your name and the date to the file comment block at the top of the file where shown in the comments.
  4. User input is already coded into the worksheet.

    Do not add any other input commands or change the input order.

  5. Convert each of the following equations to C++ code in the worksheet. The letter `x` represents the expression.
    1. Summation means to add up a sequence of numbers. For expression 1, use the following formula to the sum all the inputs.

      `s um = a + b + c`

      Where `a`, `b` and `c` are the user input.

      Code this equation in the mathwork.cpp program where indicated in the comments. See the Example Run to verify correctness.

    2. For expression 2, calculate the average (arithmetic mean) using the following formula. Code this equation in the mathwork.cpp program where indicated in the comments. See the Example Run to verify correctness.

      `avg=\frac{a + b + c}{3}`

      Where `a`, `b` and `c` are the user input.

    3. For expression 3, calculate the weighted average (wa) of the input using the following formula.

      `rms=\sqrt{\frac{a^{2}+b^{2}+c^{2}}{n}}`

      Where `a`, `b` and `c` are the user input.

      Code this equation in the mathwork.cpp program where indicated in the comments. See the Example Run to verify correctness.

    4. For expression 4, calculate the variance (var) using the following formula.

      `var=\frac{(a-avg)^{2}+(b-avg)^{2}+(c-avg)^{2}}{3}`

      Where `a`, `b` and `c` are the user input.

      Code this equation in the mathwork.cpp program where indicated in the comments. See the Example Run to verify correctness.

    5. For expression 5, calculate the standard deviation (sd) of the formula shown below.

      `sd=\sqrt{\frac{(a-avg)^{2}+(b-avg)^{2}+(c-avg)^{2}}{3}}`

      Where `a`, `b` and `c` are the user input.

      Code this equation in the mathwork.cpp program where indicated in the comments. See the Example Run to verify correctness.

  6. Example Run: The input prompts and outputs of the program must look like the following for full credit, including the same order of input and exact wording of the output. For the input shown you must get the same output. However, the output must change properly if the inputs are different.
    ***Math Worksheet***
    Enter three numbers separated by spaces, and press the Enter key
    For example: 1 2 3[Enter]: 1 2 3
    
    You entered: a = 1, b = 2, c = 3
    Parenthesis show correct results with the example numbers.
    Expression1 (6): 6
    Expression2 (2): 2
    Expression3 (2.125): 2.125
    Expression4 (0.666667): 0.666667
    Expression5 (0.816497): 0.816497
    
    ***Math Worksheet***
    Enter three numbers separated by spaces, and press the Enter key
    For example: 1 2 3[Enter]: 4 6 8
    
    You entered: a = 4, b = 6, c = 8
    Parenthesis show correct results with the example numbers.
    Expression1 (6): 18
    Expression2 (2): 6
    Expression3 (2.125): 6.25
    Expression4 (0.666667): 2.66667
    Expression5 (0.816497): 1.63299
    

    In the above example runs, the user entered the values shown in aqua italics (for emphasis) to produce the output. Your program does NOT print the characters in aqua italics, nor does the user input appear in aqua italics. The values in (parentheses) are expected values when entering the example input of  1 2 3 .

  7. Display the output using the default formatting and precision for the numbers -- do NOT add any formatting statements to the code.
  8. After displaying the output, exit the program.
  9. Submit the source code file mathwork.cpp with the rest of the assignment as described in Deliverables.

Division and modulus operators
Image source: Ed Parrish

Project 2: Applying Division and Modulus

In C++, dividing one integer by another truncates the remainder, which removes the decimal part. To compute the integer remainder, we use the remainder operator (%), also known as the modulus operator.

The % operator finds the remainder after division of one number by another. The C++ expression a % b (pronounced a mod b) returns the remainder of the division of a by b if both numbers are positive. For example:

    3  r 1
2 ) 7
   -6
    1 remainder

For this project we will use both the division and remainder operators to extract single digits form an integer number.

Before starting to code, develop an understanding of the problem and plan as as we discussed in lesson 2.4. You must include both your problem restatement and your plan's algorithm within your C++ source code file (see specification 2).

Project Specifications
  1. Write a program that gets three numbers from the user and then sums:
    1. the hundreds digit of the first number
    2. with the tens digit of the second number and
    3. the unit digit of the third number.
    For example, the sum of the diagonal digits of the following three numbers is 3 because the numbers are all 1 on the diagonal as shown by the highlight.
    123
    416
    781
    

    After reading these specifications, restate the problem in your own words and develop a plan as we discussed in lesson 2.4.

  2. Name the source code file sumdiag.cpp and include all your code in this single file.

    Be careful of the spelling, including capitalization, as you will lose points for a misspelled name. Naming is important in programming.

    When starting your coding effort, restate the problem in the file comment at the top of the file and copy your algorithm into the main function as we discussed in lesson 2.4.4.

  3. Ask the user for the three integer numbers, and no other input, as shown in the Example Run.
  4. Use the division operator (/) and remainer operator (%) to extract digits from the numbers entered by the user.
  5. Example Run: The input prompts and outputs of the program must look like the following for full credit, including the same order of input and exact wording of the output. For the input shown you must get the same output. However, the output must change properly if the inputs are different.
    Enter first number:  123
    Enter second number: 416
    Enter third number:  781
    Sum of diagonal digits is 3
    
    Enter first number:  123
    Enter second number: 456
    Enter third number:  789
    Sum of diagonal digits is 15
    
    Enter first number:  1234
    Enter second number: 56
    Enter third number:  789
    Sum of diagonal digits is 16
    
    Enter first number:  1
    Enter second number: 2
    Enter third number:  3
    Sum of diagonal digits is 3
    

    In the above example run, the user entered the values shown in aqua italics (for emphasis) to produce the output. Your program does NOT print the characters in aqua italics, nor does the user input appear in aqua italics.

    Notice the fourth example. Entering a 1 is the same as 001. Similarly, entering a 2 is the same as entering 002 and entering a 3 is the same as entering 003. Thus arranging the three numbers in a matrix of rows and columns shows us how the answer was derived.

    001
    002
    003
    
  6. After displaying the output, exit the program.
  7. Do not use if-statements, strings or techniques we have not covered.
  8. Submit the source code file sumdiag.cpp with the rest of the assignment as described in Deliverables.
Hints:
References and More Information
  1. Long Division with Remainders
  2. Modulo operation

Solar system
Image source

Project 3: Exploring Other Worlds

The mass of every moon and planet in our solar system differs. Since mass affects our weight [1], how much we weigh will differ on other worlds [2]. Mass also affects how high we can jump on other worlds.

Before starting to code, develop an understanding and plan as as we discussed in lesson 2.4. You must include both your problem restatement and your plan's algorithm within your C++ source code file (see specification 2).

Project Specifications
  1. Write a program that calculates your weight and how high you can jump on the moon and other worlds.

    After reading these specifications, restate the problem in your own words and develop a plan as we discussed in lesson 2.4.

  2. You must name the source code file worlds.cpp and include all your code in this single file.

    Be careful of the spelling, including capitalization, as you will lose points for a misspelled name. Naming is important in programming.

    When starting your coding effort, restate the problem in the file comment at the top of the file and copy your algorithm into the main function as we discussed in lesson 2.4.4.

  3. Ask the user for the following inputs (and no other input) in this order, as shown in the Example Run below:
    1. What do you weigh (in pounds) on Earth?
    2. How high can you jump (in feet) on Earth?
  4. Use the following table of values to calculate the weight and jump height on the moon and other worlds:
    Name Gravity Relative to Earth
    Moon 0.166
    Mercury 0.378
    Venus 0.907
    Mars 0.377
    Jupiter 2.364

    Source: NASA [3]

    The gravity numbers in the table are relative to Earth's gravity. So, if you weigh 110 lbs on Earth you will weigh 18.15 lbs (110 lbs * 0.166) on the Moon. Similarly, if you can jump 2.5 feet high on Earth, you can jump 15.2 feet (2.5 feet / 0.166) on the Moon.

  5. Example Run: The input prompts and outputs of the program must look like the following for full credit, including the same order of input and exact wording of the output. For the input shown you must get the same output. However, the output must change properly if the inputs are different.
    Planetary Exploration
    
    What do you weigh (in pounds) on Earth? 110
    How high can you jump (in feet) on Earth? 2
    
    Name    Weight  Jump Height
    ----    ------  -----------
    Moon    18.26   12.0482
    Mercury 41.58   5.29101
    Venus   99.77   2.20507
    Mars    41.47   5.30504
    Jupiter 260.04  0.846024
    
    Planetary Exploration
    
    What do you weigh (in pounds) on Earth? 175.5
    How high can you jump (in feet) on Earth? 2.1
    
    Name    Weight  Jump Height
    ----    ------  -----------
    Moon    29.133  12.6506
    Mercury 66.339  5.55556
    Venus   159.179 2.31533
    Mars    66.1635 5.57029
    Jupiter 414.882 0.888325
    

    In the above example run, the user entered the values shown in aqua italics (for emphasis) to produce the output. Your program does NOT print the characters in aqua italics, nor does the user input appear in aqua italics.

  6. The output must line up nicely as shown in the Example Run. Print "\t" (inside quotes) before the gap shown in the Example Run to line up the output.
  7. Use the default formatting and precision for the numbers -- do NOT add any numerical formatting statements to the code.
  8. After displaying the output, exit the program.
  9. Submit this project with the rest of the assignment as described in Deliverables.
References and More Information
  1. Newton's law of universal gravitation: Wikipedia
  2. Your Weight On Other Worlds: Exploratorium
  3. Planetary Fact Sheet - Ratio to Earth Values: NASA

Image
Image source

Project 4: Pair Programming Worksheet

Pair programming is where two programmers work together at one computer to develop code projects. One, the driver, writes code while the other, the observer or navigator,[1] reviews each line of code as it is typed in. The two programmers switch roles frequently [1]. Students generally have higher confidence in their work and perform better when pair programming [2]. These effects were tested at Cabrillo College among other places [3].

Programming can be beneficial, but following certain guidelines is important to maximize success [4][5]. This project introduces you to the concepts and best practices of pair programming for our course programming projects.

Project Specifications
  1. Watch the video Introduction to Pair Programming, a professionally developed video (10 minutes).
  2. Save this Pair Programming Worksheet following the menu File > Download as > Plain Text (.txt), and save the file as pairprogramming.txt.
  3. Fill out and answer the questions in pairprogramming.txt without deleting any of the existing text.

    Provide thoughtful answers for full credit.

  4. Submit the pairprogramming.txt file with the rest of the assignment as described in Deliverables.
  5. For reference, here is a link to the The Rules of Pair Programming
References and More Information
  1. Pair programming: Wikipedia article
  2. The effects of pair-programming on performance in an introductory programming course.
  3. Pair Programming (Case Study 1)
  4. The Rules of Pair Programming
  5. All I Really Need to Know about Pair Programming I Learned in Kindergarten: Good suggestions on how to program in pairs.

Extra Credit

Completing the following is worth the extra credit points shown in parenthesis.

  1. Add an additional interesting and useful math equation derived from mathworks.cpp making use of all three of the variables and a math function. (1 point for completing with a math function and 1 point for interest and usefulness.)
    1. Submit the source code with the extra math equation in a file named xcmathwork.cpp.
    2. Describe what the equation is used for in your README.txt file.
    3. Include the data input section of the original mathwork.cpp file.
    4. Follow the format of the previous equations by assigning the solution to a unique variable.
    5. Follow the output of the previous equations by labeling the equation with a cout statement displaying the label Equation6 followed by the value assigned to the unique variable when entering the example values 1 2 3 and then the equation result itself, like:
      Equation6 (42): 42
      Equation6 (42): 210
  2. Look up constant variables in the textbook (p.39) and declare and use constants integers in sumdiag.cpp for all numbers in equations. (1 point)

    Look up magic numbers in the textbook (p.39) or on the internet to understand the solution to magic numbers.

  3. Make use of named constants in worlds.cpp so the program does not use any magic numbers. (1 point)

    Look up magic numbers in the textbook (p.39) or on the internet to understand the solution to magic numbers.

Make sure to list the extra credit you complete in the README.txt file.

Tutorial Lab

In preparation for next weeks lessons, complete the following:

  1. Read the assigned reading in the textbook
  2. Complete the Tutorial Exercises in CodeLab 2 before the specified due date.

    Refer to the assigned reading for the next lesson to help you understand the problems. Also, you can use the online lecture notes for more information as the notes become available. You can look at solutions if you miss your first few attempts and are stuck by clicking the "Solution" tab.

Grading Criteria

The instructor will evaluate your assignment using the following criteria. Thus you should check your assignment against these criteria to maximize your score.

Each criteria represents a specific achievement of your assignment and has a scoring guide. The scoring guide explains the possible scores you can receive. Some scoring guides have a list of indicators. These indicators are a sign of meeting, or a symptom of not meeting, the specific criterion. Note that a single indicator may not always be reliable or appropriate in a given context. However, as a group, they show the condition of meeting the criterion.

For information on grading policies, including interpretation of scores, see the syllabus.

Lesson Exercises

  • 2: All lesson exercises attempted and turned in
  • 1: Some lesson exercises completed and turned in
  • 0: No lesson exercises completed or turned in

Programming Projects (x3)

  • 4: Demonstrates mastery of the program
    • Applies concepts from the lessons appropriately
    • Meets all specifications (see above)
    • Runs to completion with no abnormal error conditions
    • Generates correct output given correct input
    • Correct file name
  • 3: Has most of the functionality expected of the program
    • Demonstrates some techniques from the lesson
    • Attempts to meet all but one of the specifications (see above)
    • Implementation seems more complicated than necessary.
    • May have one minor error
  • 2: Has some of the functionality expected of the program
    • Demonstrates some techniques from the lesson
    • Attempts to meet at least 1/2 of the specifications (see above)
    • Implementation seems excessively complicated.
    • May have 2-3 minor errors
  • 1: Serious functional problems but shows some effort and understanding
    • Attempts to meet less than 1/2 of the of the specifications (see above)
    • Has a major error or many minor errors
    • Implementation seems very convoluted
    • Demonstrates few techniques from the lesson
  • 0: Not turned in or uses techniques not covered

Pair Programming Worksheet

  • 4: Student completed and submitted the learning worksheet with masterful effort
  • 3: Student completed and submitted the learning worksheet with substantial effort
  • 2: Student completed and submitted the learning worksheet with minimal effort
  • 1: Student submitted the learning worksheet
  • 0: No learning worksheet submitted

Programming Style

  • 2: Code is well-documented including:
  • 1: Code has some documentation errors
  • 0: No apparent attempt to follow documentation standards or write documentation comments

CodeLab Exercises

Number completed correctly / number exercises * 8 and rounded up to the nearest integer.

README.txt File

  • 2: README.txt file submitted following the instructions
  • 1: README.txt file submitted but some information was missing
  • 0: No README.txt file submitted

Total possible: 30, plus extra credit

Deliverables

Students submit some homework as they work on it like CodeLab. However, students must submit other homework in Canvas following the link to A2-Math and Memory. For detailed instructions see: How To Submit Homework Assignments. Include the following items when submitting to Canvas:

  1. README.txt file prepared by following the instructions for submitting homework.
  2. All the exercise files from Lesson 2
    • hellome.cpp
    • syntax.txt
    • variables.cpp
    • arithmetic.cpp
    • plan.txt
    • double.cpp
    • errors.txt
    • erroneous.cpp
  3. mathwork.cpp
  4. sumdiag.cpp
  5. worlds.cpp
  6. pairprogramming.txt
  7. Optionally, xcmathwork.cpp (extra credit)

Note: Make certain your programs compile before you turn them in. When a program does not compile then it does not function either. For all programming projects, you should expect little or no credit if your program does not compile and run. For more information see the Grading Criteria.

You must submit all the files needed to complete your assignment together. Your assignment must work as submitted. Remember to test and double check your files before submitting them. If you make a mistake, you can resubmit up to the deadline. If you resubmit, you must include all your assignment files in the last submission as Canvas hides prior submissions.

Last Updated: September 06 2018 @23:32:43