Vous êtes sur la page 1sur 10

November 24, 2007

CS 201 (Intro. to Computing) MIDTERM I


1 2 3 4 5 6 7 TOTAL

Name and Last Name :


ID :
SUNet Username :

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 seven 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.

b) (2 points) Circle all of the literals of the following program lines.

string    x12    =     "parazit" ; 

int    circle    =    129   ;

c) (2 points) What is the smallest unsigned integer number?


NAME, LASTNAME:

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

#include <iostream>
using namespace std;

double attir (double, int);
void tuttur (int);

int main()
{
tuttur (9);
return 0;
}

double attir (double p, int q)
{
cout << "attir begin " << p << " " << q << endl;
p = p * 2;
q = q ­ 2;
cout << "attir middle " << p << " " << q << endl;
return p;
cout << "attir end " << p << " " << q << endl;
}

void tuttur (int p)
{
int q;
q = attir(1.45, p­0.5);
cout << "tuttur " << p << " " << q << endl;
}

b) (4 points) Fill in the “Message Displayed” column of the following table below according
to the given piece of program. Assume that a, b, and c are integer variables.

if (a == b == c) Message
a b c
{ Displayed
cout << "all equal" << endl;
} 61 74 0
else
{ 0 0 0
cout << "some differ" << endl;
}
NAME, LASTNAME:
NAME, LASTNAME:

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

int i,j,k;
i=0;
j= 7;
k=0;
while (i!=j)
{
k++;
i++;
j--;
if (j==0)
{
j=9;
}
}
cout << k << endl;

b) (6 points) In the following piece of code x, y and z are three boolean variables. Fill in the
table below to specify for which values of x, y and z, the program piece displays "Fener"
and "Cimbom". Put either true or false in the boxes.
if ( (x && y && z) || (!x && !y && !z) )
if (!y)
cout << "Fener" << endl;
else
cout << "Cimbom" << endl;

x y z
Fener

Cimbom

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

   120/11+(2*(2*(2+2))/0.5)/10
NAME, LASTNAME:

4) (17 points) Write a function that takes a string parameter (let's name it word). The function
should return true if word contains at least one uppercase vowel of English alphabet.
Otherwise (i.e. if word does not contain any uppercase vowel), then the function should return
false.

The uppercase vowels of English alphabet are the characters A, E, I, O and U.


NAME, LASTNAME:

5)
a) (6 points) Fill in the box in the following loop with an appropriate boolean expression
(condition) so that the output is five lines of "hello". Do not add, delete or update anything
else.

int count = 12;
do
{
cout << "hello" << endl;
count = count ­ 7;

} while (       count >= ­16       );

b) (6 points) Consider the following program piece. Assuming that the variable myuint is
entered as a valid unsigned integer from the keyboard, is it possible to see the message
Everything   has   a   limit   in   computers on the screen? If possible, which
myuint value may cause this message to be displayed (you may leave the result as a
mathematical formula and you may use some constants in your answer)?

unsigned int myuint;
cin >> myuint;
if (myuint+1 < myuint)
{
cout << "Everything has a limit in computers" << endl;
}

c) (5 points) Rewrite the following expression using C++ syntax. Please write only an
expression, not a function or a program. When needed, you can use math library functions.
Assume that all variables (x, a and b) are of type double.

ba  5
x  sin(b)
NAME, LASTNAME:

6) (16 points)

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

Write a program that reads a string from keyboard and displays all of the 3-character
substrings of it. If the input string has less than three characters, then the program
should display an error message.

For example, if the input string is cs, then the program should display an error
message since the string cs has less than 3 characters in it.

As another example, if the input string is yazarim, then the output should be:
yaz
aza
zar
ari
rim

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.

#include <iostream>
#include <string>
using namespace std;

int main()
{
string word;
int i;
cin >> word;

if (          word.length() >= 3          )

{
for (i=0;       i <= word.length() ­ 3        ;i++)

{
cout <<           word.substr(i, 3)         << endl;
}
}
else
{
cout<<"Error! The word has less than 3 characters"<<endl;
}
return 0;
NAME, LASTNAME:

}
NAME, LASTNAME:

7) (17 points) Assume that we have three more member functions of the Robot class. These
are getX(), getY() and getDir() member functions. They are described below.

getX(): This function returns an integer value which is the current x position of the robot
object.
getY(): This function returns an integer value which is the current y position of the robot
object.
getDir(): This function returns a Direction type value which is the current direction of
the robot object (i.e. returns east, west, north or south).

The enum type Direction is given below for your convenience.

enum Direction { east, west, north, south };

Now the question comes.

Write a function that takes a robot parameter. The function should return an integer value
according to the following rules:
 The function should return 0 if the parameter robot is at a cell other than (5,5).
 The function should return 1 if the parameter robot is at (5,5) and faces north.
 The function should return 2 if the parameter robot is at (5,5) and faces south.
 The function should return 3 otherwise (i.e. if the parameter robot is at (5,5) and faces
either east or west).

You are not allowed to move or turn the parameter robot in the function. Moreover, you
cannot assume anything about the robot's current direction and position. These data are not
passed to the function as parameters. The only parameter of the function is the robot itself. In
order to learn the direction and the position of this robot parameter, you will need to use the
abovementioned three member functions.

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

int Check (Robot & r)
NAME, LASTNAME:

 The actual Question 7 is the one on this page.

 Please invalidate Question 7 in the stapled booklet by


crossing it over.

 However, please provide your answer on the empty


space after Question 7 on the stapled booklet; not on
this page.

7) (17 points) Assume that we have two more member functions of the Robot class. These are
getX() and getY()  member functions. They are described below.

getX(): This function does not take any parameter and returns an integer value which is the
current x position of the robot object.
getY(): This function does not take any parameter and returns an integer value which is the
current y position of the robot object.

Now the question comes.

Write a function that takes a robot parameter. The function should return an integer value
according to the following rules:
 The function should return 0 if the parameter robot is on the 5th row but not at (5,5).
 The function should return 1 if the parameter robot is on the 5 th column but not at
(5,5).
 The function should return 2 otherwise (i.e. if the parameter robot is at (5,5) or none of
the coordinates are 5).

You are not allowed to move or turn the parameter robot in the function. Moreover, you
cannot assume anything about the robot's current position. These data are not passed to the
function as parameters. The only parameter of the function is the robot itself. In order to learn
the position of this robot parameter, you will need to use the abovementioned two member
functions.

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

int Check (Robot & r)

Vous aimerez peut-être aussi