2018 - Final - Spring - PF
2018 - Final - Spring - PF
Page 1 of 8
Question # 2: [15 Marks]
Write a C++ function “ScrambleWord” that changes the order of characters in a word in a given string. For changing
the order of letters, the simplest thing you could do is either sort the word in ascending or descending order.
For example, if the string given to the function is, “National University of Computer and Emerging Sciences”, and
the word to scramble is “Computer”, then the original string should change to,
“National University of Cemoprtu and Emerging Sciences”, if the word is sorted in ascending order.
Page 2 of 8
Question # 3: [2+3+10= 15 Marks]
Robotics International is starting its new project “RoboCleaner”. RoboCleaner is a robot
which will clean the carpets. For cleaning, it divides a carpet into 8×8 blocks. If there is
any garbage in a cell then those cells in block are marked dirty (by character ‘d’),
otherwise it is marked clean (by character ‘c’). The block having RoboCleaner in it, is
marked with character ‘*’ (star). RoboCleaner always keeps its face towards the first row.
It has four eyes so it can see in four directions i.e. front, back, left and right (from the cell
in which it is standing).
For example, according to the carpet configuration shown in Figure 1, RoboCleaner’s eyes give following readings:
Front Eye – 1 dirty block(s) in the front Back Eye – 0 dirty block(s) at the back
Right Eye – 2 dirty block(s) on right Left Eye – 3 dirty block(s) on left
Your task is to write a program which performs following tasks:
1. Declare a carpet of 8×8 blocks and initialize blocks’ state as given in file carpet.txt and
2. Ask user to enter current position of robot (current row and current column. you can assume that user will never
enter out of bound indices). Mark the cell having robot in it (robot can overwrite a dirty or clean block)
Page 3 of 8
3. Print the carpet.
4. Write a function SeeCarpet which will print the information seen by all four eyes of the robot from its current
position as given in example.
Page 4 of 8
Question # 4: [7+ 3= 10 Marks]
A positive integer is called an Armstrong number, if the sum of cubes of individual digits of number is equal to that
number itself. For example
Number 153 is Armstrong because sum of cubes of individual digits (1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3) = 153
Number 12 is not Armstrong because sum of cubes of individual digits (1 * 1 * 1 + 2 * 2 * 2) = 9
a. Write a C++ function that takes an integer as parameter and finds whether the number is Armstrong or not and
return true or false accordingly.
b. Write a C++ function which takes 1-D array of integers and its size as input and it removes all of the Armstrong
numbers from this array. (May require shifting)
Page 5 of 8
Question # 5: [10+5+15= 30 Marks]
A diagonal-constant matrix, is a square matrix in which each descending diagonal from left to right is constant with the same
value. For instance, the following matrix is a diagonal-constant matrix:
a. Write a C++ function that takes, as parameters a 2D matrix and its dimensions and determines, whether it is a diagonal-
constant matrix or not by returning true or false.
Page 6 of 8
b. Write another C++ function that takes as parameters a 2D matrix and its dimensions and it writes its data in a file
“Output.txt”, if it is a diagonal-constant matrix, output format is given below.
c. Write a C++ function that takes as parameters a 2D matrix and its dimensions and rotates the matrix in anti-clock wise
direction, if it is a constant diagonal matrix.
A diagonal-constant Matrix After Rotation
A B C D E B C D E D
F A B C D A A B C C
G F A B C F F A B B
H G F A B G G F A A
I H G F A H I H G F
Page 7 of 8
Page 8 of 8