Sie sind auf Seite 1von 9

November 25, 2006

CS 201 (Intro. to Computing) MIDTERM I


1 2 3 4 5 6 7 TOTAL

Name and Last Name :


ID :

Notes: a) Please answer the questions only in the provided space after each question.
b) Duration is 100 minutes.
c) Close-book, close-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) (12 points)

Write a function that takes a string parameter (let's name it str) and returns another string. The
returned string must be composed as follows: the first two characters will be the first two
characters of str. To the end of this, str will be appended entirely.

If there are less than 2 characters in str, then the function must return an empty string (i.e., a
string with no characters in it).

For example, if str is "bekleyin", then the function returns "bebekleyin".


NAME, LASTNAME:

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

#include <iostream>
using namespace std;

double divide (int a, int b)
{
return a/b;
}

double compute (int a, int b)
{
cout << "compute begin: " << a << " " << b << endl;
a = a*b;
b = a+b­1;
cout << "compute end: " << a << " " << b << endl;
return 7/4;
}

int main()
{
int a=1, b=10;
if (a>0)
{
cout << "main first if" << endl;
a­­;
}
else if (a==0)
{
cout << "main second if" << endl;
a­­;
}
else if (a<0)
cout << "main second if" << endl;

a=7;
b = compute(b,a);
double x= divide (10,4);
cout << "main x: " << x << endl;
cout << "main end: " << a << " " << b << endl;
return 0;
}
NAME, LASTNAME:

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

int k;
int i;
for (k=pow(2,18),i=0;k>5;i++)
{
k/=2;
}
cout << i << " " << k << endl;

b) (4 points) Fill in the “Message Displayed” column of the table below according to
following program piece. If no message is displayed, put a dash ( ─ ) in the box.. Leaving a
box blank means you do not know the answer for this case!

if (x)
{ x y Message Displayed
cout << "aaa" << endl;
if (y) true true
cout << "bbb" << endl;
} true false
   else 
   cout << "ccc" << endl; false true

false false

c) (6 points) Write the Boolean test expressions of the following nested if statements so that
the bodies of the statements give correct information about the values of integer variables a 
and b and the program does not crash. Give your answer in the boxes below. No extra code
or variable is allowed other than the required boolean expressions.

if (          b!=0  && a/b > 2                   )

{
  cout << a << " / " << b << " is greater than 2" << endl;
}

else if (            b!=0   &&  a/b <= 2               )

{
  cout << a << " / " << b << " is less than or equal to 2" << endl;
}
else 
{
  cout << a << " / " << b << " is undefined since denominator is 0" <<endl;
NAME, LASTNAME:

}
NAME, LASTNAME:

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

b) (1 point) What is the main difference between a parameter and a local variable?

c) (2 points) How many bits are used to represent:

(i) a long integer variable?

(ii) a double variable?

d) (3 points) Is the following expression syntactically correct? If not, explain the syntax
problem(s). If so, give the result of the expression.

   5+­4­+5*­3/+8­(­10)

e) (2 points) Is the following piece of code syntactically correct? If not, explain the syntax
problem(s). If so, give the values of the variables first, second and third.

double first, second, third;


first = second = third
= 6.26754e4;

f) (2 points) Is the following piece of code syntactically correct? If not, explain the syntax
problem(s). If so, give the value of mychar.

char mychar = "Z";


NAME, LASTNAME:

5) (20 points)
Write a function that takes an integer parameter and returns true if the digit “1” occurs
exactly once in this integer; returns false otherwise.

For examples:
If the parameter is 591254, then the function should return true.

If the parameter is 219413, then the function should return false since there are more than one
1's.

If the parameter is 456, then the function should return false, since there are no 1's in this
integer.

Hint: You do not need to use loops in this question. Think of a string based solution!
NAME, LASTNAME:

6) (14 points)

The program below is a partial solution for the following problem:

Write a program that reads 20 integer numbers from keyboard and displays the
maximum among them.

However, the program is incomplete. Complete this program by filling out the boxes with
appropriate variables/expressions/arguments 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.

#include <iostream>

using namespace std;

int max (int n1, int n2)


{
if (n1 > n2)
return n1;
else
return n2;
}

int main()
{
int i=100;
int num, maxnum;

cin >> maxnum ;

while ( i >= 82 )

{
cin >> num ;

maxnum = max ( num, maxnum );

i--;
}

cout << "max of these numbers is " << maxnum << endl;

return 0;
}
NAME, LASTNAME:

7) (18 points) Write a function that takes a robot and an integer (say n) as parameters. The
function should move the robot n cells towards north if n is a positive even number,
or n cells towards east if n is a positive odd number. During this movement, the robot should
put one thing on each cell visited. If n is not positive, then the robot should not move.

You may assume that there are enough amount of things in the robot's bag. You may also
assume that there are no walls in the robot world.

However, you cannot assume anything about the robot's current direction and position. These
data are not passed to the function as parameters. Your function should work properly no
matter current location and the direction of the parameter robot are.

Robot member functions are listed in the next page for your convenience. You are not
allowed to use any other member functions that we added for homework 5.

The heading of the required function is given below. Your solution should use this heading.

void GoN (Robot & celik, int n)
NAME, LASTNAME:

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 the current cell contains nothing. Returns false if the current cell contains one
or more things.

bool BagEmpty ();
Returns true if the robot’s bag contains nothing. Returns false if the robot’s bag contains one
or more things.

Das könnte Ihnen auch gefallen