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 completed code to another person or look at another person's code 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.

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 this is a solo programming project and not a pair-programming project. 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.

Resturant food

Project 1: Calculating Meal Cost

Meals in a restaurant include the cost of purchasing the food, sales tax and tip (gratuity). Before the 20th century, Americans viewed tipping as inconsistent with the values of an egalitarian, democratic society. After prohibition, restaurants encouraged tipping to make up for losses due to the lack of alcohol sales and the opposition to tipping gradually faded [1]. Tipping is now an American institution such that a 15% gratuity is now customary when a waiter provides adequate service at a restaurant with table service [2].

Project Specifications
  1. Write a program to calculate the total cost of a meal given the meal price (base cost of a meal), tip percent (the percentage of the meal price being added as tip), and tax percent (the percentage of the meal price being added as tax) as user input.
  2. You must name the source code file mealcost.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. Ask the user for the following inputs (and no other input) in this order, as shown in the Example Run below:
    1. Meal cost (before tax and tip)
    2. Percentage tip (percentage of base cost added as tip)
    3. Percentage tax (percentage of base cost added as tax)
  4. Example Run: The input prompts and outputs of the program must look like the following, including the same order of input and wording of the output, for you to receive full credit. For the input shown you must get the same output. However, the output numbers must change if the inputs are different.
    Enter the base meal cost: 12.34
    Enter the percentage tip: 20
    Enter the percentage tax: 8.5
    The total meal cost is $15.86.
    

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

  5. Use the round() function for rounding to two decimal places, as discussed in lesson 2.3.5.

    Hint: Multiply by 100, round, and then divide by 100.

  6. After displaying the output, exit the program.
  7. Submit this project with the rest of the assignment as described in Deliverables.
References and More Information
  1. Segrave, Kerry (1998). Tipping: An American social history of gratuities. Jefferson, North Carolina: McFarland & Company. ISBN 0786403470 (Google preview).
  2. Gratuity - United States - History: Wikipedia, retrieved 2017-01-03.
  3. Publication 115, Tips, Gratuities, and Service Charges: California State Board of Equalization (tax administration)

Example Student Activities Card

Project 2: Composing Student ID's

Cabrillo issues every student a unique seven-digit Student Identification Number (Student ID). Cabrillo uses the number during registration and other processes, as well as printing the number on Student Activity Cards.

For this project, you will generate and print your unique Student ID number derived from a starting integer number. You will need to use integer division and modulus to extract each digit from the starting number and arrange the digits in the correct order.

Project Specifications
  1. Write a program that prints your unique seven-digit Student ID number by extracting it from the seed number in the starter code: sidmod.cpp
  2. You must name the source code file sidmod.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. Do NOT ask the user for any input in this project.
  4. Do NOT directly access or print your Student ID or any literal number; instead, use only division and modulus operations to extract the seven digits of your Student ID from the SEED variable and print the extracted digits.

    At a minimum, your code must include 6 division operations and 6 modulus operations to extract the digits. Do not use if-statements or other techniques we have not covered.

  5. Do NOT change the original SEED statement in any way. Your code must use the SEED number provided exactly.
  6. Example Run: The outputs of the program must look like the following for full credit. Your program must display your unique Student ID number.
    My student id: 0123456
    

    In the above example run, the programmer's Student ID is 0123456. Your number will be different.

  7. After displaying the output, exit the program.
  8. Submit this project with the rest of the assignment as described in Deliverables.
Hints:

For an example, refer to lesson 2.3.4. Try doing the same thing as the example Program using Modulus to Split Digits but using the starter code from the project, like:

  1. If we print SEED % 10 we will get the last number: 0
    cout << SEED % 10; // last seed number
    
  2. If we print SEED / 10 we get: 123456789
    cout << SEED / 10; // 123456789
    

    Then notice that 123456789 % 10 yields 9, which is the second to last number. So to get the second to last number we use:

    cout << SEED / 10 % 10; // second from last number
    
  3. If we keep going with division by powers of 10 followed by % 10, we can get any digit from the SEED.

Image

Project 3: Imagine All the People

Worldwide population continues to grow every year [1]. One model for projecting the growth is given by the equation:

population = 7.35 · e 0.0071 (Year - 2016)

which reports the results in billions.

The letter e in the above formula is the mathematical symbol for Euler's number [2], the base of the natural logarithms. To calculate this exponent in C++, you will need to use the exp() function from the cmath library discussed in lesson 2.3.5.

Project Specifications
  1. Write a program that requests a year from a user and displays the estimated population for that year as shown in the Example Run below.
  2. You must name the source code file popgrowth.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. Ask the user for the year, and no other input, as shown in the Example Run.
  4. Calculate the population for the year using the formula for population shown above. Use the exp() function from the cmath library to calculate the power of e (Euler's number).
  5. Example Run: The outputs of the program must look like the following for you to receive full credit. For the input shown (2020) you must get the same output. However, the output numbers must change if the inputs are different.
    Enter the year: 2020
    Estimated worldwide population in 2020: 7.56173 billion
    

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

  6. Use the default formatting and precision for the numbers -- do NOT add any numerical formatting statements or rounding to the code.
  7. After displaying the output, exit the program.
  8. Submit this project with the rest of the assignment as described in Deliverables.
More Information
  1. World Population Information: US census
  2. How To Pronounce Euler: YouTube

Extra Credit

Completing the following is worth extra credit. (2 points)

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

Conversion Process Example

Project 4: Nibbling on Numbers

Converting a decimal number to a binary number is one of the most common procedures performed in computer operations [1]. In this project you will convert a decimal (base 10) number into a binary number. We will limit the size of the number to four (4) binary digits, which is known as a nibble (or nybble to match the letters in byte) [2].

Here is an example of converting the number 1310 to binary:

13 / 2 = 6 r 1
 6 / 2 = 3 r 0
 3 / 2 = 1 r 1
 1 / 2 = 0 r 1

Notice that the remainders in reverse order are the binary equivalent of 1310: 11012.

Project Specifications
  1. Write a program that inputs a decimal number between 0 and 15 and outputs the binary equivalent.
  2. You must name the source code file dec2nibble.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. Ask the user for an integer number between 0 and 15, as shown in the Example Runs below.
  4. Convert the integer decimal number into a binary number using a series of division, modulus, and other mathematical operations as described in lesson 2.3.4. Your solution must use at least three division operators and four modulus operators. Do not use if-statements or other techniques we have not covered.
  5. Example Runs: The input prompts and outputs of the program must look like the following for full credit, including the same order of input and wording of the output. For the input shown you must get the same output. However, the output must change if the input is different.
    Enter a decimal number between 0 and 15: 2
    The binary equivalent is: 0010
    
    Enter a decimal number between 0 and 15: 13
    The binary equivalent is: 1101
    

    In the above two example runs, the user entered "2" and "13" (without the quotes) as the integer numbers to convert.

  6. After displaying the output, exit the program.
  7. Remember that your solution must use only the techniques we have covered so far, so loops are NOT allowed.
  8. Submit this project with the rest of the assignment as described in Deliverables.
References and More Information
  1. Converting Decimal Numbers into Binary Numbers
  2. Nibble: Wikipedia
  3. How to Convert from Decimal to Binary: WikiHow

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

Projects (x3)

  • 5: 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
  • 4: 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
  • 3: 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
  • 2: 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
  • 1: Does not compile or wrong file turned in
  • 0: Not turned in or uses techniques not covered

Program Style

  • 3: Code is well-documented including:
  • 2: Code has a minor documentation error
  • 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
  3. mealcost.cpp
  4. sidmod.cpp
  5. popgrowth.cpp
  6. Optionally, dec2nibble.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 problems, 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. Your assignment must work as submitted. Remember to test and double check your files before submitting them and make certain they compile. 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.

Last Updated: February 14 2017 @19:14:08