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

programming workbook arrays

Uploaded by

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

programming workbook arrays

Uploaded by

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

National University of Computer and Emerging Sciences

School of Computing Fall 2018 Islamabad Campus

Serial No:
CS118
Mid Term
Programming Fundamentals Total Time: 2 Hour
Wednesday, October 24, 2018 Total Marks: 75
Course Instructor
Jawad Hassan, Amna Irum ________________
Signature of Invigilator
Atifa Sarwar

____________ ______________ ___________________ _____________________


Student Name Roll No Section Signature

DO NOT OPEN THE QUESTION BOOK OR START UNTIL INSTRUCTED.


Instructions:
1. Attempt on question paper. Attempt all of them. Read the question carefully, understand
the question, and then attempt it.
2. Please read the complete paper before attempting any question and manage your time
intelligently.
3. Additional sheet are provided for rough work at the end.
4. If you need more space write on the back side of the paper and clearly mark question
and part number etc.
5. After asked to commence the exam, please verify that you have twelve (12) different
printed pages including this title page. There are total of 6 questions.
6. Use permanent ink pens only. Any part done using soft pencil will not be marked and
cannot be claimed for rechecking.
7. Use proper indentation while writing code and make sure that your code is legible.
Failing to do so can cost you marks.

Q-1 Q-2 Q-3 Q-4 Q-5 Q-6 Total


Marks
Obtained
Total
20 15 10 10 10 10 75
Marks

Vetted By: _________________________Vetter Signature: _____________________

Page 1 of 12
National University of Computer and Emerging Sciences
School of Computing Fall 2018 Islamabad Campus

Question I ...…………………………………………………………………………. (20 Marks)


Write the output produced by executing the following code? Please write proper explanation of
the bug where required, without proper explanation no marks will be awarded. [2 marks each]

Code Output

int main( ) {

int x = 8, y = 0, z ;
while ( x >= 0 && y <=5 )
{
if ( x == y )
break ;
else
cout<<x<<y ;
x-- ;
y+=2 ;
}
return 0;
}

int main(){

const int U = 8, L = 2;
int n1, n2, n3 = 12, n4 = 3;
n1 = n3 > n4 ? n4 > U ? n3 : L : L;
cout << n1 << endl;
return 0;
}

int main(){

char alphabet= ‘A’;


for(int i = (‘F’-‘A’+1); i >= 1; --i)
{
for(int j = 1; j <= i; ++j)
{
cout << alphabet << " ";
}
++alphabet;

cout << endl;


}

return 0;
}

Page 2 of 12
National University of Computer and Emerging Sciences
School of Computing Fall 2018 Islamabad Campus

int main (){

int x, y;
x = 5;
y = ++x * ++x;
cout << x << y;
x = 5;
y = x++ * ++x;
cout << x << y;
return 0;
}

int main (){

int z=1;
for(int i = 5 ;i >= 1; --i)
{
z=i ;
for(int z = 1; z <= i; ++z)
{
cout << z << " ";
}
++z;

cout << i << z<< endl;


}
return 0;
}

int main(){

int x = 1, y =2, n=50 ;


while (y <=n )
{
if ( n%y == 0 )
{ n=n/y;
x=x+1; }
else
{ y=y+1; }
cout<<x<<y;
}
return 0;
}

Page 3 of 12
National University of Computer and Emerging Sciences
School of Computing Fall 2018 Islamabad Campus

int main() {
int x = 6, y = 7, z ;
while ( x >= 0)
{
while (y >=0)
{
cout<< y-- ;
}
cout<< x<<”\n” ;
y=x-- ;
}
return 0;
}

int main(){

int suite = 5 ;
switch ( suite ) ;
{
case 0+5 ;
cout<< "\nClub" ;
case 1+5 ;
cout<< "\nDiamond" ;
}
return 0;
}

int main() {
int a = 0, b=36;
float f=3.9;
b+= (a = 50)*f/3*10-b%5;
cout << a << "$" << b;
return 0;
}

int main() {
float x = -10;
while (x)
{
x *= 10;
x += 10;
}
cout << x << endl;
return 0;
}

Page 4 of 12
National University of Computer and Emerging Sciences
School of Computing Fall 2018 Islamabad Campus
Question II ……………………..……………………………………………………. (15 Marks)
a) (5 marks) Write a C++ program using if-elseif-else to input basic salary and allowances of
an employee and calculate its Gross salary according to following:

Basic Salary <= 10000: HA = 20% of Basic Salary


Basic Salary <= 20000: HA = 25% of Basic Salary
Basic Salary > 20000: HA = 30% of Basic Salary

Gross salary is the final salary computed after the additions of HA (House Allowance) into
the Basic Salary.

Page 5 of 12
National University of Computer and Emerging Sciences
School of Computing Fall 2018 Islamabad Campus
b) (2 marks) Write an input validation loop that asks the user to enter a number in the range
of 10 through 25.

a) (2 marks) Convert following code into if-elseif-else

char sport;
cin >> sport;
switch (sport)
{
case 'c':
cout << "You like Cricket";
case 'f':
cout << "You like Football";
break;
case 't':
case 'H':
cout << "You like Tennis";
cout << "You like Hockey";
case 'B':
cout << "You like BasketBall";
break;
}

Page 6 of 12
National University of Computer and Emerging Sciences
School of Computing Fall 2018 Islamabad Campus
b) (2 marks) Write a C++ program that find maximum value and the minimum value attained
by f(x) =𝑥 2 + 3𝑥 + 2 the interval [-9, 8].

c) (2 marks) Rewrite the following code in for loop?


int i = 3;
do {
cout << "Hello World";
i++;
cout << i;
} while (i < 5 && i >= 2);

d) (2 marks) Point out the logical error in the following program and correct it.

int main()
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; i <= 10; j++)
{
cout<<”*”;
}
cout<<”\n”;
}
}

Page 7 of 12
National University of Computer and Emerging Sciences
School of Computing Fall 2018 Islamabad Campus
Question III …………………………………………………………………………… (10 Marks)
Write a C++ program to check whether a triangle is a right angle triangle or not.

Note: In any right triangle, sum of square of two sides is always equal to the square of third side.

Page 8 of 12
National University of Computer and Emerging Sciences
School of Computing Fall 2018 Islamabad Campus
Question IV …………………………………………………………………………… (10 Marks)
The value of a function F is defined by following infinite series:

1 𝑥2 𝑥4 𝑥6 𝑥8
F= − + − + … … ..
3 9 27 81 243
Write a C++ code that take input N (number of terms) and x as arguments and calculates the
function’s value. In other words, calculate the sum of the series for first N terms. For example if
N=5 and x=2 then it should display 0.744856.

Page 9 of 12
National University of Computer and Emerging Sciences
School of Computing Fall 2018 Islamabad Campus
Question V.…………………………………………………………………………… (10 Marks)
Write a program to print pyramid using numbers

For lines = 4 For lines = 5

Page 10 of 12
National University of Computer and Emerging Sciences
School of Computing Fall 2018 Islamabad Campus
Question VI …………………………………………………………………………… (10 Marks)
Write a C++ program that takes input of an amount from user and print the minimum number of
notes (Rs. 500, 100, 50, 20, 10, and 5) required for the amount.

Input:
Input amount: 575

Output:

Total number of notes:


500: 1
100: 0
50: 1
20: 1
10: 0
5: 1
2: 0
1: 0

Page 11 of 12
National University of Computer and Emerging Sciences
School of Computing Fall 2018 Islamabad Campus

Page 12 of 12

You might also like