You are on page 1of 13

Computing Fundamentals and Programming

Final Exam Solution


Student Name: ______________________________________________

Roll Number: ______________________________________________

Marks: 120

Duration: 180 minutes

1 January, 2018

Rules:
1. All questions are compulsory.
2. Solve all questions on the answering sheet/s
3. Plagiarism, cheating or any act that can result in a possible cheating is prohibited
4. Anybody found talking, having eye contact, looking at other students' work or
showing/sharing his work will be immediately disqualified from the exam.
5. Calculator/ Cell phone/ Digital diary/No gadgets allowed.
6. Work on your exam to the best of your knowledge. No questions require elaboration.
7. If you think a question has a mistake, mention the mistake and don't raise a query
8. Read the statement carefully before you start attempting the question.
Q.1: Write a program in C++ that will: (1+1+4+1+2+1) = 10 Marks

1. Declare a char array of length 30


2. Take a text input of max 30 characters from user
3. For every character in your input, convert ascii value to its triple. (multiply by 3)
4. Print every such tripled number on screen, separated by comma
5. Print every such number in a file named ouput.txt, separated by comma
6. Make sure the input string is not entered empty.

Solution:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
char name[30];
cout << "Please enter a string: ";
cin.getline(name, 30);
if (strlen(name) == 0)
{
cout << "String is empty. Try again";
return 0;
}
ofstream fout("output.txt");

for (int i = 0; i < strlen(name); i++)


{
cout << name[i] * 3 << ",";
fout << name[i] * 3 << ",";

}
return 0;
}

Q.2: Write a program in C++ that will (1+1+3+6+1+1+1+1) = 15 Marks

1. Declare a character array of 10 length


2. Take your roll number as input (for example BSCS17099 ).
3. Assume that your roll number is as per ITU format. Program will give an error if the input is not
as per the ITU format.
4. Program will then separate the char array and will print Degree on one line (BSEE or BSCS)
5. Program will print the year of enrollment on another line (say 17 or 16)
6. Program will print the student number or roll number on another line ( 99 in above example)
7. Program will also print the approximate year of graduation, that is after 4 years (For example for
Batch 17, It will be 21, and for batch 15 it will be 19)
Solution:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
char rollno[10];
cout << "Please enter your rollno: ";
cin.getline(rollno, 10);

if (strlen(rollno) != 9)
{
cout << "Invalid roll number.";
return -1;
}

for (int i = 0; i < 4; i++)


{
if (!isalpha(rollno[i]))
{
cout << "Invalid roll number.b";
return -1;
}
}

for (int i = 4; i < 9; i++)


{
if (!isdigit(rollno[i]))
{
cout << "Invalid roll number.c";
return -1;
}
}

cout << endl << "Degree: " << endl;


for (int i = 0; i < 4; i++)
{
cout << rollno[i];
}

cout << endl << "Year: " << endl;


int year = 0;
for (int i = 4; i < 6; i++)
{
year += (rollno[i] - 48)*pow(10, 1 - (i - 4));
}

cout << endl << "Year : " << year;

int rn = 0;
for (int i = 6; i < 9; i++)
{
rn+= (rollno[i] - 48) * pow(10, 2 - (i - 6));
}
cout << endl << "Roll Number: " << rn << endl;
cout << endl << "Graduation Year: " << year + 4 << endl;

return 0;
}

Q.3: Write a program in C++ that does following without using square brackets notation:

20 Marks

1. Declare an array of 30 float numbers (you can use square brackets here) 3 marks
2. Assign 2.71 to 0th element 3 marks
3. Assign all elements of the array incrementing previous number by 0.23 3 marks
4. Create a function called average that will receive this array as parameter 3 marks
5. Calculate average and return the result to main 3 marks
6. Make sure you call this function in main to show its working. 3 marks
7. De-allocate the array at the end of the program. 2 marks

Solution

#include <iostream>
#include <fstream>
using namespace std;

float average(float *);

int main()
{
float *num = new float[30];
*(num + 0) = 2.71;

for (int i = 1; i < 30; i++)


{
*(num + i) = *(num + i - 1) + 0.23;
cout << num[i] << ", ";
}

cout << average(num) << endl;


delete num;
return 0;
}

float average(float *f)


{

float sum = 0;
for (int i = 0; i < 30; i++)
{
sum += *(f + i);
}
cout << "Sum" << sum << endl;
return sum / 30.0;
}
Q.4: Write a function in C++ named reverse. The function will take a character array as input and will
return the character array reversed in order. For example if given parameter “Super” returns “repuS”.

10 marks

Solution

char* reverse(char *input)


{
int length = strlen(input);
char* temp = new char[length+1];
int i;
for ( i = 0; i < length; i++)
{
temp[i] = input[length - i - 1];
}
temp[i] = '\0';
return temp;
}

Q.5: Write a program in C++ that will take your name as input, and will convert each character of
your name to its ascii number, then add 20 to it and multiply it with 3. After that it will print sum of all
such numbers and it will also print it in a form of Sum equation 0of all numbers. For example, for input
name “Basit” the output will be:
10 Marks

Solution:

#include <iostream>
using namespace std;

int main()
{
char name[100];
cout << "Please enter your name: ";
cin.getline(name, 100);

int sum = 0;
int temp;
for (int i = 0; i < strlen(name); i++)
{
temp = (name[i] + 20) * 3;
cout << temp;
sum += temp;
if (strlen(name) - 1 == i)
cout << "=";
else
cout << "+";
}

cout << sum << endl;


return 0;
}

Q.6: What will be the output of the program if you write your name as input. Replicate above output
box for your name input and changed output on the answer sheet.

10 Marks

Solution

Depends on the user’s name input 😊

For example:

Please enter your name: Abdullah Bin Naufal

255+354+360+411+384+384+351+372+156+258+375+390+156+294+351+411+366+351+384=6363

Q.7: Define following: 10 Marks

• Persistent memory

Computer memeory that stores information and does not get deleted even after you shut down the
computer.

• CPU

Central Processing unit, the component of the computer that performs the arithmetic and logical
operations. It’s the brain of the computer.

• Compiler

A software that converts the source code in a programming language to machine executable code
(machine language).
• Array

Array is a set of consecutive variables declared in the memory of same type. Array has a name and is
declared with one statement.

• Pointer

A special variable that is used to store the address of another variable. It can be used for accessing the
content of the original variable.

Q.8: What is the output of following program? 10 Marks

int main(){

char d[10];

for (int i = 0; i < 10; i++)


{
if (i % 2 == 0)
d[i] = (i + 50) << 1 % 128;
else
d[i] = (i + 120) >> 1;
}

for (int i = 0; i < 10; i++)


cout << d[i];

cout << endl;

return 0;
}

Solution

d<h=l>p?t@
Q.9: Please explain what this program does and what does the output screen look like. More
details you give, better it is, including what does the program do, how does user interact with the
program (input/output), when does the program stop, what is the motive of the program etc.
10 marks

#include <iostream>
#include <Windows.h>
#include <conio.h>
using namespace std;

void checkInput();

int enemyx, enemyy, myx, myy, bulletx, bullety, score;

int main(){
score = enemyx = enemyy = 0;
myx = 25;
myy = 19;
bulletx = bullety = -1;

while (true){

for (int i = 0; i < 20; i++){


for (int j = 0; j < 50; j++){

if (enemyy == bullety && enemyx == bulletx){


cout << "You win " << endl;
exit(0);
}

if (i == myy && j == myx){


cout << "^";
continue;
}
if (i == enemyy && j == enemyx){
if (enemyx > 0){
cout << "<";
enemyx--;
continue;
}
else
enemyx = 49;
}
if(i == bullety && j == bulletx){
if (bullety>0){
cout << "*";
bullety--;
}
continue;
}

if (i == 19)
cout << "-";
else
cout << " ";

checkInput();
}
cout << endl;
}

Sleep(10);
system("cls");
}
}
// code continued
void checkInput(){
char ch;
if (_kbhit()){
ch = _getch();

switch (ch) {
case 65:
myx = (myx > 0) ? --myx : myx;
break;
case 83:
myx = (myx < 49) ? ++myx : myx;
break;
case 32:
bulletx = myx;
bullety = myy - 1;
break;
case 81:
exit(0);
}
}
}

Solution:

• The program is a game


• The letter ^ represents the player’s location. Player is firing at the enemies. Letter ^ is placed
somewhere at the bottom of the screen.
• A row of dashes is placed at the bottom of the game screen, ^ is placed among the dashes on
the ground.
• The letter < represents the enemy plane that is flying from right to left on top of the screen
• The user can press key ‘A’ on keyboard to move the cannon (^ letter) to the left
• The user can press key ‘S’ on the keyboard to move the cannon (^ letter) to the right
• The user can press space on the keyboard to fire the cannon
• The bomb is fired and moves from bottom of screen to the top. * is the bomb
• If the bomb reaches the top, it disappears
• You can fire bombs again and again
• If the bomb hits the enemy, program ends with a message “You Win”
Q.10: Please assume you have this data record as mentioned by following struct: 5 Marks

struct Employee
{
int employeeNumber;
char name[45];
float salary;
int age;
};

If you write a C++ program to store such employees in a file using sorted data (Hint: Random access
filing, remember the hackathon?), answer the following questions:

1. What is the size of each Employee in the memory.

4+45+4+4 = 57

2. What will be the size of each record if we save one employee on each line of file.

Assuming
11 characters for an int, 10 for data, one space
11 characters for a float, 10 for float and one space
46 characters for name including one space
2 for new line
11+46+11+11+2 = 81

3. What will be the size of file if we have to save 10,000 employees in the file (no data yet) ?

810,000 bytes

4. Will the file size change when we add or delete records to this file? Why?

No change, all data will be over-written, file size will remain the same. Empty file had just spaces.

5. What is the drawback of this fixed length field random access filing approach?

Random access filing approach with fixed data will occupy data in file even when it is not being used. So
if the file is expected to save 10,000 records, but there is no data, it will still use 810,000 bytes.
Technically all these bytes are wasted.

Q.11p: Write a program in C++ that 10 Marks

1. Declares two char arrays for file names called file1 and file2
2. Take input of two file names from user into file1 and file 2.
3. Open file1 for reading, open file 2 for writing
4. Read everything in file1
5. Reverse the whole file’s text
6. Writes the reversed data into file2

Solution:

#include <iostream>
#include <fstream>
using namespace std;

int main(){
char file1[40], file2[40];

cout << "Pelase enter file1 name: ";


cin.getline(file1, 40);

cout << "Please enter file2 name: ";


cin.getline(file2, 40);

ifstream fin(file1);
ofstream fout(file2);

if (!fin)
{
cout << "File reading failed" << endl;
return 0;
}

// Calculating file size


int filesize = 0;

while (true)
{
if (fin.get() == -1)
break;

filesize++;

// reading file

char * content = new char[filesize];


fin.close();
fin.open(file1);
char c;

for (int i = 0; i < filesize; i++)


{
content[i] = fin.get();
}

// writing output file

for (int i = filesize-1; i >=0; i--)


{
fout << content[i];
}

fout.close();
fin.close();
return 0;
}
Ascii Table (You’ll need it)
DEC Symbol Description
0 NUL Null char 42 * Asterisk 85 U Uppercase U
1 SOH Start of Heading 43 + Plus 86 V Uppercase V
2 STX Start of Text 44 , Comma 87 W Uppercase W
3 ETX End of Text 45 - Hyphen 88 X Uppercase X
4 EOT End of Transmission 46 . full stop 89 Y Uppercase Y
5 ENQ Enquiry 47 / divide 90 Z Uppercase Z
6 ACK Acknowledgment 48 0 Zero 91 [ bracket
7 BEL Bell 49 1 One 92 \ Backslash
8 BS Back Space 50 2 Two 93 ] bracket
9 HT Horizontal Tab 51 3 Three 94 ^ Caret
10 LF Line Feed 52 4 Four 95 _ Underscore
11 VT Vertical Tab 53 5 Five 96 ` Grave accent
12 FF Form Feed 54 6 Six 97 a Lowercase a
13 CR Carriage Return 55 7 Seven 98 b Lowercase b
14 SO Shift Out / X-On 56 8 Eight 99 c Lowercase c
15 SI Shift In / X-Off 57 9 Nine 100 d Lowercase d
16 DLE Data Line Escape 58 : Colon 101 e Lowercase e
17 DC1 Device Control 1 59 ; Semicolon 102 f Lowercase f
18 DC2 Device Control 2 60 < Less than 103 g Lowercase g
19 DC3 Device Control 3 61 = Equals 104 h Lowercase h
20 DC4 Device Control 4 62 > Greater than 105 i Lowercase i
21 NAK Negative Ack 63 ? Question mark 106 j Lowercase j
22 SYN Synchronous Idle 64 @ At symbol 107 k Lowercase k
23 ETB End of Transmit Block 65 A Uppercase A 108 l Lowercase l
24 CAN Cancel 66 B Uppercase B 109 m Lowercase m
25 EM End of Medium 67 C Uppercase C 110 n Lowercase n
26 SUB Substitute 68 D Uppercase D 111 o Lowercase o
27 ESC Escape 69 E Uppercase E 112 p Lowercase p
28 FS File Separator 70 F Uppercase F 113 q Lowercase q
29 GS Group Separator 71 G Uppercase G 114 r Lowercase r
30 RS Record Separator 72 H Uppercase H 115 s Lowercase s
31 US Unit Separator 73 I Uppercase I 116 t Lowercase t
32 Space 74 J Uppercase J 117 u Lowercase u
33 ! Exclamation mark 75 K Uppercase K 118 v Lowercase v
34 " Double quotes 76 L Uppercase L 119 w Lowercase w
35 # Number 77 M Uppercase M 120 x Lowercase x
36 $ Dollar 78 N Uppercase N 121 y Lowercase y
37 % Percent 79 O Uppercase O 122 z Lowercase z
38 & Ampersand 80 P Uppercase P 123 { Opening brace
39 ' Single quote 81 Q Uppercase Q 124 | Vertical bar
40 ( Open bracket 82 R Uppercase R 125 } Closing brace
41 ) Close bracket 83 S Uppercase S 126 ~ tilde
84 T Uppercase T 127 Delete

You might also like