You are on page 1of 2

Programming Lab 6 Example first mid-term exam questions

Try to solve the following selected example exam questions before the lab.
Then during the lab use the compiler to check your solutions and/or complete unsolved
questions.
Remember that the following reference sheet will be provided in the exam:
EEE241ReferenceMT1.pdf (you can download it from the course website).

Question
Write down the output of the program given below.
#include <iostream>
using namespace std;
int main() {
double x=6.8;
while(true) {
x = x / 2;
cout << x << endl;
if ( x<1.0 ) break;
x = x + 0.1;
}
}
Notes:
Follow carefully the evolution of the values of the variables in the program. The questions asks for the
output only, so the relevant expression is cout << x << endl;. Clearly indicate the result of this
statement. Note that the statement is executed more than once in the while loop.

Question
Write down the output of the
program given right.

#include <iostream>
#include <cmath>
#include <string>
using namespace std;

Hint: take care when assigning


results to operations involving
integer division and to
type integer variables.

int main()
{
double a=6.7, b=1.4, c=9.0, r, s, t;
int
i = 7, j = 2010, k = 5, L, M;
string s1 = "ter", s2 = "millime", s3;
r = a * b / k;
s = j / i * c;
t = sqrt(9*c);
Notes:
L = int(c+k);
Try this by hand, then copy/paste the
M = L % k;
program into your compiler and
s3 = s2 + s1;
compare the output with
cout << s3 << '\n';
your answer.
cout << r << '\t' << s
<< '\t' << t << endl;
cout << L << '\t' << M << endl;
return 0;
}

More >

Programming Lab 6 Example first mid-term exam questions


Continued.

Question
Implement the following flowchart in a C++
program:
start

Question
Implement the following flowchart in a C++
program:
Start

Input x
Input n
z=2

y = x/z

x<2

f=1
k=1
y = |x-z|

F
Output n, f

Output x, y

T
End

end

k <= n

f=f*k
k=k+1

Question
Write a C++ program that inputs 20 floating-point values from the keyboard and outputs:
a) The number of values that are negative
b) The number of values that are zero
c) The number of values that are positive.
Notes:
For example, the output might be 7,3,10 meaning that there are 7 negative values, 3 values that are
zero, and 10 values that are positive.

Question
Write a program that evaluates and outputs the result of the first 50 terms of the following
infinite series sum:


2 3 4
Use a while statement.
Notes:
Read the question carefully. First this is a series sum, so the output is a single value.
You need to create a loop that sums 50 terms of the series 1/1 + 1/2 + 1/3 +1/4 and so on until 1/50,
then multiply by pi.

More example first mid-term exam questions can be found on the course website.

You might also like