A1 Oop

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

1

CL-1004 Object Oriented Programming

Assignment Number 1
CL-1004 Object Oriented Programming

Fall 2024

Instructions:

• Partially or fully copied assignments will be marked as zero.


• Submission will be Online with Screenshots of running code
• Late submissions are not allowed.
• Solve all the questions in the given order.
• Write the programs with clarity and add comments where necessary.
• Each question carries 10 marks.

Q1: Reverse the Array

You need to write a function that reverses the content of the array. It should receive the following
arguments

• array (array can be of any of the three types char/int/bool)


• length of array (an integer that shows number of items in the array)
• type of array (any of following three strings char / int / bool)

void* reverse_array(void* array, int length, char* type){


}

You need to use two pointer method, where one pointer points to start of the array while the other
pointer points to end of the array. And they move towards each other after each swap

Handle following edge cases

• if the array is NULL then throw runtime error “Cannot reverse a null array.”
• if the size of array is zero then throw runtime error “Cannot reverse an empty array.”
• if the type of array is other than char/int/bool then throw runtime error “Cannot reverse
array of any type other than char/int/bool”

Write a sample main code that uses

• Test all three exceptions cases


• Tests the function with int, char and bool array

Q2: Transpose of a matrix

You are given a square matrix of size N x N. Write a C++ function to transpose the matrix in place.
2

CL-1004 Object Oriented Programming

The transposition of a matrix involves swapping its rows and columns. For example, the transpose
of the matrix:

Copy code
1 2 3
4 5 6
7 8 9

is:

Copy code
1 4 7
2 5 8
3 6 9

Your task is to implement a function void transposeMatrix(int **matrix, int N) where


matrix is a double pointer to the matrix, and N is the size of the matrix. The function should
transpose the matrix in place, modifying the original matrix.

Here's the function signature for clarity:

void transposeMatrix(int **matrix, int N);

Constraints:

• 1 <= N <= 1000


• Elements of the matrix are integers in the range [-1000, 1000]
• In the main, make use of dynamic memory allocation to input different sizes of matrix
• for main, Create separate function for memory allocation, getting user input, print
transpose, and release memory at the end

Your implementation should correctly transpose the given matrix without using any additional
memory (i.e., no extra matrix). Ensure that your solution works efficiently for large matrices.

Q3: Capitalize sentence

You are given a sentence as a null-terminated C-style string (char array). Write a C++ function to
capitalize the first letter of each word in the sentence.

For example, given the string "hello world", the function should modify it to become "Hello
World".

Your task is to implement a function void capitalizeSentence(char **sentence) where


sentence is a double pointer to the input string. The function should capitalize the first letter of
each word in the sentence in place, modifying the original string.

Here's the function signature for clarity:


3

CL-1004 Object Oriented Programming


void capitalizeSentence(char **sentence);

Constraints:

• The input sentence is null terminated.


• Words in the sentence are separated by spaces.
• The sentence may contain any printable ASCII characters.
• You are not allowed to use any additional memory (i.e., no extra arrays).

Your implementation should correctly capitalize the first letter of each word in the given sentence
without using any additional memory. Ensure that your solution works efficiently for sentences of
varying lengths and word counts.

Q4: Music playlist manager

Write a C++ program to develop a system to manage music playlists where each playlist can
contain a different number of songs, each song is stored dynamically.
Allow users to create, modify, and delete playlists and songs dynamically.

Q5: Number Conversion

Write a C++ program that converts a given decimal number into


binary, octal, and hexadecimal representations using 1D pointers for character storage.
Ensure proper memory allocation and deallocation.

Sample input
Enter a decimal number: 63
Sample output
Binary: 111111
Octal: 77
Hexadecimal: 3F

Q6: Matrix Operations

Create a C++ program that performs matrix addition, subtraction and multiplication
using 2D pointers. Users should input two matrices, and the program should dynamically
allocate memory to store the matrices, perform the chosen operation, and display the result.
Sample Input:
4

CL-1004 Object Oriented Programming

Enter the number of rows and columns for Matrix A: 2 2


Enter elements for Matrix A:

12
34
Enter the number of rows and columns for Matrix B: 2 2
Enter elements for Matrix B:

56
78
Enter 1 for addition or 2 for multiplication: 1
Sample Output
Matrix Addition Result:
68
10 12

Q7: String Concatenation

Build a C++ program that dynamically allocates memory


for two strings and concatenates them into a new string using DMA. The program should
allow users to input the two strings, and it should handle memory allocation efficiently.

Q8: Momentum

Momentum is defined as the product of an item’s mass and its velocity. Mass is a scalar value,
whereas velocity is generally expressed as a vector quantity with three components. The product
of the scalar mass and the velocity yields momentum as a vector quantity.

Write a function named momentum that will accept as arguments a


(i) one-dimensional velocity array with three values (type double) (i.e. a 3d vector)
(ii) mass (type double)

and return a dynamically allocated array representing the momentum. Note the momentum is
determined by multiplying the scalar mass by each element of the vector array.
5

CL-1004 Object Oriented Programming

Test your momentum function by constructing a short main program that will ask the user to input
values for the velocity and mass from the console, and then display the momentum.

Q9: Average Momentum

Write a program to determine the average momentum of a collection of items with random
velocities and masses. Do this using the following outline:
1. Construct a function named randVec that will take no arguments and return a dynamically
allocated 3-element array of doubles. Each element in the array should be a randomly generated
value in the range -100.0 through +100.0.
2. Using randVec and your momentum function from the previous part, generate momentum
vectors for 100 items, each of which has a random velocity (as described above) and a randomly
generated mass in the range 1.0 through 10.0. Save the momentum vectors using a suitable array
of pointers.
3. Determine and display the average momentum vector of the items using a for loop.
[Hint: the average should be done component by component.]

Q10: Letter Frequency

Write a function that will take a string and return a count of each letter in the string. For example,
"my dog ate my homework" contains 3 m's, 3 o's, 2 e's, 2 y's and one each of d, g, a, t, h, w, r and
k.
Your function should take a single string argument and return a dynamically allocated array of 26
integers representing the count of each of the letters a ….. z respectively. Your function should be
case insensitive, i.e., count 'A' and 'a' as the occurrence of the letter a. [Hint: use the letter to integer
conversion functions.] Do not count non-letter characters (i.e., spaces, punctuation, digits, etc.)
Write a main function that will solicit a string from the user using getline, call your letter frequency
function and print out the frequency of each letter in the string. Do not list letters that do not occur
at least once.
Example:
Enter a string: my dog ate my homework
Letter frequency
a1
d1
e2
6

CL-1004 Object Oriented Programming

g1
h1
k1
m3
o3
r1
t1
w1
y2

You might also like