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

Fall'23 - Mid2 - Solution

Uploaded by

Faizan Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Fall'23 - Mid2 - Solution

Uploaded by

Faizan Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

PF – Mid-2 Solution

Write the output of the given code in the space provided.


(Suppose there is no error)

Q1 20

Sr Code Answer
.
a. for (int i = 1; i <= 3; ++i) *
{
for (int j = 1; j <= i; ++j) *
{ **
for (int k = 1; k <= j; ++k)
cout << '*';
*
cout << endl;
**
}
cout << endl; ***
}
b. for (int i = 1; i <= 4; i++) 0111
{ 1011
for (int j = 1; j <= 4; j++) 1101
{ 1110
if (i == j)
{
cout << "0 ";
}
else
{
cout << "1 ";
}
}
cout << endl;
}
int i = 0;
int j = 5;
while ( i < 10 && j > 0 )
{
c. cout << i * j;
0
j = i - j;
i++;
}
int main()
{
int firstNum = 28;
int secondNum = 25;
cout << (firstNum = 38 - 7) << endl; 31
cout << (firstNum <= 75) << endl; 1
d. cout << (firstNum > secondNum + 10) << 0
endl; 1
cout << (firstNum >= 3 * secondNum - 100) 0
<< endl;
cout << (secondNum - 1 == 2 * firstNum) <<
endl;
}
National University of Computer and Emerging Sciences
Department of Computer Science Chiniot-Faisalabad Campus

int main()
{
Question Skipped due to
int r,a,b,c;
ambiguous statement.
e. r = (a>b?(a>c?5:7):(b>c?9:10));
Marks adjusted in above
cout<<r;
parts.
return 0;
}

2ndMid Term Fall-2023 Page 2 of 8


National University of Computer and Emerging Sciences
Department of Computer Science Chiniot-Faisalabad Campus

Q2 15

Sum =

1. Calculate the power of “X” to “n” using a loop. (3.5 Marks)


2. Calculate the factorial of a number “X” using a loop. (3.5 Marks)
3. Use the solution of parts 1 and 2 to find the sum of the series up to “n” terms. (8 Marks)
Solution:
#include <iostream>
int main()
{
// all necessary variables
double x, sum = 1.0;
int n, even = 2;

// Input values of x and n from the user


std::cout << "Enter the value of X and N: ";
std::cin >> x >> n;

// Part-1: Power of X-to-N


double ans = 1.0;
for (int i = 0; i < n; i++)
{
ans = ans * x;
}

// Part-2: Factorial of X
int fact = 1;
for (int j = 1; j <= x; j++)
{
fact = fact * j; -----------
}

// Part-3: Sum of the series up to n terms


for (int k = 1; k < n; k++) -----------
{
// Correct loop condition for Power -----------
for (int i = 0; i < even; i++)
{
ans = ans * x; -----------
}

// Correct loop condition for Factorial---------


for (int j = 1; j <= even; j++)
{
fact = fact * j; ----------
}

2ndMid Term Fall-2023 Page 3 of 8


National University of Computer and Emerging Sciences
Department of Computer Science Chiniot-Faisalabad Campus

//Alternate addition and subtraction for even/odd iteration


if (k % 2 == 0)
{
sum = sum + ans / fact;
}
else
{
sum = sum - ans / fact;
}

// updating variables after each iteration


even = even + 2;
ans = 1.0;
fact = 1;
}

// Output the result


std::cout << "Sum of the series = " << sum << std::endl;

system("pause");
return 0;
}

2ndMid Term Fall-2023 Page 4 of 8


National University of Computer and Emerging Sciences
Department of Computer Science Chiniot-Faisalabad Campus
Q1.
a. If the number of items bought is less than 5, then the shipping charges are 200.00 for each
item bought; if the number of items bought is at least 5, but less than 10, then the shipping
charges are 150.00 for each item bought; if the number of items bought is at least 10, there
are no shipping charges. Write down the code to calculate the correct shipping charges. 5
//Check for Items below 5
if (numOfItems > 0 && numOfItems < 5)
shippingCharges = 200.0 * numOfItems ;
//To check for at-least five but less than 10
else if (numOfItems >= 5 && numOfItems < 10)
shippingCharges = 150.00 * numOfItems ;
//It must be there to avoid for -ve values
else if (numOfItems >= 10)
shippingCharges = 0.0;

b. Suppose that classStanding is a char variable, gpa and dues are double variables. Write a
switch expression that assigns the dues as following: If classStanding is ‘f’ or ‘F’, the dues
are 20000.00; if classStanding is 's' or ‘S’, (if gpa is at least 3.75, the dues are 15000.00;
otherwise dues are 10000.00); if classStanding is 'j' or ‘J’, (if gpa is at least 3.75, the dues are
9000.00; otherwise dues are 11000.00); if classStanding is 'n' or ‘N’, (if gpa is at least 3.75,
the dues are 5000.00; otherwise dues are 7500.00). (Note that the code 'f' stands for first year
students, the code 's' stands for second year students, the code 'j' stands for juniors, and the
code 'n' stands for seniors.) 10
char classStanding;
double gpa, dues;
switch (classStanding)
{
case 'f':
case 'F':
dues = 20000.0;
break;
case 's':
case 'S':
if (gpa >= 3.75)
//and above gpa

dues = 15000.0;
else
dues = 10000.0;
break;
case 'j':
case 'J':
if (gpa >= 3.75)
dues = 9000.0;
else
dues = 11000.0;
break;
case 'n':
case 'N':
if (gpa >= 3.75)
dues = 5000.0;
else
dues = 7500.0;
2 Mid Term
nd
Fall-2023 Page 5 of 8
National University of Computer and Emerging Sciences
Department of Computer Science Chiniot-Faisalabad Campus
break;
}

c. Write down the following scenario using conditional (ternary) operator. The grade
allocated to the students
5 = 2.5 * 2
i. if (x + y > 15)
y = x;
else if (x == y)
y = 2 * y;
else
y = 2 * x;

y = (x + y > 15) ? x : (x == y) ? 2 * y : 2 * x; preferred solution


OR
(x + y > 15) ? y = x : (x == y) ? y = 2 * y : y = 2 * x;

ii. if (x + y > 15) {


if (x == z)
z = x + 5;
else
z = x;
}
else if (x < y)
z = x + y;
else
z = x * y;

z = (x + y > 15) ? (x == z) ? x + 5 : x : (x < y) ? x + y : x * y;

2ndMid Term Fall-2023 Page 6 of 8

You might also like