2002bf07mt.sampsoln

2002bf07mt.sampsoln - SYSC 2002B Fall 2007/2008 Midterm...

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

View Full Document Right Arrow Icon
SYSC 2002B Fall 2007/2008 Midterm Exam Question 1. (5 marks) A professor wants to keep track of all of the different students who have asked questions in class. Write a program that reads in student numbers until -999 is entered and then prints out all of the different numbers that have been entered. You may not declare any arrays whatsoever but must instead make use of the facilities provided by class IntBag. Be sure to include all necessary "include" statements. #include "stdstuff.h" #include "IntBag.h" (½ mark for including IntBag.h) void realMain () { IntBag diffNums; int studNum; bool haveValue; for (;;) { // cout not required for full marks cout << "Enter the number of a student who asked a question: "; cin >> studNum; // read in the student number (½ mark) if (studNum == -999) break; // end of loop (½ mark) // (1½ marks for the next two lines, excluding break) if (diffNums.countOccurrences(studNum)==0) { // not yet in bag if (! diffNums.add(studNum) ) break; // so add him/her // note that we exit loop if bag fills } // else do nothing } // now print out all the different students (note that this may be // an incomplete list if the bag filled up) if (diffNums.size()==0) { // nothing to print cout << "No students have asked questions in class.\n"; } else { // (1½ marks for next 4 lines) cout << "The following students asked questions in class:\n"; for (haveValue=diffNums.startWalk(studNum); haveValue; haveValue=diffNums.continueWalk(studNum)) { cout << studNum << "\n"; // new line not required } } } void main () { // global try/catch
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/24/2010 for the course SYSC 2002 taught by Professor Unknown during the Fall '07 term at Carleton.

Page1 / 3

2002bf07mt.sampsoln - SYSC 2002B Fall 2007/2008 Midterm...

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

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