You are on page 1of 3

Adama Science and Technology University

School of Electrical Engineering and Computing


Department of Computer Science and Engineering
Worksheet 4
Looking Back
1. Identify the items in the right o the simple code on the left.
/* This program converts distance in kilometer to
decameter, meters, decimeter.
*/ Block comment:
#include <iostream> //allows input & output ____________________________
using namespace std; Line comment:
#include <stdlib.h> ____________________________
#define DECAFACTOR 100 //defined constant Preprocessor directives
const float METERFACTOR=10;//memory constant ____________________________
int main() Identifiers:
{ ____________________________
float kilo, deca, meter, deci; Keywords:
system(“cls”); ____________________________
cout << “Enter distance in kilometer” << ‘\n’; Data Types
cin >> kilo; ____________________________
deca= DECAFACTOR *kilo; Literal constants
meter=METERFACTOR*deca; ____________________________
deci=meter/10.0; Defined constants
system(“cls”); ____________________________
cout << “kilometer: “ << ‘\t’ << kilo <’\n’; Memory constants
cout << “Decameter: \t” << deca << ’\n’; ____________________________
cout << “Meter: \t” << meter << “\n”; Variables
cout << “Decimeter: \t” << deci ; ____________________________
system(“pause”); Escape sequence
return 0; ____________________________
} Library functions
____________________________

2. Write the output displayed in each of the following cases.


int x = 2, y= 3, z= 1;char ch=96;
a. cout <<( x + x/6 + y);
b. cout << (3*y + x);
c. cout <<(4 + z - (x + z) % 2)
d. cout <<(x - 2 * (3 + z) + y);
e. cout<<(z=y=x+5) << “ ” << z *x %y
f. cout <<x << y<< x++ + --y;
g. cout <<++ch;
h. int var=10;
cout << “var = “ << var<<endl;
cout << “var = “ << ++var<<endl;;
cout << “var = “ << var++<<endl;
i. y=x<y ||z++;
cout<<y<<”: “<<z<endl;

1|Page
Adama Science and Technology University
School of Electrical Engineering and Computing
Department of Computer Science and Engineering

3. What value is assigned to the variable on the left side of the assignment statement?
short a=3, b=4 , c= 8, x=2;
char ch1= ‘A’, ch2=’a’, ch;
float f=1/3.0, y;
a. x += b-- * ++a;
b. x /= c % -3;
c. ch += 3;
d. x=(++a < b);
e. ch= ch1 + 32;
f. x = (f == 1 / 3.0)
g. x = b / a;
h. x=!( (a > b || c < ch1) && ch2 – 48 > 0);
i. x = (ch1 < ch2) && (a - b) || !(a + b < c)
j. b < c ? a < b ? c * a - 2 : c / a+ 1 : c - a

I. Simple programming
4. Write a program that converts an angle in degree to radian. Provide good user prompt.
5. Write a program that prompts a user to enter the number of second elapsed for an event and
converts this to m hours, n minute and p seconds. For example, if the user enters 7840, the
program displays 2:10:40.
6. Write a program that accepts three number and display the largest number divided with the
smaller number;
7. Write a program that prompts a user for an integer value in the range 0 to 32767 and then
prints the individual digits of the number on a line with four spaces between the digits. The
first line is to start with the left most digit and prints all five digits; the second line is to start
with the second digit from the left and print four digits, and so forth. For example, if the user
enters 1234, the output looks like
0 1 2 3 4
1 2 3 4
2 3 4
3 4
4
8. Write a program to create a customer’s bill for a company. The company sells only five
different products: TV, VCR, Remote Controller, CD player and Tape Recorder. The unit
prices are $400.00, $220.00, $300.00, and $150.00 respectively. The tax rate is 8.25 % of the
sale. The program must prompt and read the quantity sold for each product and produce an
output shown below (you may need to use manipulator functions, IOS class).
QTY DESCRIPTION UNIT TOTAL
PRICE PRICE
___ _____________ _______ _________
XX TV 400.00 XXXX.XX
XX VCR 220.00 XXXX.XX
XX REMOTE CTRLR 35.20 XXXX.XX
XX CD PLAYER 300.00 XXXX.XX
XX TAPE RECORDER 150.00 XXXX.XX

2|Page
Adama Science and Technology University
School of Electrical Engineering and Computing
Department of Computer Science and Engineering
__________
SUBTOTAL XXXX.XX
TAX XXX.XX
TOTAL XXXX.XX

3|Page

You might also like