programming workbook arrays
programming workbook arrays
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
Page 1 of 12
National University of Computer and Emerging Sciences
School of Computing Fall 2018 Islamabad Campus
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(){
return 0;
}
Page 2 of 12
National University of Computer and Emerging Sciences
School of Computing Fall 2018 Islamabad Campus
int x, y;
x = 5;
y = ++x * ++x;
cout << x << y;
x = 5;
y = x++ * ++x;
cout << x << y;
return 0;
}
int z=1;
for(int i = 5 ;i >= 1; --i)
{
z=i ;
for(int z = 1; z <= i; ++z)
{
cout << z << " ";
}
++z;
int main(){
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:
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.
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].
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
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:
Page 11 of 12
National University of Computer and Emerging Sciences
School of Computing Fall 2018 Islamabad Campus
Page 12 of 12