You are on page 1of 12

Lahore University of Management Sciences

BS Programme

Name SOLUTION Section: Roll Number :


Course Title: Computational Problem Solving Semester: Fall
Course Code: CS 100 Academic Year: 2017-2018
Instructor: Dr. Shafay, Dr. Rizwan, Zaid Date: October 20, 2017
Exam: Midterm Time Allowed: 100 Minutes
Total Pages: 12 Total Marks: 100

DO NOT OPEN THIS EXAM UNTIL TOLD TO DO SO.

The instructions below must be followed strictly. Failure to do so can result in serious grade loss.
⇒ Do not talk to anyone once the exam begins.
⇒ Keep your eyes on your own paper.
⇒ Read all questions very carefully before answering them. All questions are compulsory.
⇒ Only answers written within the given area will be marked.
⇒ You may use page 5 and the sides of other pages for rough work.
⇒ Put your pens down immediately when you are asked to. Marks may be deducted if you keep writing.
⇒ There are multiple versions of the exam. Do not try to copy from others. Cheating cases will be
severely dealt with.
⇒ You are not required to include libraries or define the main function except in questions where you are
asked to write a complete program.

Specific instructions:

1. Open book/notes, closed book/notes, help sheet: Closed Book / Closed Notes.

2. Calculator usage: Not allowed.

3. Write in pen/pencil: Any. No red pen.

4. Any other instruction(s): Mobile phones are not allowed.

Question 1 2 3 4 5 6 7 8 9 Total

Max Marks 10 10 10 10 10 10 10 10 20 100

Marks Obtained

1
Question 1 [10 marks]

a. Which of the following cannot be used as a variable name?


(circle the answer) [1]
i. 1Price [1]
ii. Price1
iii. price1
iv. _price1
v. Price_1

b. What is the value of Total at the end of the following code? [2]
int price = 10;
int items = 2;
int Items = 3;
int Price = price + 5;
Items = items;
int Total = Price * items;

Total = 30 [2]

c. What are the values of c, d, and e at the end of the following code? [3]
string a = "CS";
string b = "100";
string c = a + b;
int d = a.length() + b.length();
string e = b.substr(1,2);

c = CS100 [1] d = 5 [1] e = 00 [1]

d. What will be the output of the following statements? [2]


double p = 3.14159;

  3 . 1 4 1 5 9
cout<<setw(8)<<p;    [1]  
cout<<setw(8)<<setprecision(4)<<p;   3 . 1 4 2
[1]

e. What are the values of x and y at the end of the following code? [2]
double a = 22 / 7;
int b = 2.717;
double x = -a * b + a / 2.0;
int y = pow(2,a) * (a % 2);

x = -4.5 [1] y = 8 (question cancelled) [1]


[+1 for everyone because of the cancelled question]
Question 2 [10 marks]

The code below contains 7 errors in total. Find each error, identify whether it
is a syntax (compile time) or logical (runtime) error, and mention how the
error can be fixed. Two of the errors are already done for you as examples.

1 #include <iostream>
2 using namespace std;
3 int Main()
4 {
5 const int days = 7;
6 int daily;
7 double raise = 5%;
8
9 if (daily < 0);
10 daily = 0;
11
12 days = 365;
13 double new_daily = daily * (1 + raise);
14 cout << "New yearly salary:' << new_daily * days;
15
16 return 0
17 }

Line (order not


Error Location Type Correction
important)

3 Main syntax main (small m)

16 0 syntax 0; (add semicolon)

6 daily logical Initialize variable daily


[0.5] [0.5] [1]
7 5% Syntax should be 0.05
("Remove %" gets credit)
[0.5] [0.5] [1]
9 ; logical Remove semicolon
[0.5] [0.5] [1]
12 days = Syntax Remove "const" from line
5 or don’t change value of
[0.5] days
[0.5] [1]
14 :' syntax Replace ' by "
[0.5] [0.5] [1]

3
Question 3 [10 marks]

a. Write a line of code for computing the following expression. Assume the
variables are already defined. [3]
! !
𝑧 =   𝑥 + 𝑦 − 2𝑥𝑦 cos 𝑎
z = sqrt(x*x + y*y – 2*x*y*cos(a))

[1] – Using sqrt function


[1] – Using cos function
[1] – Other arithmetic stuff

b. Write a complete program for distance conversion from km to miles. The


program asks the user to enter distance in km and displays the distance in
miles. Assume that the user provides valid input (i.e. you do not need to
check for incorrect input). (Hint: 1 km = 0.62 mile) [3]
#include <iostream>
using namespace std;
int main()
{
cout << "Enter distance in km: ";
double km;
cin >> km;
double miles = 0.62 * km;
cout << "That is " << miles << "miles";

return 0;
}
Note:
• Using data type float or double for km and miles [1]
• Syntax of cout and cin [1]
• iostream, namespace, main, etc. [1]

c. Write a piece of code that initializes two character variables (to any values
of your choice), swaps their values, and then displays them. [4]
char ch1 = 'a'; char ch2 = 'b';

char temp = ch1;

ch1 = ch2;

ch2 = temp;

• Attempted and did something [1]


• Major mistakes in code [2]
• Minor mistake in code [3]
• Completely correct [4]

4
Question 4 [10 marks]

The code below identifies whether a given number is prime or not. Draw the
flowchart for this code.

int n;
cout << "Enter an integer greater than 1: ";
cin >> n;

bool pr = true;
int i = 2;
while (i <= n / 2)
{
if (n % i == 0)
{
pr = false;
}
++i;
}
if (pr)
cout << "This is a prime number";
else
cout << "This is not a prime number";

Draw the flowchart on the next page. You may use this page for any rough
work for any question.

Programmer joke: !false


It’s funny because it’s true…

5
[Flowchart]

• Attempted and did something (input, output, etc.) [3]


• One decision statement is correct (while or if) [5]
• Both decision statements correct (while and both if) [7]
• Minor mistake [9]
• Completely correct [10]

6
Question 5 [10 marks]

a. The code below takes the integer num from the user and computes
another integer b. What is the output of the code for each given input?
Can you explain what the code is doing to num? [6]

int num, temp, rem, b = 0, place = 1;

cout << "Enter an integer: ";


cin >> num;
temp = num;

while(temp > 0)
{
rem = temp % 2;

b = b + (rem * place);

temp = temp / 2;

place = place * 10;


}

cout << b;

num b

100 [2]
4

101 [2]
5

Explanation = Code is converting num into its binary value [2]

7
b. The code below calculates the tax due on a person’s salary. It has
some logical errors in the branching conditions. What is the output of
this code for each given input? [4]

double salary;
bool tax_free;
double tax_rate = 0.10;
double tax;
const int min_taxable = 1000;

cout << "Enter salary and tax status (exempt or not): ";
cin >> salary >> tax_free;

if (salary > min_taxable) { tax_rate = 0.05; }

else if (salary > 2 * min_taxable) { tax_rate = 0.10; }

else if (salary > 3 * min_taxable) { tax_rate = 0.15; }

else if (salary < min_taxable) { tax_rate = 0.00; }

else if ( tax_free ) { tax_rate = 0.00; }

tax = salary * tax_rate;


cout << tax;

salary tax_free tax

1000 1 (true) 0 [1]

1000 0 (false) 100 [1]

500 0 (false) 0 [1]

4000 1 (true) 200 [1]

8
Question 6 [10 marks]

The following code has no errors. What will be the value of variables a, b,
c, d, and e at the end of the code? Identify what each of these variables
are counting.

int a = 0, b = 0, c = 0, d = 0, e = 0;
string s = "Midterm Exam CS100";
for(int i = 0; i < s.length(); i++)
{
if(s.substr(i,1) == "a" || s.substr(i,1) == "A"
|| s.substr(i,1) == "e" || s.substr(i,1) == "E"
|| s.substr(i,1) == "i" || s.substr(i,1) == "I"
|| s.substr(i,1) == "o" || s.substr(i,1) == "O"
|| s.substr(i,1) == "u" || s.substr(i,1) == "U")
a++;
else if((s.substr(i,1) >= "a" && s.substr(i,1) <= "z")
|| (s.substr(i,1) >= "A" && s.substr(i,1) <= "Z"))
b++;
else if (s.substr(i,1) >= "0" && s.substr(i,1) <= "9")
c++;
else if (s.substr(i,1) == " ")
d++;
else
e++;
}

Variable Value Purpose/Function

4 Counts number of vowels


a [1] [1]

9 Counts number of consonants (non vowels)


b [1]
[1]
3 Counts number of digits
c [1]
[1]
2 Counts number of spaces
d [1]
[1]
0 Counts number of characters other than letters,
e numbers and spaces
[1]
[1]

9
Question 7 [10 marks]
Write a piece of code that takes three different integers as input from the
user and prints two things:
a. The integer that is neither the biggest nor the smallest.
b. How many of the integers are odd.
Assume that the user provides valid input (i.e. you do not need to check for
incorrect input).

int n1, n2, n3;

cout << "Enter 3 integers: ";

cin >> n1 >> n2 >> n3;

cout << "The middle number is ";

if (n1 < n2 && n2 < n3) { cout << n2; }

else if (n1 < n3 && n3 < n2) { cout << n3; }

else if (n2 < n1 && n1 < n3) { cout << n1; }

else if (n2 < n3 && n3 < n1) { cout << n3; }

else if (n3 < n1 && n1 < n2) { cout << n1; }

else if (n3 < n2 && n2 < n1) { cout << n2; }

int odd = 0;

if (n1 % 2 == 1) { odd++; }

if (n2 % 2 == 1) { odd++; }

if (n3 % 2 == 1) { odd++; }

cout << odd << " of the numbers are odd " << endl;

Attempted and did something (input, output) [2]

Part a Part b
Used decision statements [1] Used decision statements [1]
Correct syntax of decision Correct syntax of decision
statements + major error in statements + major error in
conditions [2] conditions [2]
Correct syntax of decision Correct syntax of decision
statements + minor error in statements + minor error in
conditions [3] conditions [3]
Completely correct [4] Completely correct [4]
10
Question 8 [10 marks]

Write a piece of code that takes in two positive integers m and r from the
user and prints all the numbers less than r that are exact powers of m.
Assume that the user provides valid input (i.e. you do not need to check for
incorrect input).

int m, r;

cout << "Enter m and r: ";

cin >> m >> r;

int po = m;

for (int i = 2; po < r; i++)

cout << po << endl;

po = pow(m,i);

• Attempted and did something [3]


• *Attempted + partially correct logic [5]
• *Attempted + good logic [8]
• Completely correct [10]

11
Question 9 [20 marks]

Write a piece of code that takes in a time from the user: one number
between 1 and 12 for hours, another number between 0 and 59 for minutes,
and a string for AM or PM. It then tells the user whether the time he/she has
entered is before, after, or equal to your bedtime (8:00 PM). Assume that the
user provides valid input (i.e. you do not need to check for incorrect input).

int h, m;

string ampm;

cout << "Enter hour, minute, and AM/PM: ";

cin >> h >> m >> ampm;

if (h == 8 && m == 0 && ampm == "PM")

cout << "The time is equal to your bedtime";

else if (h >= 8 && h < 12 && ampm == "PM")

cout << "The time is after your bedtime";

else

cout << "The time is before your bedtime"

• Attempted and did something [4]


• *---------[8]
• *-------- [12]
• *-------- [16]
• Completely correct [20]

12

You might also like