You are on page 1of 6

AMORAH VENKATESHWARA COLLEGE

ASSIGNMENT UNIT- 1

COURSE: B. TECH

SUBJECT: DATA STRUCTURE AND ALGORITHMS (ICS 302)

BRANCH: ECE

SUBMITTED BY: ARTI KAK


SEMESTER: 3rd
STUDENT ID: SVU66850724
I. OBJECTIVE TYPE QUESTIONS

1. Raw material is called?


Answer: Data
2. Array is a collection of?
Answer: Homogeneous
3. Complexity is a measurement of?
Answer: No of iterations
4. Primitive data type contains?
Answer: Only one data
5. Linear data types are in?
Answer: Sequential manner

II. VERY SHORT TYPE QUESTIONS

1. Discuss about ADT.

Answer: Abstract Data Type (ADT) is a theoretical concept that represents a data type by its behavior
rather than its implementation. It defines a set of operations that can be performed on the data along with
the behavior of these operations. The actual implementation details are hidden, providing an abstraction
layer that allows programmers to focus on using the data structure rather than worrying about its internal
representation.

Characteristics of ADT:
i. Encapsulation: ADT encapsulates data and operations into a single unit.
ii. Data Hiding: The internal representation of data is hidden from the user.
iii. Interface: ADT provides a set of operations that can be performed on the data, defining its behavior.
iv. Flexibility: ADTs can be implemented in various ways, providing flexibility in choosing the most
suitable implementation based on requirements.

2. Write short note on non-linear data type with proper example.


Answer: Non-linear data types do not follow a sequential order and may have multiple branches or levels.
One common example of a non-linear data type is a tree.

Short note:

Trees are hierarchical data structures composed of nodes connected by edges.


Each node contains a value and references to its child nodes.

Nodes are organized in levels, with the top node called the root and the bottom nodes called leaves.

Trees are used to represent hierarchical relationships, such as file systems, organization charts, and family
trees.

Example: Binary Search Tree (BST)

A binary search tree is a specific type of tree where each node has at most two children.

The left child of a node contains a value less than the node's value, while the right child contains a value
greater than the node's value.

BSTs are commonly used for searching, insertion, and deletion operations due to their efficient structure.

3. What is complexity? Discuss its fields where complexity is measured.

Answer: Complexity refers to the measurement of resources required by an algorithm to execute as a


function of the input size. It helps in analysing the efficiency and scalability of algorithms. Complexity is
measured in terms of time complexity and space complexity.

Fields where complexity is measured:

Time Complexity: Time complexity measures the amount of time an algorithm takes to complete as a
function of the input size. It indicates how the runtime of an algorithm grows with increasing input size.

Space Complexity: Space complexity measures the amount of memory space an algorithm requires to
execute as a function of the input size. It indicates how the memory usage of an algorithm grows with
increasing input size.

4. Differentiate between 1D and 2D array.

Answer: 1D Array:

A 1D array is a linear data structure that stores elements in a single row or column.

It is accessed using a single index.

Example: Array of integers [1, 2, 3, 4, 5]

2D Array:

A 2D array is a collection of multiple 1D arrays arranged in rows and columns to form a grid-like structure.

It is accessed using two indices, one for the row and another for the column.

Example: Matrix of integers [[1, 2, 3], [4, 5, 6], [7, 8, 9]]


5. Differentiate between primitive and non-primitive data type.
Answer: Primitive Data Type:

Primitive data types are basic data types predefined by the programming language.

They are directly supported by the language and typically have fixed sizes.

Examples: int, float, char, boolean

Non-primitive Data Type:

Non-primitive data types are derived from primitive data types or other non-primitive types.

They are also called reference types as they store references to memory locations where data is stored.

Examples: Arrays, Classes, Interfaces

Differences:

Storage: Primitive data types store actual values, whereas non-primitive data types store references to
objects.

Size: Primitive data types have fixed sizes determined by the language, while non-primitive data types can
vary in size depending on the data they hold.

Mutability: Primitive data types are immutable (cannot be changed), whereas non-primitive data types can
be mutable.

Examples: Primitive data types include integers, floats, etc., while non-primitive data types include arrays,
classes, etc.

III. LONG ANSWER QUESTIONS


1. Define Recursion with an example.

Answer: Recursion is a programming technique where a function calls itself in order to solve a problem.
It involves breaking down a problem into smaller subproblems, solving each subproblem recursively,
and combining their results to solve the original problem.

Example:

# Factorial using recursion

def factorial(n):

if n == 0:

return 1

else:
return n * factorial (n - 1)

Num = 5

Print ("Factorial of", num, "is", factorial(num))

In this example, the factorial () function calls itself with a smaller value (n - 1) until the base case (n == 0) is
reached. Each recursive call calculates the factorial of a smaller number, and the results are combined to find
the factorial of the original number.

2. Write a program to add an element in a 1D array at a specific position

Answer: def add_element (arr, element, position):

arr. Insert (position, element)

array = [1, 2, 3, 4, 5]

new_element = 10

position = 2

add_element (array, new_element, position)

print ("Updated Array:", array)

Output:

Updated Array [1, 2, 10, 3, 4, 5]

3. Write a program to add and subtract two matrices.


Answer: def add matrices (matrix1, matrix2):
result = [[matrix1[i][j] + matrix2[i][j] for j in range(len(matrix1[0]))] for i in range(len(matrix1))]
return result

def subtract_matrices (matrix1, matrix2):


result = [[matrix1[i][j] - matrix2[i][j] for j in range(len(matrix1[0]))] for i in range(len(matrix1))]
return result

matrix1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]


matrix2 = [[9, 8, 7], [6, 5, 4], [3, 2, 1]]

print ("Matrix 1:", matrix1)


print ("Matrix 2:", matrix2)
print ("Matrix Addition:", add_matrices (matrix1, matrix2))
print ("Matrix Subtraction:", subtract_matrices (matrix1, matrix2))
4. Discuss all types of linear data structure with example.

Answer: Linear data structures are arrangements of data elements in which each element is connected to its
previous and next element in a linear order. The main types of linear data structures are arrays, linked lists,
stacks, and queues.

Arrays: Arrays are a collection of elements of the same data type arranged in contiguous memory locations.
Example: int array [] = {1, 2, 3, 4, 5};

Linked Lists: Linked lists are a collection of nodes where each node contains data and a reference (or
pointer) to the next node in the sequence. Example: 1 -> 2 -> 3 -> 4 -> 5

Stacks: Stacks are a collection of elements with Last-In-First-Out (LIFO) order. Elements are added and
removed from the same end, typically called the top of the stack. Example: function call stack.

Queues: Queues are a collection of elements with First-In-First-Out (FIFO) order. Elements are added to the
rear and removed from the front. Example: waiting line in a cafeteria.

5. Write a program to delete an element in an 1D array from a specific position.


Answer: def delete_element (arr, position):
if position < 0 or position >= len(arr):
print ("Invalid position!")
return
arr.pop(position)

array = [1, 2, 3, 4, 5]
position = 2
delete_element (array, position)
print ("Updated Array:", array)

Output:
Updated Array: [1, 2, 4, 5]

You might also like