You are on page 1of 9

April 6, 2010

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) Closed-book, closed-notes, no calculators, no mobile phones and computers.
One A4 size cheat-note is allowed.
d) There must be 8 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 << " Gulsen Demiroz, 1480, gulsend" << endl;

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

(i) a long integer variable? 4 bytes = 32 bits

(ii) a double variable? 8 bytes = 64 bits

c) (2 points) One of the statements below does not give a syntax error, but the others give a syntax
error when compiled. Which one is the one with no syntax errors? You may assume x is defined as an
int variable.

a) x = 6.26754e4;

b) x - 3 += 2;
c) char mychar = "Z";
d) true = x;
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;
}

int calculate (int a, int b)


{
cout << "calculate begin: " << a << " " << b << endl;
a = b / a + 1;
b = a + b * 2;
cout << "calculate end: " << a << " " << b << endl;
return b;
}

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++;
}
if (a==0)
cout << "main third if" << endl;

b = calculate(b, a + 2);
double x = divide (b*4, a+3);
cout << "main x: " << x << endl;
cout << "main end: " << a << " " << b << endl;
return 0;
}

main first if
main third if
calculate begin: 10 2
calculate end: 1 5
main x: 6
main end: 0 5
NAME, LASTNAME:

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

int main()
{
int i, c;
for (i=pow(2.0,16), c=0; i > 4; c++)
{
i = i / 8;
}
cout << c << " " << i << endl;
}

5 2

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

11%4-5*-8/2-9

3 -40/2

3 - -20 - 9

23 - 9

14

c) (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 "istanbul" and "izmir". Put
either true or false in the boxes.

if ( (x && y && z) || (!x && !y && !z) )
if (z)
cout << "izmir" << endl;
else
cout << "istanbul" << endl;

x y z
izmir true true true

istanbul false false false


NAME, LASTNAME:

4) (16 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, s1 and s2, and an integer parameter count. This
function will take count many characters from s1, appends the ‘-‘ character to it, then appends count
many characters from s2 and lastly appends the ‘-‘ character. Then it should repeat this until it
reaches the end of the strings. Then the function returns this resulting string.
You may assume that the length of s1 and s2 are equal and that length is greater than 0. You may also
assume count is also greater than 0.

Examples:
s1 = “abcdef”, s2 =”ghijkl”, count =3, then the function should return “abc-ghi-def-jkl” .
s1 = “abc”, s2 =”ghi”, count =1, then the function should return “a-g-b-h-c-i” .
s1 = “abc”, s2 =”ghi”, count =4, then the function should return “abc-ghi” .

Your result should not end with the ‘-‘ character.

string RoundConcat (string s1, string s2, int count)


{
string result = "";
unsigned int i;
for (i = 0; i < s1.length(); i += count)
{
result += s1.substr(i, count) + "-";
result += s2.substr(i, count) + "-";
}
result = result.substr(0, result.length()-1);

return result;
}
NAME, LASTNAME:

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


int main()
{
string s = "yazayazayaz";
string result = "";
unsigned int i, c = 2;
for (i = 0; i < s.length(); i += c)
{
s = s.substr(c, s.length());
result += s.substr(i, c);
}
cout << s << " " << result;
return 0;
}
zayaz zazaz

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


#include <iostream>
using namespace std;
int main()
{ int a = 4;
cout << "a before for loop is " << a << endl;
for (int b=0; b < 4; b++)
{ if (b%2 == 0)
{ int a = b+1;
cout << a << endl;
}
else
{ a = a + b;
cout << a << endl;
}
}
cout << "a after for loop is: " << a << endl;
return 0;
}
a before for loop is 4
1
5
3
8
The value of a after for loop is: 8

c) (4 points) Write the Boolean test expressions of the following if statements so that the bodies of
the statements give correct information about the values of integer variable x. No extra code or
variable is allowed other than the required boolean expressions.
int x;
if ( (x % 2 == 0) && (x >= 0) )
cout << “x is a positive even integer” << endl;
else if ( x >= 0 )
cout << “x is a positive odd integer” << endl;
NAME, LASTNAME:

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

Write a program that reads real numbers from the keyboard until -1 is read as the input. As
soon as the program encounters -1 as a number, it stops asking for more numbers. Then it
displays the total of all numbers and the average of these numbers (excluding the -1 of
course). If there are no numbers entered other than -1, then the program displays "No
numbers were entered".

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.

int main()
{
double number; // number entered by the user
double total = 0; // the total of numbers
int count; // count of numbers entered

count = -1 ;

do
{
cout << "Enter a real number or -1 to quit: ";

cin >> number ;

total = total + number ;

count = count + 1 ;

} while ( number != -1 );

if (count != 0)
{
cout << endl << "Total of " << count
<< " numbers = " << total+1 << endl;
cout << endl << "Average of " << count

<<" numbers = "<< (total+1) / count ;


}
else
cout << "No numbers were entered.";

return 0;
}
NAME, LASTNAME:

7) (20 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 program that creates a robot at one of the 4 corner positions ((0, 0), (0, 8), (8, 0), (8, 8)) inside
a 9-by-9 bounded world facing east. First, the program should read two integer numbers (say X, Y)
where (X, Y) is one of these 4 corners. Then it creates the robot at this position (X, Y). After that, your
program will then move the robot to (4, 4) position from any of these four corners.

You may assume that X and Y values are valid, such that they are either equal to 0 or 8, so you do not
need to make an input check against other 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 (4, 4).

The explanations about GetInput (which is used to read values in Robot applications) and Robot
member functions are also given below:

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. Input box has two bottoms, “OK” and “Cancel”. “OK” button is used to
assign the input to the variable, and “Cancel” button assigns an empty string to the variable if
the type of variable is string, or similarly 0 is assigned if the type of the variable is integer. If
the user enters a non-integer value where integer is expected, then the value of the integer
becomes 0. GetInput function may not be used to input several variables; in such a case, you
have to call it several times.

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.

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.
NAME, LASTNAME:

Robot created at position (0,0) Robot created at position (0,8)

Robot created at position (8,0) Robot created at position

#include "Robots.h"

void TurnLeft(Robot & rob)


{
rob.TurnRight();
rob.TurnRight();
rob.TurnRight();
}

void TurnBack(Robot & rob)


{
rob.TurnRight();
rob.TurnRight();
}
NAME, LASTNAME:

int main()
{
int xpos,ypos;

GetInput("Please Enter X position of the robot", xpos);


GetInput("Please Enter Y position of the robot", ypos);
Robot rob(xpos, ypos, east);

// top left corner


if (xpos == 0 && ypos == 8)
{
rob.Move(4); // move to (4,8)
rob.TurnRight(); // face south
}
// bottom left corner
else if (xpos == 0 && ypos == 0)
{
rob.Move(4); // move to (4,0)
TurnLeft(rob); // face north
}

// top right corner


else if (xpos == 8 && ypos == 0)
{
TurnBack(rob);
rob.Move(4); // move to (4,0)
rob.TurnRight(); // face north
}
// bottom right corner
else if (xpos == 8 && ypos == 8)
{
TurnBack(rob);
rob.Move(4); // move to (4,0)
TurnLeft(rob); // face north
}
rob.Move(4); // move to (4,4)

return 0;
}

You might also like