Anda di halaman 1dari 8

November 22, 2008

CS 201 (Intro. to Computing) MIDTERM I


1 2 3 4 5 6 7 TOTAL

Name and Last Name : Cscan 201oğlu


ID :
SUNet Username :

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. One A4 size cheat-
note is allowed.
d) There must be eight pages (including this one). Please check it out!

QUESTIONS
1)
a) (1 point) Write the cout statement (not the whole program; just one statement) that
displays your first name, last name, your ID number and your SU email address on the screen.

cout << "Cscan 201oglu, 00000, noname@su.sabanciuniv.edu" << endl;

b) (2 points) What does the function return type of void mean? Please circle the correct
answer.

a) The function returns a numeric value.


b) The function does not return any value.
c) The function can return any value.
d) The function returns a string.

c) (2 points) One of the statements below is syntactically correct, but the others are wrong.
Which one is the correct one? Assume that the variables m and n are defined as int
variables.

a) 3 = m; b) n = 3; c) m + n = 3; d) 3 = m + n;
NAME, LASTNAME:

2) a) (9 points) What is the output of the following program?

#include <iostream>
using namespace std;

int doSomethingElse(int y)
{
y++;
return y;
}

int doSomething(int x, int y)


{
int k;
x *= 2;
k = y + 2;
y = doSomethingElse(x);
cout <<"In doSomething: x="<<x<<" y="<<y<<" and k="<<k<<endl;
return y;
}

int main()
{
int x = 1, y = 8;
cout << "In main: Before: x=" << x << " and y=" << y << endl;
int z = doSomething(x+1, y);
cout <<"In main: After: x="<<x<<" y=" <<y<<" and z="<<z<<endl;
return 0;
}

In main: Before: x=1 and y=8


In doSomething: x=4 y=5 and k=10
In main: After: x=1 y=8 and z=5

b) (7 points) What is the output of the following program piece?

int i, j = 5, k = 0, count = 0;

for (i = 1; i < j; i++)


{
j--;
if (i % 2 != 0)
k++;
else
i--;
count++;
}
cout << i << " " << j << " " << k << " " << count << endl;

2 2 1 3
NAME, LASTNAME:

3)
a) (6 points) What is the output of the following program piece?

string s="ANA";
string t="5";
int i;
for (i=5; i>=1; i--)
{
s = s.substr(0, s.length()/2);
s = s + s;
t = t + t;
}

cout << s << endl;


cout << t.length() << endl;
AA
32

b) (3 points) In the following piece of code, num is an integer variable. Fill in the table
below to specify the output for different values of num.
if (2 <= num <= 5)
{
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
}

num=3 num=10 num=0

Output Yes Yes Yes

c) (3 points) What is the value of the following expression?

2*7%(2-4-(3-8))+(3*(5+4-3))
-5 9
-2
6
3 18

14

20
NAME, LASTNAME:

4) (18 points)
In this question, you may prefer not to attempt to solve it by signing the “not attempted” box
below and secure 4 points. If you sign the “not attempted” box below, you accept that you did
not answer this question and you will receive 4 points. In this case, your answer will not be
graded even if you write something as solution.

Not attempted

Write a function that takes two string parameters (lets name them source and sub). This
function:
− must return 1 if source does not contain sub.
− must return 2 if source contains sub, but source does not start with sub and does not end
with sub.
− must return 3 otherwise.

For example, if source is "borabora" and sub is "ra", then the function returns 3 since
"borabora " contains "ra", but it ends with "ra".

As another example, if source is "borabora" and sub is "bo", then the function returns 3 since
"borabora " contains "bo", but it starts with "bo".

As another example, if source is "borabora" and sub is "abo", then the function returns 2 since
"borabora" contains "abo", but it does not start with "abo" and does not end with "abo".

As another example, if source is "borabora" and sub is "cs", then the function returns 1 since
"borabora " does not contain "cs".

int begin_end_check (string source, string sub)


{
unsigned int begin, end;

begin = source.find(sub);
end = source.rfind(sub);

if (begin == string::npos)
return 1;
else if (begin != 0 && end != source.length() - sub.length())
return 2;
else
return 3;
}
NAME, LASTNAME:

5)
a) (7 points) Fill in the box in the following program piece with an appropriate boolean
expression (condition) so that the output becomes "Bingo" if only two of the input numbers
are equal but the other one is different.

Do not add, delete or update anything else other than the expression that you will write in the
box.

int a, b, c;
cin >> a >> b >> c;

if ( (a == b && a != c) || (a == c && a != b) || (b == c && b != a))

{
cout << "Bingo" << endl;
} Alternative Solutions (Paranthesis may be omitted here and in actual solution)
(a == b && b != c) || (a == c && c != b) || (b == c && c != a)

(a == b && a != c && b != c) || (a == c && a != b && c != b) ||


(b == c && c != a && b != a)

b) (7 points) What should be the initial value of the variable x in order for the following piece
of code to display 48 as the output? Please give your answer by writing in the box within the
code below.

int a, b;
int sum=0;

int x= 6 ;

for (a=1; a<=x; a++)


{
sum +=2;
for (b=1; b<=x; b++)
{
sum +=1;
}
}

cout << sum << endl;

c) (3 points) Given the following declarations and initial values:

int x = 4;
int y = 7;
bool b = false;

What is the value of the following expression?

x == y || x * 2 > y && !b || false

true (1 is also OK)


NAME, LASTNAME:

6) (16 points) The piece of program below is a partial solution for the following problem:

Write a function that reads 100 real numbers from keyboard and returns true if all of
these input numbers are non-negative (i.e. positive or zero). This function should
return false otherwise.

As soon as the function encounters a negative input number, it should not read the rest
of the numbers.

However, the program is incomplete. Complete this program by filling out the boxes with
appropriate expressions so that the program solves the above problem.

You are not allowed to delete or update anything in the program. Moreover, you cannot
add anything other than what you are going to write in the boxes.

string check (double number)


{
if (number < 0.0)
{
return "negative";
}
else if (number == 0.0)
{
return "zero";
}
else
{
return "positive";
}
}

bool read_and_check ()
{ i<100 is also OK instead of i<=99
double input; b == true is also OK instead of b
int i;

bool b = true ;

for (i=0; i<=99 && b ; i++)


{
cin >> input;

if ( check( input ) == "negative" )


{
b = false;
}
}
return b;
}
NAME, LASTNAME:

7) (16 points) Write a program that creates a robot at position (7,0) facing east. Then, the
program should read two integer numbers (say X, Y). After that, your program will then move
the robot to (X, Y) position.

You may assume that X and Y values are entered as non-negative numbers, so you do not need
to make an input check against negative coordinates.

The direction of the robot at the destination is not important. In other words, your robot's
direction may be anything when it reaches (X, Y).

The explanations about GetInput (which is used to read values in Robot applications)
and Robot member functions are given in the next page for your convenience.

(7,0)

#include "Robots.h"
using namespace std;

int main ()
{
int X, Y;
Robot cs (7, 0, east); //east may be omitted
GetInput ("Please enter the X coordinate of destination", X);
GetInput ("Please enter the Y coordinate of destination", Y);

if (X>7)
{
cs.Move(X-7);
cs.TurnRight();
cs.TurnRight();
cs.TurnRight();
cs.Move(Y);
}
else
{
cs.TurnRight();
cs.TurnRight();
cs.Move(7-X);
cs.TurnRight();
cs.Move(Y);
}
return 0;
}
NAME, LASTNAME:

void GetInput(string prompt, string inp);


void GetInput(string prompt, int inp);
GetInput function requires two parameters. First parameter is a string that prompts the user for the
input. The other parameter is a variable, which can be either a string variable or an integer variable.
The function simply prints the prompt inside an input box and assigns the user input to the input
variable. GetInput function may not be used to input several variables; in such a case, you have to call
it several times.

GetInput is NOT a Robot member function. It is a free function.

Robot Member functions


void Move ();
Moves the robot one cell in the direction it is facing. If the robot is blocked by a wall, world boundary,
or another robot, it crashes.
void Move (int numcells);
Moves the robot numcells cells in the direction it is facing. During this journey, if the robot is blocked
by a wall, world boundary, or another robot, it crashes.
bool Blocked ();
Returns true if the robot is blocked by a wall, world boundary, or another robot. Returns false
otherwise.
void TurnRight ();
Changes the direction of the robot (relative to its original direction). For example, if the robot is facing
north, it will be facing east after TurnRight is called.
PickThing ()
Gets one thing from the current cell and put it in the bag of the robot object. If the cell occupied by the
robot is not empty, one thing is transferred to the robot’s bag. That means, the cell content is reduced
by one thing and the bag content of the robot is increased by one thing. If the cell is empty, nothing
happens.
PutThing ()
Gets one thing from the bag of the robot object and put it in the current cell. If the robot’s bag is not
empty, one thing is transferred to the cell occupied by the robot. That means, the cell content is
increased by one thing and the bag content of the robot is decreased by one thing. If the bag is empty,
nothing happens.
void SetColor (Color color);
Changes the color of the robot. Color is an enumerated type defined in Robots.h. It can take the values
white, yellow, red, blue, green, purple, pink, orange.
bool FacingEast ();
Returns true if the robot is facing east. Returns false if it is facing any other direction.
bool FacingWall ();
Returns true if the robot is blocked by a wall or the world boundary (but not when it is blocked by
another robot.) Returns false otherwise.
bool CellEmpty ();
Returns true if current cell contains nothing. Returns false if current cell contains one or more things.
bool BagEmpty ();
Returns true if robot’s bag contains nothing. Returns false if robot’s bag contains one or more things.

Anda mungkin juga menyukai