Sunteți pe pagina 1din 8

Work-sheet1 for introduction in computer programming using C++

Chapter one; Fundamentals of computers


1. What are the six main components of a computer and their main function?
2. How do the main components of the computer communicate with each other?
a. System bus
b. Memory
c. Keyboard
d. Monitor
3. What are the major differences between main memory and external memory? And
what is stored in main memory and in external memory?
4. The Operating System is responsible for
a. (a) Controlling peripheral devices such as monitor, printers, disk drives
b. (b) Detecting errors in users' programs
c. (c) Make the coffee
d. (d) Provide an interface that allows users to choose programs to run and to
manipulate files
e. (e) Manage users' files on disk
5. Convert the following numbers:
a. 198010 to binary, hexadecimal and octal
b. 10010011012 to decimal, hexadecimal and octal
c. 768 to binary, hexadecimal and decimal
d. 43F16 to binary, decimal and octal
6. Perform the following arithmetic operations;
a) (53)8 (21)8 in base 8
b) (-49)10 + (22)10 in base 2 (binary)
c) (7A1) 16 (10C)16 in base 16
d) (77)8 + (62)8 in base 8
e) -(13)8 - (15)8 in base 8 and base 2
f) (23)10 / (56)10 in binary
g) (0)16 (FF)16 in base 16
7. Fill the blank spaces of the following table by calculating the equivalent base
conversion
Decimal
764.4375

Binary

Octal

HexaDecimal

111001.011010
35.25
5A.F5B

Compiled by Mulugeta H

Page 1

Work-sheet1 for introduction in computer programming using C++


Chapter two; fundamentals of C++ programming
1. What is the need for programming?
2. What are the three main types of computer programming languages?
a. Machine language, assembly language, high level language
b. Imperative language, functional language, declarative language
c. COBOL, Fortran-77, C++
3. What is the difference between a machine-language program and a high level language
program?
4. From the point of view of the programmer what are the major advantages of using a highlevel language rather than internal machine code or assembler language?
(a) Program portability
(b) Easy development
(c) Efficiency
5. What is the only language that a computer understands directly?
a. English, as spoken in Boston, Mass.
b. BASIC, the Beginners' All-purpose Symbolic Instruction Code
c. Machine language, different for every type of CPU
d. Like c and c++ languages
6. What are the roles of a compiler, assembler and interpreter and their main difference?
7. What is a source program? What is an object program?
8. What are the properties of well-designed programs?
9. What are the steps involved in going from the specification of a problem to producing an
executable program which will solve the problem?
10. What is the need of writing an algorithm before writing programming code?
11. What are advantages and drawbacks of using flow charts in developing programming
languages?
12. Write an algorithm to compute the sum of the squares of integers from 1 to N where N is a
number input from key board and also draw the corresponding flowchart. Assuming that
you have to code the above given problem, show the steps involved in program design.
13. A program is required which will read in the breadth and height of a rectangle, check the
breadth with height to identify whether is rectangle or square and which will output the
area and the length of the perimeter of the rectangle. The output should be `the area and
perimeter of the square are . . . ' whereas if they are not equal then the output should be `the
area and perimeter of the rectangle are . . . '.Write an algorithm and develop the flow chart
for this problem.
14. What is the purpose of the following two lines?
a. #include <iostream>
b. Using namespace std;
15. Explain Syntax and Logical errors?
16. A compiler produces an error in compiling a program because a closing round bracket has
been missed out in the source code. What type of error is this?
a) Syntax Error
b) Logical Error
c) Linker Error
d) Run-time Error
Compiled by Mulugeta H

Page 2

Work-sheet1 for introduction in computer programming using C++


17. Suppose you write a program that is supposed to compute the interest on a bank account at
a bank that computes interest on a daily basis, and suppose you incorrectly write your
program so that it computes interest on an annual basis. What kind of program error is this?
18. When the program is running it produces a number that is too large to fit into the space
allocated for it in memory. What type of error is this?
19. If you omit a punctuation symbol (such as a semicolon) from a program, an error is
produced. What kind of error?
20. Correct the syntax errors in the following C++ program:
include iostream
using name space std
Main();
{
Float x,y,z;
cout < "Enter two numbers ";
cin >> a >> b
cout << 'The numbers in reverse order are'
<< b,a;
}

21. Specify the possible data types you have to use with suitable declarations for variables to
represent the following items? Be sure to choose meaningful identifiers.
a. the number of students in a class
b. the grade (a letter) attained by a student in the class
c. the average mark in a class
d. the distance between two points
e. the population of a city
f. for a program to add two numbers?
g. the weight of a postage stamp
h. the registration letter of a car
22. Specify how many bytes are occupied by the following data types in a 32-bit system:
a. Type int
b. Type long double
c. Type float
d. Type long
23. Write a constant declaration that declares constants to hold the number of days in a week
and the number of weeks in a year. In a separate constant statement declare a constant pi as
3.1415927
24. If a is 5, b is 10, c is 15 and d is 0 what are the truth values of the following expressions?
(a) c == a+b || c == d
(b) a != 7 && c >= 6 || a+c <= 20
(c) !(b <= 12) && a % 2 == 0
(d) !(a >5) || c < a+b

Compiled by Mulugeta H

Page 3

Work-sheet1 for introduction in computer programming using C++


25. Given the declarations:
float x;
int k, i = 5, j = 2;
To what would the variables x and k be set as a result of the assignments
k = i/j;
x = i/j;
k = i%j;
x = 5.0/j;
26. What values does C++ use to represent true and false?
27. Consider the following section of C++ program, in which i and n are int variables
n = 7;
i = 4;
i = n++;
What are the values of i and n?
(a) i=7 n=8
(b) i=7 n=7
(c) i=8 n=8
(d) i=4 n=7
28. True or false: A variable of type char can hold the value 301.
29. Consider the following section of C++ program, in which i and n are
int variables
n = 5;
i = 9;
i = --n;
What are the values of i and n?
(a) i=9 n=5
(b) i=4 n=4
(c) i=4 n=5
(d) i=5 n=4
30. Write a logical expression which returns true if a float variable x lies between -10.0 and
10.0.

Compiled by Mulugeta H

Page 4

Work-sheet1 for introduction in computer programming using C++

Chapter three; control statements and loops


1. Percentage marks attained by a student in three exams are to be entered to a computer. An
indication of Pass or Fail is given out after the three marks are entered. The criteria for
passing are as follows: A student passes if all three examinations are passed. Additionally a
student may pass if only one subject is failed and the overall average is greater than or
equal to 50. The pass mark for an individual subject is 40. Write a C++ program to
implement this task.
2. Write a program that allows the user to enter a time in seconds and then outputs how far an
object would drop if it is in free fall for that length of time. Assume that the object starts at
rest, there is no friction or resistance from air, and there is a constant acceleration of 32 feet
per second due to gravity. Use the equation:
NB: You should first compute the product and then divide the result by 2
3. Write an algorithm to produce an n times multiplication table (n less than or equal to 10).
For example, if n is equal to four the table should appear as follows:
1
2
3
4
1 1
2
3
4
2 2
4
6
8
3 3
6
9
12
4 4
8
12
16
Convert your algorithm into a program. Use nested for loops and pay attention to
formatting your output so that it is displayed neatly.
4. Write a C++ program to get variable x from keyboard repeatedly and if x is positive then a
variable poscount is incremented and if x is negative a variable negcount is incremented to
count the number of negative and positive numbers separately and also their sum and
display the information to your screen
5. Write a nested if-else statement that will assign a character grade to a percentage mark as
follows; 70 or over A, 60-69 B, 50-59 C, 40-49 D, 30-39 E, less than 30 F. and for invalid
input requesting the user to input valid mark(invalid marks negative numbers, characters
etc)
6. Write a C++ program which when two integers x and y are input will output the absolute
difference of the two integers. That is whichever one of (x-y) or (y-x) is positive. Think of all
the cases that can arise and consequently plan a set of test data to confirm the correctness
of your program.

Compiled by Mulugeta H

Page 5

Work-sheet1 for introduction in computer programming using C++


7. Use for loops to construct a program that displays a pyramid of Xs on the screen. The
pyramid should look like this

Except that it should be 20 lines high, instead of the 5 lines shown here. One way to
do this is to nest two inner loops, one to print spaces and one to print Xs, inside an
outer loop that steps down the screen from line to line.
8. A series of positive numbers are to be entered from the keyboard with the end of the series
indicated by a negative number. The computer should output the sum of the positive
numbers. Write an algorithm for this task. Use a while type of loop with the condition
`number just entered is positive'. Think carefully about what initializations are required
before entering the while loop. Do a desk check of your algorithm with a small data set; say
3 positive numbers then a negative number. What would your algorithm do if the user
entered a negative number first? Is what your algorithm would do in this circumstance
sensible?
9. Using functions write a C++ programming code in your text editor to solve the problems
related the following activities;
a) Ask the user to enter one natural number N then your program should display the
factorial of the entered number in the form N!=N*(N-1)*(N-2)**3*2*1.For
example if the users input number is 5 then the output should be
5!=5*4*3*2*1=120
NB: ask your user to try for other number (if hi/she press some specific key) before
exiting the screen otherwise exit the program.
b) Ask request to the user that how many numbers he/she wants to enter (N). Once
the user specify the size (N numbers) input these numbers from keyboard sort
them both in ascending and descending order. Example for 5 numbers entered from
keyboard (0 7 -2 12 -11) the output should be
The numbers are before sorting are: 0 7 -2 12 -11
The numbers sorting in ascending order: -11 -2 0 7 12
The numbers sorting in descending order: 12 7 0 -2 -11
c) Ask request to the user that how many numbers he/she wants to enter (N). Once
the user specify the size (N numbers) input these numbers from keyboard find the
smallest and largest numbers and their occurrences and display the information to
the screen. Example for 10 input numbers(let 1 5 4 98 23 -2 3 45 -2 8) our out
should be ;
The 10 input numbers are: 1 5 4 98 23 -2 3 45 -2 8
The smallest number is -2 and it occurs 2 times
The largest number is 98 and it occurs 1 times

Compiled by Mulugeta H

Page 6

Work-sheet1 for introduction in computer programming using C++


d) Ask request to the user that how many numbers he/she wants to enter (N). Once
the user specify the size (N numbers) input these numbers from keyboard and
display the odd and even numbers and their quantity separately.
Example for 10 input numbers (let 1 5 4 98 23 -2 3 45 -2 8) our out should be;
The 10 input numbers are: 1 5 4 98 23 -2 3 45 -2 8
There are 5 even numbers and these are: 4 98 -2 -2 8
There are 5 odd numbers and these are: 1 5 23 3 45
e) Ask request to the user that how many numbers he/she wants to enter (N). Once
the user specify the size (N numbers) input these numbers from keyboard and find
the sum and the product (ignore if there is zero input) of all these numbers and
display the information to the screen.
Example for 10 input numbers (let 1 5 4 98 23 -2 3 45 -2 8) our out should be;
The 10 input numbers are: 1 5 4 98 23 -2 3 45 -2 8
The sum of all these 10 numbers is sum=183;
The product of all these M numbers (where M=10-number of zeros) ignoring
the zeros is product=;194,745,600
NB: ask your user to try for other number (if hi/she press some specific key) before
exiting the screen otherwise exit the program
f) Ask your to specify the length(N) of the multiplication table he/she want to have
and accepting the length as an input display N by N multiplication table: example
for N=18;

g) Ask your user to enter one number (N)and then the second number (P) for power
from keyboard then compute NP in the form NP = N*N*N*N P times=?
Example: for request first enter the number > 3
Now please enter the power >2
The result is: 32 =3*3= 9
NB: ask your user to try for other number (if hi/she press some specific key) before
exiting the screen otherwise exit the program

Compiled by Mulugeta H

Page 7

Work-sheet1 for introduction in computer programming using C++


h) Develop a program to display the fabonacii series ; fabonacii series is a series of
numbers generated by adding the next two numbers and put the result next;
1 1 2 3 5 8 13 21 34 55 89..
NB: ask your user to try for other number (if hi/she press some specific key) before
exiting the screen otherwise exit the program
i) Ask the user to enter numbers continuously if he/she press Y and update him/her
their newly average and how many numbers he/she entered previously while
he/she press any key=! Y exits the terminal (screen).
Example: enter the first number >2
You enter 1 number and the new average is updated average = 2
Do you want to enter another number if so press Y > Y
Enter the second number > 8
You entered 2 numbers and the new average is updated average = 5
Enter the third number >3
You enter 3 number and the new average is updated average = 4.3333
Do you want to enter another number if so press Y > Y
Enter the forth number > 4
You entered 4 numbers and the new average is updated average = 4.25
etc until the user press key!=Y

Compiled by Mulugeta H

Page 8

S-ar putea să vă placă și