CP4P Week5 Activity
CP4P Week5 Activity
Please keep only the portions of the document with your answers. Kindly delete the
instructions and notes.
−10% cost if you make me wade through all the stuff you were asked to delete.
Some find it easier to create a new document (Ctrl+N) and copy the portions from here to there.
What are the maximum and minimum values that can be stored in a short 16-bit signed
integer? (2.5 points)
16-bit signed integer maximum = ????? … minimum = ?????
Give examples of values that would cause overflow in positive and negative directions when
two short 16-bit signed integers are added together. (5 points)
????? + ????? are two numbers GT zero causing overflow in a short
????? + ????? are two numbers LT zero causing overflow in a short
[ play with the integerOverflow.exe program in the zip file. It requires the .NET virtual
machine so ensure Visual Studio IDE has been installed on your computer or launched on the
lab PC. ]
1 of 8
Week 5: Activity – Number systems Computer Principles for Programmers
Note: mid, low, and high are all variables of the same numeric data type; an arithmetic
operation on two variables of the same type always returns a value of that type. The type is
likely int and would be declared in the binary_search() library function where our problematic
mid calculation is found. The size of an int varies depending on the platform's C compiler.
Assume that the size of an integer is sufficient for the binary search function. The problem here
is not about the data type, its size, or bit width. Casting is not allowed. The task is to rewrite
the arithmetic so that regardless of the variables' data type, the calculation cannot
overflow.
Hint: The range of low to high values may appear to fit within the range of an integer data type.
0-------------------------low--------------high---------INT_MAX
Instead of finding the average of (low + high) / 2 as in the bug version,
0-------------------------low0------------------------------------------high
| ^ |
Casting is computationally expensive and merely avoids the problem. Casting an intermediate
result to a higher capacity data type will work reliably only if you cast from int to long long.
2 of 8
Week 5: Activity – Number systems Computer Principles for Programmers
That's the good news. The bad news is you are avoiding the bug, not solving it. Your children
will be faced with the same problem when a 64-bit sized int becomes the default. You don't
want them fixing this by casting to int128_t because then your grandchildren will inherit the
problem. When will it end?
Describe the steps you used to develop and test your solution to the binary search bug. (20
points)
A C program to test your new formula, MidBugTest.c is found in this week's zip file. The code
demonstrates the bug. Change the mid = line of code to your new formula, compile, and run.
When you are done (or just done in), read this: https://research.googleblog.com/2006/06/extra-
extra-read-all-about-it-nearly.html “Nearly All Binary Searches and Mergesorts are Broken” ––
only one of the suggested solutions in this article works in all cases. Your code should always
be portable across platforms meaning it must work in all cases.
3 of 8
Week 5: Activity – Number systems Computer Principles for Programmers
Relational C language
Logical Operators C language
operators expression
For any date in a year, what is the Boolean logic to decide if you have to go to school
during the current term?
The answer includes checking the following criteria and the links for details:
4 of 8
Week 5: Activity – Number systems Computer Principles for Programmers
Today’s date is within the Academic date range for the first and last day of the classes,
however there are no classes during study week, and the college is closed on holidays
within the term.
Today’s day-of-the-week (DoW) matched to your timetable’s number of classes on a day-
of-the-week: does your timetable have classes on this day?
You must be here on every day of exam week which follows the last day of classes. See
the Academic data link.
You may have other reasons to be or not to be on campus, e.g. project group meeting or
illness.
Use C language comparison and logical operators and write your logic in C syntax.
The following variables contain values for the current day-of-the-week, today's date, and term
dates:
int classesDoW[7] = array containing the number of classes on each Day of Week.
Use DoW as index.
Day of
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Week
DoW
0 1 2 3 4 5 6
index
Number of
0 n n n n n n
classes
5 of 8
Week 5: Activity – Number systems Computer Principles for Programmers
long startStudyWeek = first day of study week for this term YYYYMMDD
long endStudyWeek = last day of study week for this term YYYYMMDD
long startExamWeek = first day of exam week for this term YYYYMMDD
long endExamWeek = last day of exam week for this term YYYYMMDD
There is no need to write a C program, although some students like to do so and it is always
interesting to read a fully programed solution, there is no need to write a full C program.
The solution can be written as one long Boolean statement or separated into a stepwise
progression. E.g.
If your logic calls a function – and it does not have to – your answer must include each
function's C code. For example, to determine if today's date is or is not on a holiday,
6 of 8
Week 5: Activity – Number systems Computer Principles for Programmers
isHoliday(today) would return True or False – you must specify the logic inside the bool
isHoliday() {function} where bool is the returned data type and function is the { logic to
return the result of comparisons }.
There is no need to show the resolution of your Boolean expressions to TRUE or FALSE as
shown in this week's slides.
Colour Red value Green value Blue value 6 digit Hex code
Black 0 0 0 #
0 211 023 #
In Microsoft Word, you can edit a font’s colour by adjusting its Red, Green, and Blue values. However,
Word uses decimal instead of hex values where each colour value can be any decimal number from 0 to
255 (same range as a hex pair 00 – FF). Using the following Hexadecimal values, determine what colour
7 of 8
Week 5: Activity – Number systems Computer Principles for Programmers
First convert each HEX pair to decimal and then use Font Color / More Colors…Custom to adjust the RGB
decimal values:
8 of 8