Midterm Programming Fundamentals Fall 2020
National University of Computer and Emerging Sciences, Lahore Campus
Registration No.______________________
Instructions:
Attempt all questions
Calculators are not allowed
You might use extra sheets for working but please try to write the final answer in the space
provided for it
General Hints:
1. Comparison of double/float type variables is exactly similar to int type variables
2. For arithmetic operators if one of the variable/value type is double/float and one is int then the answer
is floating point value.
SHORT QUESTIONS
1. The following program segment has been written to print a simple message in case the input number is
a positive integer and contains exactly 5-digits (not less than 5 or more than 5digits). What condition
should be written within the parenthesis so that the program segment works correctly? Please remember
that you are not allowed to use any other variables or add any new instruction in the program segment.
Just write the condition using the already declared
variable(s). [3 Points]
int num = 0; cin >>
num;
if(_______________________________________________) cout<< " It is a five
digit positive number";
else cout<< " It is not a five digit positive number ";
1/10
Midterm Programming Fundamentals Fall 2020
WRITE YOUR ANSWER HERE
2. For each of the following program segments, specify the output it will produce when executed.
[8 = (1 + 1 + 1 + 1 + 1 + 1 + 1 + 1) Points]
Program Segment Output
int z = 0; z = 27 / 4;
cout << "z = " << endl<<z;
int num = 4; if ( num % 2 ==
0) cout << num / 2; else
cout << num * 4;
int RollNo, rollno; RollNo = 5;
rollno = 8;
cout << "Roll No is " << rollno;
int delta = 0, x =3; if( x % 2 == 1)
delta = delta + x; x = x + 1; if( x
% 2 == 0) delta = delta + x; x =
x + 1; if( x % 2 == 0) delta =
delta + x; cout << delta;
2/10
Midterm Programming Fundamentals Fall 2020
int num = 56;
if( num % 7 ){
cout<<"Number is divisible by 7\n";
} else{
cout<<"Number is not divisible by 7\n";
}
Program Segment Output
int i, j, k;
i = 5; j = 3; k = 7;
if (i < j) { if (j < k)
i = j; else
j = k;
} else
{
if (j > k) j=
i; else i = k;
}
cout << "i = " << i << " j = " << j << " k = " << k << endl;
int a = 30, b = 20, c = 10, d = 8;
cout << ( (a >=b) == ( d > c) ) << (c < a) << ( b +
c == a);
int a = 30, b = 20, c = 10, d = 8;
cout << (a + b / d – c) * (a % b) – ( d / c);
3/10
Midterm Programming Fundamentals Fall 2020
3. Following program has been written to compute and display average of three numbers but it is not
producing correct output. For example when we input numbers 1, 2 and 3 the program display 4
whereas the actual average is 2.
Suggest a simple change in a single line of the program that will correct this logical error.
You must also rewrite the corrected line (only) in the space provided below [3 Points]
#include<iostream> using
namespace std; int main(){
double num1, num2, num3, avg;
cin >> num1; cin >>
num2; cin >> num3;
avg = num1 + num2 + num3 /3;
cout << "The average of three numbers is "<< avg ; return 0;
}
YOUR ANSWER GOES HERE
4. Following program has some syntax errors. Identify all the syntax errors and write each line correctly
so that the program compiles without errors [6 Points]
PROGRAM LINES WITH ERROERS PROGRAM WITHOUT ERROR
#include<iostream>
Using namespace std;
int main()
{
integer TC , FH;
cout << ‘Temperature IN’;
cin << T;
F << 9\5.0* T + 32
COUT >> “Temperature OUT” << F
<Endl
4/10
Midterm Programming Fundamentals Fall 2020
Return 0;
}
WRITING C++ PROGRAMS
Problem No 1: [Currency Conversion] [7 Points]
International travelers often need to convert some amount of money from currency into
another. For example, a Pakistani traveler to Malaysia got his amount converted into US
Dollars and Ringgit Malaysia. In Malaysia he needed more Ringgits so he got some of his US
Dollars converted into Ringgits. The calculations for conversions are really simple but
travelers are not typically good at math.
In this problem you are required to write a simple program that will take two inputs i) the
amount to be converted ii) the rate of conversion, and then display the amount that the traveler
must receive after conversion. The formula for conversion is
Target Amount = Amount multiplied by the Exchange Rate
Write a program in the space provided below where the marks will be awarded for
• Correct indentation. [1 Points]
• Using number of correct type of variables. [1 Points]
• Correct inputs and input validation (amount and rate > 0) [2 Point]
• Correct calculations [2 Points]
• Correctly showing the answers and messages. [1 Points]
Write your program in the space provided on the next page
5/10
Midterm Programming Fundamentals Fall 2020
BLANK PAGE FOR WRITING PROGRAM
6/10
Midterm Programming Fundamentals Fall 2020
Problem No 2: [BODY MASS INDEX CALCULATOR] [13 Points]
The BMI (Body Mass Index) is calculated using height (measured in meters) and weight
(measured in Kg) of a person. A person with BMI of 25.0 or more is considered overweight, while
the healthy/normal range is 18.5 to 24.9 and a person with BMI less than 18.5 is considered
underweight. The formula for computing BMI is
BMI = weight/ height2
Where the weight is in kilograms and height is in meters.
Write a program that can be used to compute the BMI of a person. Your program must input
weight in Kilograms and Height in centimeters. Your program must convert the height into
meters (1 Meter = 100 centimeters) and then use it to compute the BMI of the person using
formula described above. The program must display a message indicating if the person is
underweight, overweight or has normal weight. Marks will be awarded for
Correct indentation. [2 Points]
Using number of and correct type of variables. [2 Points]
Correct conversions and calculations [4 Points]
Correctly showing the answers and messages. [5 Points]
Write your program in the space provided on the next page
7/10
Midterm Programming Fundamentals Fall 2020
BLANK PAGE FOR WRITING PROGRAM
8/10
Midterm Programming Fundamentals Fall 2020
BLANK PAGE FOR WRITING PROGRAM
9/10
Midterm Programming Fundamentals Fall 2020
10/10