EECS168:Lab2

From ITTC
Jump to: navigation, search
Navigation
Home
Information

Syllabus
Schedule
Exam Reviews

Classwork

Labs
Submitting Work

Lab conduct

  • Do not use any unauthorized aid, such sites like rentacoder or chegg to obtain answers
  • Do not use code provided by another student
  • Do not reuse code (by you or anyone) from prior semesters
  • If you need help, seek it from:
    • Your lab TA.
    • Me, Dr. Gibbons, my email is jwgibbo@ku.edu / jwgibbo@gmail.com
  • If equipment you don't own (e.g. cycle servers) needs attention or you're having account issues put in a ticket!

Due Time

  • Due Friday June 19th by 11:59pm

Learning Objectives

  • Continue to use the debugger
  • Learn the modulus operator
  • Keep using if statements, if-else, and if-else-if statement

Boolean Expression and Operators

Boolean Operators

  • < less than
  • > greater than
  • <= less than or equal to
  • >= greater than or equal to
  • == equal to
  • != not equal to
  • Other operators including not, and, or, will come later

Boolean expression

A boolean expression reduces to true or false. It usually uses boolean operators.

Examples (int a vacuum):

  • 3<7
  • 7<3
  • 5!=5
  • 'a'=='A'
  • 5.5>=(10.5+3.1)

Syntax for if-else statement

A stand-alone if

if (Boolean expression)
{
   Runs if the boolean expression is true
}

An if else

if (Boolean expression)
{
   Runs if the boolean expression is true
}
else
{
   Runs if the boolean expression is false
}

An if, else if

if (Boolean expression 1)
{
   Runs if the boolean expression is true
}
else if(Boolean expression 2)
{
   Runs if the boolean expression 1 is false and boolean expression 2 is true
}
else
{
   Runs if all previous boolean expressions are false
}


Modulus Review

The % operator is a numeric operator that returns the remainder of division. Examples:

int denom = 5;
std::cout << (0 % denom) << std::endl; //prints 0
std::cout << (1 % denom) << std::endl; //prints 1
std::cout << (2 % denom) << std::endl; //prints 2
std::cout << (3 % denom) << std::endl; //prints 3
std::cout << (4 % denom) << std::endl; //prints 4

std::cout << (5 % denom) << std::endl; //prints 0
std::cout << (6 % denom) << std::endl; //prints 1
std::cout << (7 % denom) << std::endl; //prints 2
std::cout << (8 % denom) << std::endl; //prints 3
std::cout << (9 % denom) << std::endl; //prints 4

std::cout << (10 % denom) << std::endl; //prints 0
std::cout << (11 % denom) << std::endl; //prints 1
std::cout << (12 % denom) << std::endl; //prints 2
std::cout << (13 % denom) << std::endl; //prints 3
std::cout << (14 % denom) << std::endl; //prints 4

std::cout << (15 % denom) << std::endl; //prints 0

Exercise 1: Saffir–Simpson Scale

Let's learn about what the different categories of hurricane mean. In short, it all comes down to wind speed.

The upper bounds are non-inclusive for example, a category Four is 58 (included) up to but not including 70 m/s.

Category Wind Speed
Five ≥ 70 m/s
Four 58–70 m/s
Three 50–58 m/s
Two 43–50 m/s
One 33–43 m/s
Tropical Storm 18–33 m/s
Tropical Depression < 18 m/s

You will obtain a wind speed from the user and tell them either what category or type if it's just a tropical storm. If the wind speed is invalid, print an error.

Sample runs:

Input a wind speed (m/s): 55
A wind speed of 55 m/s is a Category 3 hurricane.
Input a wind speed (m/s): -111
Negative wind? Sorry, that's invalid.
Input a wind speed (m/s): 0
A wind speed of 0 m/s is a tropical depression.

Exercise 2: Long Division Printer

Obtain a numerator and a denominator from the user. Then, display the result of long division from the user. You may assume integers as input, but you must prevent the user from crashing the program with their input!

Your output should very closely resemble the output below:

Sample runs:

Enter a numerator: 7
Enter a denominator: 3 
7 / 3 = 2 Remainder 1
Enter a numerator: 11
Enter a denominator: 0 
Sorry, you may not divide by zero.

Exercise 3:Restaurant

You will open a restaurant that sells three items: Salads, Pasta, and Cake. Your restaurant will do the following:

  1. Ask the user if they want Salads then Drinks then Cakes
    • In all cases, you are prompting them for a yes/no answer. For yes they can type 'y' or 'Y', and for no they can type 'n' or 'N'
    • If they want an item, find out how many
    • The user must not be allowed to order less than zero of an item, if they do, simply set the amount they order to zero.
  2. Ask them for their age, this will affect the discount
    • Anyone 65 or older gets a 10% discount off the total cost BEFORE tax
    • Anyone 5 or younger gets all the cake they want for FREE!
  3. Display a cost per item (before any discounts), a subtotal (before any discounts), a tax amount, a discount amount, and a grand total
    • Tax is 5%

Sample run:

============================
WELCOME TO THE RESTAURANT
============================

Do you want Salads? (y/n): y
How many?: 10

Do you want Drinks? (y/n): N

Do you want Cakes? (y/n): Y
How many?: 10

How old are you?: 68
============================
10 Salads @ $4.25 ==> $42.50
0 Drinks @ $1.05 ==> $0.00
10 Cakes  @ $3.55 ==> $35.50
Subtotal: $78.00
Tax: $3.90
Discount: $7.80
============================
Total: $74.10

Please come again!

NOTE: For the rounding to two digits use this:

int main()
{
   std::cout.precision(2);
   std::cout << std::fixed;
   //rest of your program below...


Rubric

This lab is worth 100 points total. 168 Rubric

  • [5pts] Lab file submission: file names, tar created correctly
  • [5pts] Program have clean, readable, easy to understand output
  • [20pts] Exercise 1: Hurricanes
    • [15pts] All ranges covered accurately
    • [5pts] invalid ranges given some kind of error message
  • [20pts] Exercise 2: Long Division Printer
    • [15pts] Correct values printed with valid input
    • [5pts] Error checking
  • [50pts] Exercise 3: Restaurant
    • [5pts]Each item ordered separately
    • [10pts] Yes and No are case-insensitive
    • [5pts]User cannot order less than 0 of an item
    • [5pts]Proper itemized totals
    • [5pts]Proper subtotal
    • [5pts]Proper tax
    • [5pts]Proper discount for 65 and older
    • [5pts]Proper discount for 5 and younger
    • [5pts]Overall all receipt presentation


Lab Submission

Consult your TA for the submission method they prefer.