0% found this document useful (0 votes)
2 views

Work Sheet 2 - Basics Programming Constructors

This document is a worksheet from Addis Ababa Science and Technology University focusing on basic programming concepts and C++ language exercises. It includes questions on variable definitions, identifiers, expressions, and various programming tasks such as arithmetic operations, unit conversions, and logical expressions. The worksheet is designed for students in the Department of Software Engineering to practice and apply their programming skills.

Uploaded by

dagmawifeyissa7
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)
2 views

Work Sheet 2 - Basics Programming Constructors

This document is a worksheet from Addis Ababa Science and Technology University focusing on basic programming concepts and C++ language exercises. It includes questions on variable definitions, identifiers, expressions, and various programming tasks such as arithmetic operations, unit conversions, and logical expressions. The worksheet is designed for students in the Department of Software Engineering to practice and apply their programming skills.

Uploaded by

dagmawifeyissa7
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/ 5

Addis Ababa Science and Technology University

College of Electrical and Mechanical Engineering


Department of Software Engineering
----------------------------------------------------------------------------------------------------------------------

Work Sheet 2
Basics Programming Constructors
1. Which of the following represent valid variable definitions?

int n = -100; double d = 0.67F;

unsigned int i = -100; float f = 0.52L;

signed int = 2.9; signed char = -1786;

long m = 2, p = 4; char c = '$' + 2;

int 2k; sign char h = '\111';

double x = 2 * m; char *name = "Peter Pan";

float y = y * 2; unsigned char *num = "276811";

unsigned double z = 0.0;

2. Which of the following represent valid identifier

Identifier 2by2
seven_11 Default
_unique_ average_weight_of_a_large_pizza
gross-income variable
gross$income object.oriented

3. Define variables to represent the following entities:

▪ Age of a person.

▪ Income of an employee.

▪ Number of words

▪ A letter of the alphabet.

▪ A greeting message.

Fundamentals of Programming Page 1


Addis Ababa Science and Technology University
College of Electrical and Mechanical Engineering
Department of Software Engineering
----------------------------------------------------------------------------------------------------------------------

4. Write expressions for the following:


• To test if a number n is even.

• To test if a character c is a digit.

• To test if a character c is a letter.

• To do the test: n is odd positive or n is even negative.

• To set the n-th bit of a long integer f to 1

• To reset the n-th bit of a long integer f to 0

• To give the number of characters in a null-terminated string literal s

5. Write C++ statements to accomplish each of the following:

a) To demonstrate comments and variable declarations


b) To initialize variable as they are declared
c) Declare variables sum and x to be of type int
d) Declare first and then Initialize variable sum to 0
e) To test arithmetic, the increment and decrement operators
f) In one statement, assign the sum of the current value of x and y to z and increment the
value of x.
g) Decrement the variable x by 1, then subtract it from the variable total.
h) Calculate the remainder after q is divided by divisor and assign the result to q. Write
this statement two different ways.
i) Add variable x to variable sum and assign the result to variable sum.
j) Print " The sum is : " followed by the value of variable sum.

6. What will be the value of each of the following variables after its initialization:

double d = 2 * int(3.14);
long k = 3.14 - 3;

Fundamentals of Programming Page 2


Addis Ababa Science and Technology University
College of Electrical and Mechanical Engineering
Department of Software Engineering
----------------------------------------------------------------------------------------------------------------------

7. For each of the following problems write a C++ code to perform the required task.
a) Print sum, difference, product, and quotient of given integers respectively.
b) Find the average of two numbers given by the user
c) Perform date arithmetic, such as how many days there are between 6/1/90 and 8/3/92.
d) Transfer inches to centimeters (1 inch= 2.54 centimeters)
e) Calculate the power of a number (xx)
f) Reads a number n and prints 2n
g) Area and circumference of circle based on its radius entered from the keyboard.
h) Volume and the area of sphere of a radius r accepting its radius from user
i) Rectangle area and circumstance by reading height and width from user.
j) Volume and the area of a cylinder of radius r and height h

8. Compute the value of each legal C++ arithmetic expression. If the expression is not legal,
explain why. Clearly indicate what the data type of the result of each expression would be.

a) (1.0 / 4) * 6

b) (6 * 3) % 5

c) (3 / 4) * 4

d) (4.0 * 2) % 5

e) ((10 + 5 / 2) / 3)

9. Add extra brackets to the following expressions to explicitly show the order in which the
operators are evaluated:
a) (n <= p + q && n >= p - q || n == 0)
b) (++n * q-- / ++p - q)
c) (n | p & q ^ p << 2 + q)

d) (p < q ? n < p ? q * n - 2 : q / n + 1 : q - n)

Fundamentals of Programming Page 3


Addis Ababa Science and Technology University
College of Electrical and Mechanical Engineering
Department of Software Engineering
----------------------------------------------------------------------------------------------------------------------

10. (De Morgan’s Laws) De Morgan’s laws can sometimes make it more convenient for us to
express a logical expression. These laws state that the expression !(condition1 &&
condition2) is logically equivalent to the expression (!condition1 || !condition2). Also, the
expression !(condition1 || condition2) is logically equivalent to the expression (!condition1
&& !condition2). Use De Morgan’s laws to write equivalent expressions for each of the
following:

a) !( x < 5 ) && !( y >= 7 )


b) !( a == b ) || !( g != 5 )
c) !( ( x <= 8 ) && ( y > 4 ) )
d) !( ( i > 4 ) || ( j <= 6 ) )
11. Write a program to convert English units to metric (E.g. feet to centimeter, miles to
kilometers, gallon to liters, etc.) include a specification and code design.

12. Write a program that converts a temperature given in Fahrenheit into Celsius and vice versa.

13. Write a program that converts a letter entered from the keyboard to its uppercase or lower
case equivalent (Hint :- use the function defined in ctype.h library header file).

14. A serial transmission line can transmit 960 characters a second. Write a program that will
calculate how long it will take to send a file, given the file size. Try it on a 400MB
(419,430,400 byte) file. Use appropriate units. (A 400MB file takes days.)

15. Write a program that prompts the capacity in gallons of an automobile fuel tank and the miles
per gallons the automobiles can be driven. The program outputs the number miles the
automobile cam be driven without refueling.

16. Write a program that swaps the values of two variables and displays their former (previous)
and current values. The values of the variables are to be entered from the keyboard.

a) using a third variable and

b) Without using third variable.

17. Make a program that calculates and display the Body Mass Index (BMI) of a person. The
program should read the height and weight of a person being given.

18. Write a c ++ program that prompts the user to enter the elapsed time for an event in hours,
minutes and seconds. The program then outputs the elapsed time in seconds.

Fundamentals of Programming Page 4


Addis Ababa Science and Technology University
College of Electrical and Mechanical Engineering
Department of Software Engineering
----------------------------------------------------------------------------------------------------------------------

19. Write a program that accepts day, month and year in number (DD-MM-YY) and displays for
the user as (MM- DD-YY). For example it accepts 12 09 2017 displays September 12, 2017.

20. John has grown mango tree. He has 80 mango trees over a given area. Currently he gets 900
fruits per tree. If he plants additional mango tree in the same area. The fruit decreased by 4 in
the whole tree. Write a program which shows the number of mango tree increment and their
fruit decrement.

21. Write a C++ program that resolve a force into its X and Y component with a given angle.

22. Write a program to calculate the distance between two points (x1, y1) and (x2, y2). All
numbers and return values should be of type double. Incorporate this method into an
application that enables the user to enter the coordinates of the points.

23. Add two numbers without a ‘+’ operator.

24. Develop C++ program that used to calculate percentile mark of student score in 8th grade.

25. Write a C++ program to accept a function and find integration or derivation of the function.

Fundamentals of Programming Page 5

You might also like