2002af07mt.sampsoln

2002af07mt.sampsoln - SYSC 2002A Fall 2007 - Midterm...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
SYSC 2002A Fall 2007 - Midterm (October 22 nd , 2007) Sample Solutions Question 1 [4 marks] a) Give the output produced by the following: (1 mark) int *a, b; int *c, d, *e; b = 2; d = 1; a = &b; c = &b; e = a; *c = 7; *a = 3; cout << *c << " " << *e << "\n"; This prints: 3 3 (½ mark each) b) Give the output produced by the following: (2 marks) c) There is a memory leak. Write the missing code and clearly indicate with an arrow exactly where the code belongs . (1 mark) int *f, *g, i; f = new int; g = new int[8]; *f = 10; for ( i=0; i<8; i++ ) { g[i] = i; } cout << *f << " " << g[4] << " " << g[0] << "\n"; (*f)--; g = new int[10]; for ( i=0; i<10; i++ ) { *(g+i) = 2*i; } cout << *f << " " << g[4] << " " << g[9] << "\n"; b) This prints: (1 mark for each line; subtract ½ for each wrong answer with minimum 0 per line [i.e. must have at least two numbers correct on a line to get any marks]) 10 4 0 9 8 18 c) Add: delete [] g; (½ mark) either before or after the line: (*f)--; (½ mark)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Question 2 [4 marks] In the space below, write the hasDuplicates function used in realMain below. This method returns true if the bag contains duplicate items and false if there are no duplicates in the bag. You must include comments to get full marks. Note that this is just a regular 1606 style function. You are not adding a new method to IntBag. (There is a blank page at the end if you run out of space below. Clearly indicate on this page if your answer continues there.) #include "stdstuff.h" #include "IntBag.h" // hasDuplicates takes an IntBag argument and returns true if any item in the // bag occurs more than once, and false otherwise. bool hasDuplicates (IntBag &aBag) { // call by reference best for objects bool isMore; int item; // use startWalk and continueWalk to go through all items in the bag for (isMore = aBag.startWalk(item); isMore; isMore = aBag.continueWalk(item)){ if (aBag.countOccurrences(item) > 1) return true; // found a duplicate } // Could use a while loop instead of a for loop here, e.g.: // isMore = aBag.startWalk(item);
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

2002af07mt.sampsoln - SYSC 2002A Fall 2007 - Midterm...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online