0% found this document useful (0 votes)
14 views9 pages

2020 Midterm Solution.

NUCES Midterm Solution

Uploaded by

Gaming 4 All
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)
14 views9 pages

2020 Midterm Solution.

NUCES Midterm Solution

Uploaded by

Gaming 4 All
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/ 9

Midterm Programming Fundamentals Fall 2020

National University of Computer and Emerging Sciences, Lahore Campus


Course Name: Programming Fundamentals Course Code: CS 118
Program: BS(CS) Semester: Fall 2020
Duration: 90 Minutes Total Points: 40
Paper Date: 19/10/2020 Page(s): 9
Exam Type: Midterm-I Section: ALL

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 5-
digits). 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 ";

WRITE YOUR ANSWER HERE

ONE POSSIBLE WAY OF WRITING THE CONDITION IS

9999 < num && num < 100000

1/10
Midterm Programming Fundamentals Fall 2020

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=
z = 27 / 4; 6
cout << "z = " << endl<<z;

int num = 4;
if ( num % 2 == 0 )
cout << num / 2; 2
else
cout << num * 4;

int RollNo, rollno;


RollNo = 5; Roll No is 8
rollno = 8;
cout << "Roll No is " << rollno;

int delta = 0, x =3;


if( x % 2 == 1)
delta = delta + x;
x = x + 1; 7
if( x % 2 == 0)
delta = delta + x;
x = x + 1;
if( x % 2 == 0)
delta = delta + x;
cout << delta;

int num = 56;

if( num % 7 ){ Number is not


cout<<"Number is divisible by 7\n"; divisible by 7
}
else{

cout<<"Number is not divisible by 7\n";


}

2/10
Midterm Programming Fundamentals Fall 2020

Program Segment Output

int i, j, k;

i = 5; j = 3; k = 7;

if (i < j) {
if (j < k)
i = j; i = 7 j = 3 k=7
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;


011
cout << ( (a >=b) == ( d > c) )

<< (c < a) << ( b + c == a);

int a = 30, b = 20, c = 10, d = 8;


220
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

avg = ( num1 + num2 + num3) /3;

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> #include<iostream>

Using namespace std; using namespace std;

int main() int main()


{ {
integer TC , FH; int TC, FH;

cout << ‘Temperature IN’; cout << "Temperature IN";

cin << T; cin >> TC;

F << 9\5.0* T + 32 FH = 9/5.0* TC + 32;

COUT >> “Temperature OUT” cout << "Temperature OUT"


<< F <Endl << F < endl;
Return 0; return 0;
} }
4/10
Midterm Programming Fundamentals Fall 2020

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

#include<iostream>

using namespace std;

int main()
{
double Amount, Rate, NewAmount;

cout<<"Enter Positive Amount ";


cin >>Amount;

cout<<"Enter Positive Rate ";


cin >>Rate;

if(Amount > 0 && Rate > 0)


{
NewAmount = Amount * Rate;
cout<<"Original Amount: "<<Amount<<endl
<<"Rate: "<<Rate<<endl
<<"After Conversion "<< NewAmount<<endl;
}
else
{
cout<<" Amount and Rate must be positive";
}
return 0;
}

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

#include<iostream>
using namespace std;
int main()
{
double Weight, Hight_Centimeters, Hight_Meters, BMI;
cout<<"Enter Weight in Kilograms ";
cin >> Weight;

cout<<"Enter Height in centimeters ";


cin >> Hight_Centimeters;

if(Weight > 0 && Hight_Centimeters > 0)


{
Hight_Meters = Hight_Centimeters/100;
BMI = Weight /(Hight_Meters * Hight_Meters);
cout<<"Person with Hight "
<< Hight_Centimeters<<" cm "
<<"Weight = "<< Weight<<" Kg "
<<"Has BMI = "<< BMI<<endl;
if(BMI < 18.5)
cout<<"Person is UNDERWEIGHT"<<endl;
else if(18.5 <= BMI && BMI < 24.9)
cout<<"Weight is NORMAL"<<endl;
else
cout<<"Person is OVERWEIGHT"<<endl;

}
else
{
cout<<"Invalid Height or Weight: Positive Values Only Please";
}
return 0;
}

8/10
Midterm Programming Fundamentals Fall 2020

BLANK PAGE FOR WRITING PROGRAM

9/10

You might also like