Solution to Self Check
Short Answers
- What is the boolean expression that is true when number is between a and b inclusive?
a >= number && b <= number
- What is the boolean expression that is true when a number if positive?
number > 0
- What is the boolean expression that is true if letter is either an exclamation mark (!) or question mark (?) ?
letter == '!' || letter == '?'
- What is the boolean expression that is true when the last digit of the number is a 5?
number % 10 == 5
- Given:
Evaluate the following to truthy or falsey:
int a = 5;
int b = 12;-
therefore this is falsey
a == 12 || b == 5
F || b == 5
F || b == 5
F || F
F -
therefore this is truthy
a == 5 && b == 12
T && b == 12
T && T
T -
therefore this is truthy
a == 5 || b == 12
T || b == 12 * note that since true or anything is true, C does
not continue evaluation. This is called lazy
evaluation
T -
0 is falsey thus this is falsey
a - 5
0 -
12 is not 0, therefore this is truthy
b
12 -
therefore this is truthy
!(a==12) && !(b==5)
!(F) && !(b==5)
!(F) && !(F)
T && T
T -
therefore this is falsey
((a==5) || (b==12)) && !(a==5 && b==12)
((T) || (b==12)) && !(a==5 && b == 12)
(T) && !(a==5 && b == 12)
(T) && !(T && b == 12)
(T) && !(T && T)
(T) && !(T)
T && F
F
-
Walkthroughs
Output:
Yesterday was sunny
Programming Problems
Problem 1:
double findPopCornPrice(char size, int hasButter, int hasSeaoning)
{
double price = 5.00;
if(size == 'm'){
price = 6.00;
}
else if(size =='l'){
price = 8.00;
}
//NOTE: test should be against 0, not 1 because the spec say 0 or NOT 0.
if(hasButter != 0){
price +=1.00;
}
//NOTE: test should be against 0, not 1 because the spec say 0 or NOT 0.
if(hasSeasoning != 0){
price +=0.50;
}
return price;
}
Problem 2:
Write the following function:
float TOURate(int dayOfWeek, int hour, int min){
double rate;
//first start by determining if it is weekend or not weekend
if(dayOfWeek == 0 || dayOfWeek == 6){
//if it is weekend the time is irrelevant. Assign the offpeak rate
rate = 7.6;
}
else{
//11am to 5pm means between: 11:00 to 17:00 respectively
//first part checks only hour.. no need to check minute
//second part is for the one minute at 17:00
if( (hour >= 11 && hour < 17) || (hour == 17 && min ==0)){
rate = 15.8;
}
//7pm to 7am is 19:00 to 7:00
//hour >= 19 covers all times from 19:00 to 11:59
//hour <7 covers all times from midnight to 6:59
//hour ==7 && min ==0 covers 7:00am
else if(hour >= 19 || hour < 7 || hour == 7 && min == 0){
rate = 7.6;
}
//if its not either of above, then its the rest of the times
else{
rate = 12.2;
}
}
return rate;
}
Debugging:
The following function is suppose to find the cost of shipping a package between two locations. The function is given the weight of the package in kilograms and the distance between the two locations. It is suppose to calculate the cost based on the following rules:
- all packages have a base cost of $10.75
- any package that has a weight of more than 5 kg gets charged an extra $2.10 per kg over 5kg
- any package that has to be shipped more than 600 km gets charged $0.05 per 10 kilometer over 600km (rounding up. Thus 601 km is charged $0.05 more)
int ShippingCost(int Weight, int Distance){
int my_result = 10.75;
if(Weight > 5){
my_result + 2.10(Weight) = my_result
}
else if(Distance > 600){
my_rseult = 600 * 0.5
}
}
-
In the above code identify the following:
-
all the syntax errors
- missing semi colon here:
my_result + 2.10(Weight) = my_result
my_rseult = 600 * 0.5- variable holding result must be on left hand side of expression:
my_result + 2.10(Weight) = my_result
should be:
my_result= my_result + 2.10(Weight)
- spelling error of my_result variable:
my_rseult = 600 * 0.5
should be:
my_result = 600 * 0.5
- multiplication requires * operator. should be:
2.10(Weight)
should be
2.10 * Weight
-
all logical errors
- return type is wrong, result is clearly going to be floating point
- additional charges for weight should only apply to the portion over the threshold
- should subtract threshold amount before calculating charge
- for distance, it overwrites myResult entirely, any additional cost should have been added to myResult
- for distance, the calculation is just plain wrong. It doesn't account for going over 600km it doesn't account for per 10km unit
- return is missing
-
all stylistic flaws that do not adhere to the style guide
- variables and functions should be lowerCamelCase:
- distance not Distance
- myResult not my_result
- shippingCost
- for a function, open curly bracket should be on next line:
int ShippingCost(double Weight, double Distance)
{
...
}- With each code block, there should be a consistent indentation. if() block is not correctly indented
- variables and functions should be lowerCamelCase:
-
-
Making as few changes as possible to address all of the errors above, rewrite the function so that it functions according to spec
double shippingCost(int weight, int distance){
double myResult = 10.75;
int overDistance;
if(weight > 5){
myResult = myResult + 2.10*(weight-5);
}
else if(distance > 600){
overDistance = distance - 600;
// division by 10 rounds down so the second part lets us
// calculate portion of 10kg
myResult += (overDistance/10)*0.05 + (overDistance%10)*0.05
}
return myResult;
}