المملكة العربية السعودية
جامعة الإمام محمد بن سعود الإسلامية
كلية علوم الحاسب والمعلومات / / Kingdom Of Saudi Arabia
Al-Imam University
faculty of Computer Science and Information

Dep.: Computer Science Course: Computer Programming 1 (CS140)

Class: Level 1 Dr. Miled Tezeghdanti, Mr. Mourad Bouzegza,

Dr. Walid Aljandal, and Dr. Zouhir Zemirli

Year: 1430-1431 First Semester

Date: 12/01/1431 Allowed time: 1h30

Midterm Exam (7 pages, 4 Exercises)

(Marks: 20 Points)

الاسم
الرقم الجامعي
الشعبة
التوقيع

No books, no notes.

Write in the spaces provided.

-  Be neat.

Mark / Out of
Exercise 1 / 6
Exercise 2 / 4
Exercise 3 / 6
Exercise 4 / 4
Total / 20

Exercise 1 (6 points)

What is the output of the following codes?

1) (2 points)

#include <iostream>

using namespace std;

int main()

{

int num;

cin>num;

if (num%3==0 & num%5==0)

cout<num<" is accepted\n";

else

cout<num<" is refused\n";

return 0;

}

If the user enters the following values:

25

……………………………………………………………………………………

15

……………………………………………………………………………………

2) (2 points)

#include <iostream>

using namespace std;

int valueOne(int);

int main()

{

int x = 5;

cout < "My original number is: " x endl;

cout < "My secret number is: " valueOne(x) endl;

return 0;

}

int valueOne(int y)

{

return ++y;

}

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

3) (2 points)

#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

int x = 5;

cout < "The beautiful secret output is:\n";

for(int i = 1; i 5; i++)

cout< "\n" " "< x " * " " " i <" = " x*i;

cout<endl;

return 0;

}

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

Exercise 2 (4 points)

Write a C++ program that asks the user to enter an integer n and computes the sum of cubes between 1 and n3.

Example 1:

n = 4

n = 3

sum of cubes from 1^3 to 3^3 = 36

//This line should not be shown by your program (1 + 8 + 27 = 36)

Example 2:

sum of cubes from 1^3 to 4^3 = 100

//This line should not be shown by your program (1 + 8 + 27 + 64 = 100)

// Exercise 2

#include <iostream>

using namespace std;

int main() {

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

}

Exercise 3 (6 points)

The goal of this exercise is to draw a table with fixed-width columns and fixed-height rows. Each column should have a fixed width w = 10 spaces. Each row is one-line height.

1) The program should ask the user to enter the number of columns c. The program should make sure that c is in the range [1, 7], otherwise it requests the user to give a new value for c until a value in the range is provided.

2) The program should ask the user to enter the number of rows r. The program should make sure that c is in the range [1, 20], otherwise it requests the user to give a new value for c until a value in the range is provided.

3) The program should print a table with c columns and r rows. Each row is one line height (Don't count the first and the last lines that constitute the table borders). Each column is 10 spaces width (Don’t count the borders between columns).

Example 1:

c = 1

r = 1

************

* *

************

Example 2:

c = 2

r = 3

***********************

* * *

* * *

* * *

***********************

Example 3:

c = 8

c = 0

c = 5

r = 21

r = 0

r = 34

r = 7

********************************************************

* * * * * *

* * * * * *

* * * * * *

* * * * * *

* * * * * *

* * * * * *

* * * * * *

********************************************************

// Exercise 3

#include <iostream>

using namespace std;

int main() {

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

}

Exercise 4 (4 points)

Write a C++ function called IsEven_ButNotMultipleOf_5 that takes an integer as a parameter and returns:

0: if the value of the parameter is not even or if it is a multiple of 5.

1: if the value of the parameter is even and it is not a multiple of 5.

Example 1:

If the value of parameter is 11 the function should return 0

Example 2:

If the value of parameter is 35 the function should return 0

Example 3:

If the value of parameter is 20 the function should return 0

Example 4:

If the value of parameter is 12 the function should return 1

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………

……………………………………………………………………………………