0% found this document useful (0 votes)
484 views8 pages

CP4P Week5 Activity

If the timer value is stored in a 32-bit signed long integer, it will take approximately 49 days for integer overflow to occur. The maximum and minimum values of a 16-bit signed short integer are 32,767 and -32,768 respectively. Values that would cause overflow in a 16-bit signed short integer when added are 32,767 + 1 and -32,768 - 1. The potential issue with calculating the midpoint as (low + high) / 2 is integer overflow if low and high are large enough that their sum exceeds the maximum integer value. To fix it, the code should calculate mid as low + (high - low) / 2 to avoid adding low and high.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
484 views8 pages

CP4P Week5 Activity

If the timer value is stored in a 32-bit signed long integer, it will take approximately 49 days for integer overflow to occur. The maximum and minimum values of a 16-bit signed short integer are 32,767 and -32,768 respectively. Values that would cause overflow in a 16-bit signed short integer when added are 32,767 + 1 and -32,768 - 1. The potential issue with calculating the midpoint as (low + high) / 2 is integer overflow if low and high are large enough that their sum exceeds the maximum integer value. To fix it, the code should calculate mid as low + (high - low) / 2 to avoid adding low and high.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Week 5: Activity – Number systems Computer Principles for Programmers

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.

 Your name: Student No.: UserID: _____ @mySeneca.ca

Activity 1 of 3 – integer overflow


Imagine a stopwatch timer which counts in hundredths of a second. Start it.

DAYS HOURS MINUTES SECONDS hundredths

 If the timer value is stored in a signed long 32-bit integer,


how many days will it take until that integer overflows? (5 points)

 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

Binary Search Bug


FYI Here is an animation of a binary search which walks through the C code:
https://www.cs.usfca.edu/~galles/visualization/Search.html
[ note: numeric keypad input does not work, use the keyboard's top row ]
[ search: for the value found at index position 20 ]
[ controls at the bottom allow you to adjust animation speed and stepping ]
The following line of code was used for a long time in many standard programming libraries to
find the middle point in a range of array indices for a binary search:
mid = (low + high) / 2; // find mid-point in a range of values
Your task is to identify and fix the bug in that calculation of mid.
 What is potentially wrong with the (low + high) / 2 calculation to find the middle point?
Under what conditions would the calculation go wrong? (7.5 points)
For a demonstration of the problem, run MidBugTest.exe found in this week's zip file.

 REWRITE the code to prevent overflow (10 points)


from mid = (low + high) / 2;
to mid = ;

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
| ^ |

first, find the middle point between low and high.


0-------------------------low--------------high---------INT_MAX
low--------------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

Activity 2 of 3 – Boolean logic (25 points)

Relational C language
Logical Operators C language
operators     expression

Equal to a == b negation – NOT TRUE !( a == b)

Not equal to a != b negation – NOT a != b

conjunction – AND a > b && b > c


Greater than a > b
both expressions are true a must be > c

disjunction – OR a > b || b > c


Less than a < b
either expression is true a vs c is unknown

Greater than order of operations | operator precedence


or equal to
a >= b
( implied parentheses shown )

( comparison AND comparison )


Less than or
a <= b (comparison AND comparison) OR (comparison AND comparison)
equal to

Note: A relational expression in programming always compares one single thing to


another single thing. It does not allow testing a range or a list, e.g.
(startDate <= today <= endDate) works only for mathematics,
(today == startDate to endDate) and (today != Saturday,Sunday) works for humans,
but neither works for C programming.

 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 DoW = today's Day of Week number: 0 is Sunday, 1 is Monday, 2 is Tuesday, 3 is


Wednesday, 4 is Thursday, 5 is Friday, 6 is Saturday.

 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

 long today = today's calendar date YYYYMMDD

 long startClasses = first day of classes for this term YYYYMMDD

5 of 8
Week 5: Activity – Number systems Computer Principles for Programmers

 long endClasses = last day of classes for this term YYYYMMDD

 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

 Winter term holiday dates

o long familyDay = YYYYMMDD // third Monday in February

o long goodFriday = YYYYMMDD // depends on lunar cycle


Easter is the first Sunday following the full moon following the Spring equinox.

 Summer term holiday dates

o long goodFriday = YYYYMMDD // depends on lunar cycle

o long victoriaDay = YYYYMMDD // third Monday in May

o long canadaDay = YYYY0701 // first day of July

o long civicDay = YYYYMMDD // first Monday in August

 Fall term holiday dates

o long thanksgivingDay = YYYYMMDD // second Monday in October

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.

int flag1,flag2,flagN, ... ; // declare logical flags


flag1=flag2=flagN= ... = 0; // init logical flags to FALSE
if (comparison1 is TRUE) flag1 = 1; // true
if (comparison2 is TRUE) flag2 = 1;
...
if (flag1 && flag2 && ...) printf("GOTO SCHOOL");
else printf("stay home");

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.

Activity 3a and 3b of 3 – Numbering Systems and Conversions (15 + 10 points)


HTML colour codes are expressed in three hexadecimal values like this #0088FF which can represent
16,777,216 colours. # indicates the start of hex digit pairs which read as # 00 88 FF pronounced "hex,
zero-zero, eight-eight, Fox-Fox." (three hex values)
Hex digits are 0 – 9, Able, Baker, Charlie, Dog, Easy, Fox for 0 – 9,10,11,12,13,14,15 decimal values in a
single hex digit.
All of these are equivalent:
16×16 × 16×16 × 16×16 (6 hex digits or three hex values, #FFFFFF)
or 256 × 256 × 256 (three byte values)
8 8 8
or 2 × 2 × 2 (three 8 bit values)
24
or 2 (one 24 bit value)
or 16,777,216 possible decimal values.
Each hex digit pair represents a range of three colours: Red #FF0000, Green #00FF00, and Blue #0000FF
known as RGB values.
See https://www.rapidtables.com/convert/color/index.html to convert colour codes between HEX and
RGB. To further explore colours, go to https://www.hexcolortool.com/. The Windows calculator ( 
"calc" or  + R "calc") has a very handy Programmer function available from the hamburger button.
3a  What is the hex value for these colours? (15 points)

Colour Red value Green value Blue value 6 digit Hex code

White 255 255 255 #

Grey 128 128 128 #

Black 0 0 0 #

0 211 023 #

103 110 160 #

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

would be produced in Word.

First convert each HEX pair to decimal and then use Font Color / More Colors…Custom to adjust the RGB
decimal values:

3b  Fill in this chart as per the column headings (10 points)

6 digit Red Green Blue Describe the Final Colour


Hex decima decimal decima and change the cell's background
code l value value l value colour, i.e. R-click and see MS
(0-255) (0-255) (0-255) Word 'Shading',
to match the values for RGB
#FB6905
#0AA2C8
#E160B7
#0B6A0C

8 of 8

You might also like