0% found this document useful (0 votes)
4 views

2018 - Final - Spring - PF

PF final exam fast national university Karachi

Uploaded by

k230525
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

2018 - Final - Spring - PF

PF final exam fast national university Karachi

Uploaded by

k230525
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

National University of Computer and Emerging Sciences, Lahore Campus

Course Name: Introduction to Computing Course Code: CS101


Program: BS(CS) Semester: Spring 2018
Duration: 3 hr Total Marks: 80
Paper Date: Monday, 21 May 2018 Weight 40
Section: ALL Page(s): 8
Exam Type: Final Roll No - Sec -

Instruction/Notes: 1. Solve the exam on this question paper. Marks Q1 Q2 Q3 Q4 Q5 Total


2. You can use sheets for rough work but Obtained
do not attach or submit, with paper.

Question # 1: Write the output of following code. [10 Marks]


void doSomeMoreWork(int & m, int & n, char ch){
for (int i = 0; i < n; i++){
for (int j = 0; j < n - i - 1; j++)
cout << ch;
for (int j = 0; j < m; j++)
cout << " ";
for (int j = 0; j < n - i - 1; j++)
cout << ch;
m = m + 2;
cout << endl;
}
n = m + n;
}
void doSomework(int & n, char ch){ int main()
n = n / 2 + 1; {
int m = (n * 2) - 1; int n = 6;
for (int i = 0; i < n - 1; i++){ char ch = '@';
for (int j = 0; j < i; j++) doSomework(n, ch);
cout << ch; cout << "n: " << n << endl;
for (int j = 0; j < m; j++) cout << "ch: " << ch << endl;
cout << " "; return 0;
for (int j = 0; j < i; j++) }
cout << ch;
m = m - 2;
cout << endl;
}
m = 1;
doSomeMoreWork(m, n, ch);
}
Output:

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 diagonal-constant Matrix Not a diagonal-constant Matrix


A B C D E A B C D E
F A B C D F A B K D
G F A B C G F A B C
H G F A B H G F A B
I H G F A I H G F A

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

You might also like