Você está na página 1de 7

May 13, 2009

CS201 (Intro. to Computing) MIDTERM II


1 2 3 4 5 6 TOTAL

Name ID

: :

Notes: a) Please answer the questions only in the provided space after each question. b) Duration is 100 minutes. c) Closed-book, closed-notes, no calculators and computers. Two A4 size cheatnote pages are allowed. d) There must be six pages (including this one) in this booklet. Please check it out! QUESTIONS 1) (16 points) In this question you are going to add a new member function to the robot class. This member function, named PickThings, will pick count many things from the cell only if the cell has enough count of things. If the cell has cell_count many things in it and cell_count is less than count, then the robot will pick cell_count many things instead of count many things. The type of the count parameter is an integer and the function should also return the actual picked count, where the return type is also an integer. a) The header file of the robot class should be updated to add this new member function. Which part of the header file should change? Also, write the necessary line(s) that you should add to the header file.
The public part of class definition should be updated. The following line must be added there.

int PickThings(int count); b) Write the implementation of the PickThings member function. int Robot::PickThings(int count) { int picked = 0; while (!CellEmpty() && picked < count) { PickThing(); picked++; } return picked; }

NAME: 2) (23 points) The file that we use in this question is line oriented such that in each line there are two data items: one word for the food or drink item and the other is the price of that item. An example file structure is below.
Bread 0.85 Latte 4 Hamburger 6.5 Strawberries 5 Water 1.0 Cake 20 Juice 4.5

Write a void function that takes an input file stream, say inputFile, and a double, say money, as parameters. We assume that inputFile contains some item-price pairs in the format explained above. There could be any number of such pairs (i.e. lines) in the file. In other words, you cannot make any assumption about the number of item-price pairs in the file. Your function should search through inputFile from the beginning to find the items we can afford to buy with the amount money and display how many of such items we can buy with that money. For example, if the item is bread that you can buy with money, display something like You can buy <n> many of item bread., where <n> is the number of breads you can buy with money. You should display all the items that you can buy with money and do not subtract one item. If we can afford none of the items in the file, then your function should display "Sorry, you can buy nothing with money." on the screen. ATTENTION!!! You may assume that inputFile is already opened; therefore, please do not open it again in the function. However, you cannot make any assumption about the current position of its file position pointer; it may be at the beginning, somewhere in the middle or at the end of the file.
void displayItemsToBuy (ifstream & inputFile, double money) { string line, item; double price; int numberItems = 0; inputFile.clear(); inputFile.seekg(0); while (getline(inputFile, line)) { istringstream strline(line); strline >> item >> price; if (price <= money) { numberItems = money / price; cout << "You can buy " << numberItems << " many of item " << item << endl; } } if (numberItems == 0) cout << "Sorry, you can buy nothing with " << money << "." << endl; }

NAME: 3) a) (2 points) Write the prototype of a non-void function that takes an double parameter and returns the character that corresponds to its most significant digit. Do not write the function body, write only the prototype. char mostSignificantDigit (double num); //num could be omitted b) (2 points) Write the prototype of a void function that takes a character parameter and returns the ASCII code of it. Do not write the function body, write only the prototype.
void ASCIICode (char ch, int & code); //code could be omitted

c) (6 points) What are the values of the following expressions? ( 11.0 < ( double (26) / 3 ) / 2 ) == ( double (7/3) > 2.0 ) True ( 1 is also OK )

char ( int (7.0 / 2) + int ('B') + ( int ('d') - int ('b') ) ) 'G' (quotes may be omitted)

d) (7 points) What is the output of the following program piece when it is executed with the content of the file data.txt given below?
string ifstream int filename="data.txt"; input; number, sum = 0, errors = 0;

data.txt
27 1 err 5 2d -7 10 t3t

input.open(filename.c_str()); while ( !input.eof() && errors < 3 ) { if (input >> number) { sum += number; } else { input.clear(); errors++; } } cout << sum << " " << errors << endl;

28 3

NAME: 4) a) (10 points) What is the output of the following program? #include <iostream> using namespace std; #define LENGTH 99

void doSomething (int & x, int & y) { y = x / 2; x++; cout << "doSomething " << x << " " << y << endl; } void doSomethingElse (int x, int y) { cout << "doSomethingElse begin " << x << " " << y <<endl; x = 5; y = 20; cout << "doSomethingElse end " << x << " " << y <<endl; } int main() { int x = 2, y = LENGTH + 1; doSomethingElse(x, y); cout << "main mid " << x << " " << y << endl; doSomething(y, x); cout << "main end " << x << " " << y << endl; return 0; }

doSomethingElse begin 2 100 doSomethingElse end 5 20 main mid 2 100 doSomething 101 50 main end 50 101

b) (3 points) What is the output of the following program piece? int a = 'c' - 2; int b = 'f' - 4; cout << char(a + b - 'a') << endl;

NAME: 5) a) (5 points) Rewrite the following piece of code using switch statement instead of if-else statement.
int a; cin >> a; if (a == 1) { cout << "Gelmez dediler switchten soru" <<endl; cout << "Bakma bosuna oraya" << endl; } else if (a == 2) { cout << "Halbuki gelseydim derse" << endl; cout << "Gorurdum cozumu tahtada" << endl; } else { cout<<"Yazayim bari ne bulursam cheat sheet'ten"<<endl; }

switch (a) { case 1: cout << "Gelmez dediler switchten soru" <<endl; cout << "Bakma bosuna oraya" << endl; break; case 2: cout << "Halbuki gelseydim derse" << endl; cout << "Gorurdum cozumu tahtada" << endl; break; default: cout<<"Yazayim bari ne bulursam cheat sheet'ten"<<endl; }

NAME:

b) (3 points) Write a cout statement (only one statement, not more) to display the character of which the ASCII code is 95.

cout << char(95);

c) (3 points) Write a cout statement (only one statement, not more) in order to display the following two lines on the screen.

cout << "\"\\A\"\\\n\"\"";

d) (7 points) Consider the following program. After running this program output becomes as shown below. Fill in the table below with the values of private data members of zar and para objects at the end of the program.
#include <iostream> #include "dice.h" using namespace std; int main() { Dice zar(6); Dice para(2); cout << zar.Roll() << endl; cout << para.Roll() << endl; if (zar.Roll() == 3) { cout << zar.Roll() << endl; } else { cout << para.Roll() << endl; } return 0; }

myRollCount zar para

mySides zar para

Fill these empty boxes

NAME: 6) (13 points) The function below is a partial solution for the following problem: Given two day and month values (month1, month2, day1 and day2) and a startYear value, write a function that returns the count of the same day names (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday) for (day1, month1, startYear) and (day2, month2, startYear) dates starting from startYear up to today. Moreover, this function should also display the same day names and the matching year to the output. However, the function is incomplete. Complete this function by filling the boxes with appropriate expressions. The first box is a single Boolean expression for the while loop. The second box is also a single Boolean expression for the if statement. The third box is for a single statement. You are not allowed to delete or update anything. Moreover, you cannot add anything other than the code that you are going to write in the boxes. int SameDayName(int month1, int day1, int startYear, int month2, int day2) // post: returns if date1 and date2 has the same day name { int count = 0; Date today; while ( { Date date1(month1, day1, startYear); Date date2(month2, day2, startYear); if ( { count++; cout << "Both have same days on a " << date1.DayName()<< " in year " << startYear << endl; } startYear++; } return count; } date1.DayName() == date2.DayName() ) startYear <= today.Year() )

Você também pode gostar