You are on page 1of 6

The American University in Cairo

Computer Science & Engineering Department


CSCE 1001-03&04
Dr. KHALIL Midterm Exam II Spring 2020
First Name: Mahmoud ID: 900192592

Last Name: Elshablangy Form – I Section No. (3)

Please, Read Carefully the Following Exam Instructions


1. Exam time is 60 minutes.
2. The Exam is time-constrained. The Exam starts at 12:15 pm and ends at
1:15 pm
3. Download the exam document from your Bb once it became available and
add your name, Id and section number.
4. Type your answer for each question in the given free space. Do your
rough on external sheet.
5. Re-submit the exam document, including your answers, to your Bb
before the due time (1:15 pm).
6. You will have two submission attempts. Recommended to do the first
attempt at 1:00 pm and the second before 1:15 pm. Only the last
submission that will be graded.
7. Any violation of Academic Integrity (Cheating, Plagiarism, ) will be
reported to the university in addition of failing the exam.
8. Any submission through the email will not be considered.
---------------------------------------------------------------------------------------------------------------------------
DO NOT USE THIS SECTION
Question Points Grade
1 15

2 15

3 10

4 20

5 20

6 20

7 (Bonus) 10

Total 100 + 10

1
Question 1 ( 15 points)

Define a C++ function Rotate that takes Definition of function Rotate:


three integers a, b and c to rotate them
(i.e., give the value of a to b, the value of
b to c and the value of c to a) and return
them back. For example, if the function
takes a, b and c as 6, 2 and 8, it returns
them as 8, 6 and 2, respectively.
(Function Rotate should include only one
additional variable).

Question 2 ( 15 points)

Write a C++ recursive function SEARCH that


takes a 1-D array A of characters, start index
ss, end index ee and one single character
received over the parameter cc. The function
searches the array for the character carried
by cc, and if it finds an element with the
same character, it returns the index of the
element. If no matching with any element,
the function returns -1.

Question 3 (10 points)


What will be the content of file out.dat when the following lines are executed, assuming the file
in.dat contains the data shown (and assuming the lines are embedded in a complete and
correct program with the proper include directives)?

int num, c = 0; File in.dat File out.dat


ifstream ins;
ofstream outs; 543
ins.open(“in.dat”); 21
outs.open(“out.dat”);
while ( ins >> num )
{ outs << num;
c++;
}
outs << c << endl;
ins.close();
outs.close();

2
Question 4 ( 20 points)

Define a struct type circle described by


three member variables: X, Y (co-
ordinates of its center) and radius.

Write C++ statement to create objects


C1 and C2 of type circle. Initialize C1
with the values 1.5, 2.5 and 4.5 for X,
Y and radius, respectively. Initialize C2
with the values -1.6, -3.5 and 3.5 for X,
Y and radius, respectively.

Write C++ function dist that takes two


objects of type circle to compute and
return the distance between their
centers.
(The distance between two points (x1,
y1) and (x2, y2) = √ (x 1−x 2)2 +¿ ¿ ¿)

Write only the C++ statements of a


testing main function to display the
distance between the two objects C1
and C2 (created before) of type circle.

3
Question 5 (20 points)
Write the C++ statements for each of the following operations
Operation C++ statement(s)

Create pointer variables A and B,


each can "point" to a variable of
type int.

Create pointer variable D that can


"point" to a variable of type double.

Create a named integer variable


num to be initialized by 25.

Let pointer A point to variable


num.

Let pointer B point to the same


variable num.

Show the three possible statements


to add value 5 to the variable num.

Create a dynamic nameless integer


variable using pointer A and assign
it value 35.

Create a dynamic array B with size


20.

Remove the dynamic variable


pointed to by A

Remove the dynamic array B.

4
Question 6 ( 20 points)

Write a C++ function POP that takes an


integer 2-D array of size 20x20 to populate
each of its elements with a random integer
number x such that 9 < x < 19.

Write a C++ function SUMR that takes an


integer 2-D array of size 20x20 to compute
the sum of each of its rows. The function
stores the sum of each row in 1-D array
which is returned.

Write a testing main function that creates an The correct answer


intgere 2-D array ARR of size 20x20 and 1-D void SUMR( int A[20][20], int BR[20]) {
for (int row = 0; row < 20; row++) {
array BR of size 10, calls the POP function BR[row] = 0;
to populate the 2-D array with the random for (int c = 0; c < 20; c++)
numbers, calls the SUMR function to get the BR[row] = BR[row] + A[row]
sum of rows in the 1-D array, and finally [c];
displays the content of the 2-D array ARR }
}
and 1-D array BR in proper format.
int main() {
int ARR[20][20], BR[20];
POP(ARR);
SUMR(ARR, BR);
for (int row = 0; row < 20; row++) {
for (int c = 0; c < 20; c++)
cout << ARR[row][c] << "
";
cout << BR[row] << endl;
}
return 0;

5
Question 7 ( 10 points) (Bonus)
Select ( make it underlined bold) the correct answer from the following:

1. A function that calls itself is called:

a. Self referring
b. Recursive
c. Local
d. Anonymous
 
2. What does the following code do?
 
int *p;
p = new int;
 
a. Allocates an integer memory location called p.
b. allocates an integer memory location and save the address in p.
c. changes the type of p to be of type in not pointer.
d. Deallocates an integer called p from memory.
 
The following code contains portions of a C++ program. They are assumed to exist inside a proper
main function. There are no syntax errors in the code. Answer the questions accordingly.
 
3. What is wrong with the functionality of the following C++ code?
         //This code is supposed to create a 2D array of size 5x5
        //and initialize its contents to some numbers (doesn’t matter)
        //except that one of diagonals must be set to zero

        int x [5][5];


       
        for ( int i=0; i<5; i++)
            for ( int j=0; j<5; j++)
{
                if (I == j)
x[i][j] = 0;
                else
x[i][j] = 100;
                x[i][j] = 100;
            }

a. Not all the elements in the array are set to a value


b. None of the diagonals are properly set to 0
c. The first row is the one set to 0
d. The first column is the one set to 0
 
4. What is the output of the following C++ code: 
int x= 915;
            int y; int z;
            y = x / 10;
            z = y % 10;   
            cout << z;
 
a. 91
b. 5
c. 1
d. 15

Good Luck
6

You might also like