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

Computer programming assignment

Uploaded by

binoj.lakshan12
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)
22 views

Computer programming assignment

Uploaded by

binoj.lakshan12
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/ 4

UNIVERSITY OF VOCATIONAL TECHNOLOGY

Faculty of Information Technology


Bachelor of Technology in Software/ Network/ Multimedia and Web -
2023 / 2024 (B1 & B2)
Assignment

IT104021 - Computer Programming

Question 1

a) State the following statements either true or false.


i. Comments are executed by the C++ compiler.
ii. A variable name can begin with a number in C++.
iii. Global variables in C++ can be accessed by any function in the program.
iv. Literals represent values that can be modified in C++.
v. The scope of a local variable is limited to the function or block it is declared in.
vi. The const keyword can be used to modify variables in C++.
vii. The arithmetic operators in C++ include addition, subtraction, multiplication and
division.
viii. The assignment operators assign values from the right-side operand to the left side
operand.
ix. Comparison operators return Boolean true or false based on comparing the
operands.
x. Right shift (>>) moves bits to the right, filling with 1s on the left.

b) What do C++ compiler and linker do?


c) Name the C++ compiler you used during the practical sessions.
d) Write down the advantages of writing comments in computer programs.

Page 1 of 4
Question 2

a) Consider the following code (Figure Q2.1) and explain each component indicated using
numbers.

Figure Q2.1

b) What does the sizeof() function do in C++?


c) What is the meaning of the term Variable initialization?
d) What should you consider when naming variables, functions and classes in C++?
Question 3

a) If
int x = 20; int y = 30; int z = 10;
Write down the output of the following C++ programs.
i. cout << (z = = y) && (x = = 20);
ii. cout << (x < = y) || (y = = z);
iii. cout << (x != y);
iv. cout << !(x > 3);
v. cout<< (x & y);
vi. cout<< (x | y);
vii. cout<< (~z);
viii. cout<< (x += 2);
ix. cout<< (x << 2);
x. cout<< (y >> 2);

Page 2 of 4
Question 4

a) Write a program to check if a number entered by the user is odd or even.


b) Write a program to find the sum of all odd numbers between 1 to n, where n is entered by
the user. Use a for loop in your program.
c) Write a program to find the average of all elements in the following integer array.
int myArray[5] = {20, 10, 50, 40, 30};
d) Write a program to check if the following array contains a given number x. Print true or
false.
int myArray[5] = {20, 10, 50, 40, 30};

Question 5

a) Write a function named "add" to take two integer values as arguments and return the
summation.
b) Include the "add" function that you have previously created in a class named "Class2".

c) Call the "add" method of "Class2" from the main method and print the return value
(summation).

Question 6

a) Write a C++ program that takes an integer as input and uses conditional statements to
determine if it's even or odd.
b) Implement a C++ function to find the largest element in an integer array using loops and
conditional statements.
c) Create a C++ function to find the smallest element in an integer array using loops and
conditional statements.
d) Define a C++ function that reverses the elements of an integer array.
e) Write a C++ function to count the occurrences of a specific element in an integer array.
f) Implement a C++ function that filters an integer array to remove duplicates and returns a
new array without duplicates.
g) Write a C++ function that takes two integer arrays as input and returns a new array
containing the elements that are common to both arrays.
h) Create a C++ function that takes two integer arrays as input and returns a new array
containing all unique elements from both arrays.

Page 3 of 4
i) Write a C++ function to search for a specific element in a 2D integer array and return its
location (row and column).
j) Write a C++ program to calculate the Diameter, Circumference and Area of a circle, if
the radius is given as an input. (Consider PI= 3.145). Create three functions to calculate
Diameter, Circumference and Area.

Assignment Submission:

Write answers in Microsoft Word file and rename the file with your
registration number then submit to the Google Classroom.

Page 4 of 4

You might also like