II: Low-Level Representations

Article Info

This worksheet will assess your knowledge of the low-level representations we have been exploring in the workshop content. The worksheet is formed of two parts; a quiz designed to test your understanding of the topic, and a programming task, designed to test how well you can apply the knowledge in practice.

To complete this worksheet:

  1. Complete the worksheet 2 quiz on learning space
  2. Implement the code for the worksheet task

Submission Instructions

For the final submission, you should submit your completed program.cs file and a PDF of your highest mark in the correct folder in your git repository.

The following filenames are expected:

  • quiz.pdf - the saved quiz result
  • Program.cs - your solution to part B

The final repository must not contain the whole CS project/solution. Only the ‘Program.cs’ should be submitted. Your code must also not import any 3rd party libraries, however use of the standard library is fine.

Part A: Quiz

Complete the quiz, ‘Worksheet 2: Low-Level Representations’ on learning space.

Part B: Programming Activity

Examine the provided skeleton project. This is a program designed to implement some of the concepts we have talked about in session, but there are some features missing. Your task is to implement these in such a way that the contract specified in this document, and the comments is met.

You must not alter the function signatures (names, return values, argument types). Doing so will negatively impact your mark, and may result in a mark of zero for this part

Must vs Could

These tasks are split into two categories:

  • Core tasks which should at least be attempted, but may still provide a challenge to beginners
  • Advanced tasks which are meant to provide deeper understanding and unlock the higher grade boundaries

For each task, the available marks are divided into two sets of requirements:

  • must and must not indicate requirements which must be completed for this task to be scored
  • should and should not indicate extentions which will only be graded if must and must not criteria have been successfully filled

Finally, the very top grade boundaries are not automatically marked. Marks of 80+ on the programming component can only be obtained by a manual asessment of the code. The assessor will award marks in this boundary based on code quality.

Although a perfectly valid and elegant approach in ‘real-world’ programming, clever ‘one liners’ which achive the objectives but not do not show understanding of the process will not scored highly on this critera (ie, doing it ’the long way’ vs using standard library shortcuts will be rewarded). Our objective is to demonstrate understanding of the concepts not write production code.

The worksheet is structured like this for a number of reasons:

  1. Provide structure for beginners, but still provide a suitable challenge for experienced programmers
  2. Mimic real-world requirements (such as those found in RFC documents)

Hexadecimal Definition

Many of the following tasks rely on hexadecimal numbers, for the purposes of the assignment a valid hexadecimal number is assumed to be:

  • Prefixed with the prefix 0x (lowercase x)
  • A sequence of characters, where each character is in one ranges 0-9 and A-F inclusive
  • Less than 10 characters long (not including the prefix)
  • Not padded (ie, 15 is 0xF not 0x0F)
  • Unsigned (ie, there will be no negative numbers)

Note that some of these criteria are simplifying assumptions for this assignment.

Core Tasks

The following tasks must be attempted in order to achive adequate or better on the ‘programming activity’ portion of the worksheet. Your methods must not ask for user input during execution, and must not write to standard error or standard out (ie, you don’t need to use Console or similar).

Test data will be provided to these functions via a suitable unit testing framework. If the tests cannot run successfully, or time out (take too long) during execution this will be counted as failing that test. Any attempt to bypass or comprimise the execution envrionment will be treated as academic misconduct.

NOTE: Static functions in this worksheet must not store persistant state (ie, static properties) - doing so is not required, and may result in some tests breaking.

Conversion to Hexadecimal

The method ConvertToHex should take a number, passed in as a function argument and return the hexadecimal representation of this number (as defined above).

  • The output number must meet the definition provided above
  • Inputs in the range 0-255 must be accepted and processed correctly
  • Inputs upto C#’s maximum integer (signed) size should be handled
  • If the input value is negative, you should return the string ERROR.

Conversion from Hexademical

The method ConvertToInt should take a string, passed in as a function argument and return the integer representation of this number.

  • The function must handle values between the range 0x0 and 0xFF correctly
  • The function must return -1 for incorrect inputs (ie, values which don’t meet our defintion of a hexadecimal number)
  • The function should be able to correct for padded hexadecimal values (ie, 0x01 should return 1 rather than -1 (invalid) )
  • The function should return -1 for a hexadecimal number which cannot be represented by a (singed) integer.
  • The function should be robust (ie. not crash) against incorrect inputs (returning -1 in these cases)

You may assume the should criteria superceeds earlier criteria (eg, no test will check that the function returns -1 for a padded value like 0x0F).

To assist you in this task, I have provided a utility function.

Vector Maths

Part of this worksheet is about basic operations in vectors, including the use of Pythagoras’ Theorem to find the length of a vector or the distance between two points.

Almost all games involve objects moving around in 2D or 3D space, and an understanding of the mathematics of vectors is essential to programming this movement.

You may find it useful to read the following GCSE revision guides before attemping this task:

There are a few functions which will need to be implemented:

  • addVectors takes two vectors: a and b and adds their components

  • subVectors takes two vectors: a and b and subtracts their components

  • lengthVector should calculate the length of a vector passed as an argument

  • vectorDistance should calculcate the distance between two vector arguments

  • For this part of the task, the functions must work for 2D vectors

  • The functions should work for 3D vectors

  • The functions should not accept vectors of unsupported sizes - and should return an array of size 0 (or 0.0F) in these cases

For the puposes of this function, performing operations on vectors of differing sizes is considered unsupported.

Advanced Tasks

These are advanced tasks intended to provide a challenge or extend your knoweldge (including providing an opportunity to explore refactoring). It is advised to attempt all of the core tasks first.

For each of these activities, there are required (must) and extention (should) tasks. As with the core tasks, no marks will be awarded for extention tasks unless the core requirements are met.

Conversion From Octal

Your ConvertToInt method must accept numbers in the ‘modern’ octal format (ie, 0o7) as well as the existing hexademical prefix from the core requirements.

  • The prefix 0o indicates the presence of an octal number. Only digits between 0 and 7 are valid.
  • The may assume the numbers are unsigned positive values
  • The function must handle the range 0o0 to 0o77 (63)
  • The function should handle arbitrary (positive) octal numbers upto the maximum value of a (signed) integer.

Jumbled Hexadecimal

NOTE: as a challenge function, this does not use the hexacimal defintion above, as it has it’s own rules.

This is designed as a harder extention task. The function ChallengeMiddleEndianHex will be given a hexadecimal number, and you must output that number according to the following, ‘jumbled hexadecimal’ format. This format is one devised specifically for this task. You will not find infomation on this format online.

The first two characters of the hexadecimal number must be extracted and form the lower byte (0-255), the next two values form the upper byte. For example, in ordinary hexadecimal, the value 0xFF00 means 65280, however, in this ‘jumbled’ representation these are flipped. The number is ‘really’ 0x00FF (255).

  • The function must handle inputs in the range 0x0000 to 0xFFFF
  • The function must treat inputs which don’t start with 0x as invalid
  • The function should handle both lower and upper case letters (f and F accepted).
  • Invalid inputs should return a value of 0
  • The function should handle values larger than 0xFFFF (up to int), in these cases the pattern is followed.
  • The function should handle overflows using ‘wrap around’ (modulo/remainder) logic.

For the last extention task, 0x11223344 should be understood as 0x22114433

You may assume all (valid) inputs are positive numbers.

If you want to test your solution, you can take your four character hexadecimal value (0xFF00), and flip the order of the two bytes. In the general case, 0xHHLL becomes 0xLLHH. The individual characters in the pairs will not be flipped.

Assessment Rubric

This assessment is not designed around a rubric, instead you can view a detailed marking breakdown.

Section Task Must Should Subtotal
Quiz /20 /10 /30
Core Tasks Conversion to Hexademical /10 /3 /31
Conversion from Hexademical /8 /3
Vector Maths /5 /2
Advanced Tasks Jumbled Hexadecimal /7 /5 /23
Conversion from Octal /6 /5
Solution Quality - - /16 /16
/56 /44 /100
Graduation Cap Book Open book GitHub Info chevron-right Sticky Note chevron-left Puzzle Piece Square Lightbulb Video Exclamation Triangle Globe