2002 Computer Science 330FC Language Implementation Your Marks for Assignment 4 All marking queries should initially be directed to the marker. Marking is done by running test cases, reading source code, sample programs, documentation, etc. ################################################################################ Basic Assignment ################################################################################ ________________________________________________________________________________ 1 Increment and decrement operators ________________________________________________________________________________ Works for integers? (3 marks) println( "q1a_incDec1:" ); int x = 5; int a, b, c, d; a = x++; b = x++; c = ++x; d = ++x; println( "a = " + a + ", b = " + b + ", c = " + c + ", d = " + d ); a = x--; b = x--; c = --x; d = --x; println( "a = " + a + ", b = " + b + ", c = " + c + ", d = " + d ); Works for chars? (3 marks) println( "q1b_incDec2:" ); char x = 'a'; char a, b, c, d; a = x++; b = x++; c = ++x; d = ++x; println( "a = " + a + ", b = " + b + ", c = " + c + ", d = " + d ); a = x--; b = x--; c = --x; d = --x; println( "a = " + a + ", b = " + b + ", c = " + c + ", d = " + d ); Works when uses lots of registers? (3 marks) println( "q1c_incDec3:" ); int f() begin return 5; end int g() begin return 3; end []int a = new []int {0,1,2,3,4,5,6,7}; int x = f() + a[g()]++; println( "x = " + x ); println( "a[3] = " + a[g()] ); Only computes the address once? (3 marks) println( "q1d_incDec4:" ); int f() begin println( "Invoke f()" ); return 5; end []int a = new []int {0,1,2,3,4,5,6,7}; int x = a[f()]++; println( "x = " + x ); println( "a[ 5 ] = " + a[ 5 ] ); Suitable examples provided to test? (3 marks) 1 (xxx/15 marks) Increment and decrement operators ________________________________________________________________________________ 2 Array representation and Subarrays ________________________________________________________________________________ Works for arrays (can really only check by reading the code generated, and seeing whether subarrays work, because otherwise they would get marks for handing in the template.) (5 marks) println( "q2a_array1:" ); []int a = new []int { 1, 2, 3, 4, 5 }; []int b = new [5]int; int i; for i = 0; i < size a; i++ do b[ i ] = a[ i ]; println( "b[ " + i + " ] = " + b[ i ] ); end int f() begin return 3; end println( "b[ 3 ] = " + b[ f() ] ); println( "q2b_array2:" ); []char a = new []char { 'a', 'b', 'c', 'd', 'e' }; []char b = new [5]char; int i; for i = 0; i < size a; i++ do b[ i ] = a[ i ]; println( "b[ " + i + " ] = " + b[ i ] ); end int f() begin return 3; end println( "b[ 3 ] = " + b[ f() ] ); Works for int subarray (10 marks) println( "q2c_subArray1:" ); []int a = new []int { 0, 1, 2, 3, 4, 5, 6 }; void printArray( []int a; ) begin int i; for i = 0; i < size a; i++ do print( a[ i ] + " " ); end println(); end void zero( []int a; ) begin int i; for i = 0; i < size a; i++ do a[ i ] = 0; end end printArray( a[ 3 .. 5 ] ); zero( a[ 3 .. 5 ] ); printArray( a ); Works for char subarray (10 marks) println( "q2d_subArray2:" ); []char s = "abcdefghijklmnopq"; void printArray( []char a; ) begin int i; for i = 0; i < size a; i++ do print( a[ i ] ); end println(); end printArray( s[ 5 .. 8 ] ); Works for subarray when use lots of registers (5 marks) println( "q2e_subArray3:" ); void printArray( []int a; ) begin int i; for i = 0; i < size a; i++ do print( a[ i ] + " " ); end println(); end []int a = new []int { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; int f() begin println( "invoke f()" ); return 5; end int g() begin println( "invoke g()" ); return 8; end printArray( a[ f() .. g() ] ); Suitable examples provided to test? (5 marks) 2 (xxx/35 marks) Array representation and Subarrays ________________________________________________________________________________ 3 run-time representation of strings ________________________________________________________________________________ Works for char arrays, both interpreter and compiler (15 marks) println( "q3a_charArray1:" ); []char konnichiWa = "konnichi wa"; []char hello = new []char { 'h', 'e', 'l', 'l', 'o' }; println( konnichiWa ); println( hello ); println( hello[ 0 .. 3 ] ); Works for concatenation, both interpreter and compiler (15 marks) println( "q3b_charArray2:" ); []char s = "abcdefgh"; []char leftPad( []char s; int width; ) begin while size s < width do s = ' ' + s; end return s; end []char rightPad( []char s; int width; ) begin while size s < width do s = s + ' '; end return s; end println( "\"" + s + "\"" ); println( "\"" + leftPad( s, 12 ) + "\"" ); println( "\"" + rightPad( s, 12 ) + "\"" ); Suitable examples provided to test? (5 marks) 3 (xxx/35 marks) run-time representation of strings ################################################################################ Bonus Assignment ################################################################################ ________________________________________________________________________________ 4 Reprinting of non-char arrays in simulator to make like interpreter ________________________________________________________________________________ Works for one dimensional arrays (15 marks) println( "q4a_printArray1:" ); bool true = 0==0; bool false = not true; println( new []int { 1, 2, 3, 4 } ); println( new []bool { true, false, true, false } ); println( new []real { 1.1, 2.2, 3.3 } ); Works for multi dimensional arrays (15 marks) println( "q4b_printArray2:" ); [][]int a = new [][]int { new []int { 1, 2, 3 }, new []int {4, 5 } }; println( a ); [][]int b = new [][]int { new []int { 1, 2, 3 }, null }; println( b ); Suitable examples provided to test? 4 (xxx/30 marks) Reprinting of non-char arrays ________________________________________________________________________________ ################################################################################ The markers may give up to 10 marks, to about one student in 10, for good documentation, good design, additional features implemented (above those specified for bonus marks), additional test examples, etc. ################################################################################ extra (xxx/10 marks) Insight, Design, additional work, etc. ################################################################################ ################################################################################ Enter raw total mark here: Not Marked ################################################################################ Your raw mark is considered to be out of 80. However students may obtain more than full marks due to increases in marks for some sections, attempting the bonus topics, etc. Your adjusted mark, taking into account bonus and penalty marks will be computed separately, from the time of submission. ################################################################################ Comments: ################################################################################