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

data dtructure and algo class 1v

assignment engineering 2nd year
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

data dtructure and algo class 1v

assignment engineering 2nd year
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

ASSIGNMENT 2

Q1. Define sparse matrix. Briefly explain representation of sparse matrix with
the help of link list and array.

A sparse matrix is a matrix in which the majority of its elements are zero. In other words, it
is a matrix where only a small number of elements have non-zero values. Storing sparse
matrices using traditional two-dimensional array structures can be inefficient in terms of both
space and time, as it would allocate space for all the elements (including zeros). Instead,
specialized data structures can be used to efficiently store and manipulate sparse matrices.

Representation of Sparse Matrix

Sparse matrices can be represented using various methods, but two common representations
are through linked lists and arrays.

1. Representation Using Linked Lists

In the linked list representation, only the non-zero elements of the matrix are stored along
with their row and column indices. This approach reduces space complexity significantly.

Structure of a Node: Each node in the linked list can have the following structure:

• row: The row index of the element.


• col: The column index of the element.
• value: The non-zero value of the element.
• next: A pointer to the next node in the linked list.

Q2. Given a two dimensional array A1(1:8, 7:14) stored in row-major order with
base address 100 and size of each element is 4 bytes, find address of the element
A1(4, 12).

To find the address of the element ( A1(4, 12) ) in a two-dimensional array stored in
row-major order, we can use the following formula:

[ \text{Address}(A[i, j]) = \text{Base Address} + \left( (i - \text{base row}) \times


\text{number of columns} + (j - \text{base column}) \right) \times \text{size of each
element} ]

In this case:

• Base Address = 100


• Base row = 1
• Base column = 7
• Number of columns = ( 14 - 7 + 1 = 8 ) (columns from 7 to 14)
• Size of each element = 4 bytes
• (i=4)
• ( j = 12 )

Now we can plug in the values into the formula:

1. Calculate the offset: [ \text{offset} = (4 - 1) \times 8 + (12 - 7) ] [ = 3 \times 8 +


5 = 24 + 5 = 29 ]

2. Calculate the address of ( A1(4, 12) ): [ \text{Address}(A1(4, 12)) = 100 + 29


\times 4 ] [ = 100 + 116 = 216 ]

Thus, the address of the element ( A1(4, 12) ) is 216.

Q3. Given a two dimensional array Z1(2:9, 9:18) stored in column-major order
with base address 100
and size of each element is 4 bytes, find address of the element Z1(4, 12).

To find the address of the element ( Z1(4, 12) ) in a two-dimensional array stored in
column-major order, we can use the following formula:

[ \text{Address}(Z[i, j]) = \text{Base Address} + \left( (j - \text{base column}) \times


\text{number of rows} + (i - \text{base row}) \right) \times \text{size of each element} ]

Given:

• Base Address = 100


• Base row = 2
• Base column = 9
• Number of rows = ( 9 - 2 + 1 = 8 ) (rows from 2 to 9)
• Number of columns = ( 18 - 9 + 1 = 10 ) (columns from 9 to 18)
• Size of each element = 4 bytes
• (i=4)
• ( j = 12 )

Now we can plug in the values into the formula:

1. Calculate the offset: [ \text{offset} = (j - \text{base column}) \times


\text{number of rows} + (i - \text{base row}) ] [ = (12 - 9) \times 8 + (4 -
2) ] [ = 3 \times 8 + 2 = 24 + 2 = 26 ]

2. Calculate the address of ( Z1(4, 12) ): [ \text{Address}(Z1(4, 12)) =


100 + 26 \times 4 ] [ = 100 + 104 = 204 ]
Thus, the address of the element ( Z1(4, 12) ) is 204.

Q4. What is stack? Explain basic primitive operation of stack with example?

A stack is a linear data structure that follows the Last In, First Out (LIFO) principle,
meaning that the last element added to the stack is the first one to be removed. It’s analogous
to a stack of plates, where you can only add or remove the top plate.

Concepts of Stack

• Push: Add an element to the top of the stack.


• Pop: Remove the element from the top of the stack.
• Peek (or Top): View the element on the top of the stack without removing it.
• IsEmpty: Check if the stack is empty.

Q5. Write a C program to implement a stack with all necessary overflow and
underflow checks using array.
#include <stdio.h>
#include <stdlib.h>

#define MAX 100

struct Stack {
int top;
int arr[MAX];
};

void initialize(struct Stack* stack) {


stack->top = -1;
}

int isFull(struct Stack* stack) {


return stack->top == MAX - 1;
}

int isEmpty(struct Stack* stack) {


return stack->top == -1;
}

void push(struct Stack* stack, int value) {


if (isFull(stack)) {
printf("Stack Overflow\n");
return;
}
stack->arr[++stack->top] = value;
}

int pop(struct Stack* stack) {


if (isEmpty(stack)) {
printf("Stack Underflow\n");
return -1;
}
return stack->arr[stack->top--];
}

int peek(struct Stack* stack) {


if (isEmpty(stack)) {
printf("Stack is empty\n");
return -1;
}
return stack->arr[stack->top];
}

int main() {
struct Stack stack;
initialize(&stack);
int choice, value;

while (1) {
printf("1. Push\n2. Pop\n3. Peek\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(&stack, value);
break;
case 2:
value = pop(&stack);
if (value != -1) {
printf("Popped value: %d\n", value);
}
break;
case 3:
value = peek(&stack);
if (value != -1) {
printf("Top value: %d\n", value);
}
break;
case 4:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}

Q4. Write an algorithm to reverse a string of characters using stack.


To reverse a string using a stack, we can follow this algorithm. The key idea is to push each
character of the string onto the stack and then pop each character off the stack to reverse the
order.
Algorithm to Reverse a String Using a Stack

1. Initialize an empty stack.


2. Push each character of the string onto the stack, one by one.
3. Pop each character from the stack and append it to a new string. Since stacks follow Last In,
First Out (LIFO), the characters will come off the stack in reverse order.
4. Return or print the reversed string.

Q5. What is the advantage of Polish expression over infix notation? Write an
algorithm to convert an infix expression into reverse Polish expression

Polish notation, also known as prefix notation, and Reverse Polish Notation (RPN) (postfix
notation) have distinct advantages over traditional infix notation when it comes to evaluating
expressions.

Advantages of Polish Notation (Prefix and Postfix) over Infix Notation

1. No Need for Parenthese


2. Simplified Evaluation
3. Better suited for Computers

Algorithm to Convert Infix Expression to Reverse Polish Notation (Postfix)

To convert an infix expression (like A+B×CA + B \times CA+B×C) to postfix (Reverse


Polish Notation), we can use the Shunting Yard Algorithm, which makes use of a stack for
operators and ensures that operators are applied in the correct order.

Steps of the Algorithm

1. Initialize an empty stack for operators and an empty list (or string) for the output (postfix
expression).
2. Scan each character in the infix expression from left to right:
o If the character is an operand (e.g., a variable or number), append it to the output
list.
o If the character is an operator (e.g., +, -, *, /):
▪ While there is an operator at the top of the stack with greater or equal
precedence than the current operator (and the stack is not empty), pop
operators from the stack to the output list.
▪ Push the current operator onto the stack.
o If the character is an opening parenthesis (, push it onto the stack.
o If the character is a closing parenthesis ):
▪ Pop operators from the stack to the output list until an opening parenthesis
( is encountered.
▪ Discard the opening parenthesis.
3. After the expression has been fully scanned, pop any remaining operators from the stack to
the output list.
4. The output list now contains the postfix (RPN) expression.
Q6. Convert the given infix expression to postfix expression.

1. Expression: A + ( (B – C) * (D – E) + F) / G $ (H – J)

• Postfix: A B C - D E - * F + G / H J - $ +

2. Expression: (A + B) * (C – D) $ E * F

• Postfix: A B + C D - E $ F * *

3. Expression: (A + B) * (C ^ (D – E) + F) – G

• Postfix: A B + C D E - ^ F + * G -

4. Expression: A + B * C

• Postfix: A B C * +

5. Expression: A + B * C ^ D – E

• Postfix: A B C D ^ * + E -

6. Expression: A + [(B + C) + (D + E) * F] / G

• Postfix: A B C + D E + F * + G / +

7. Expression: (A + B) * C / D + E ^ F / G

• Postfix: A B + C * D / E F ^ G / +

8. Expression: (A + B) * C / D

• Postfix: A B + C * D /
9. Expression: ((A + B – C / D) / E)

• Postfix: A B + C D / - E /

10. Expression: A / (B – C / D ^ E) + F

• Postfix: A B C D E ^ / - / F +

11. Expression: A – B / (C * D ^ E)

• Postfix: A B C D E ^ * / -

Q7. Evaluate the following expressions.

1) 5+4×25 + 4 \times 25+4×2

According to the order of operations (PEMDAS: Parentheses, Exponents, Multiplication and


Division (left to right), Addition and Subtraction (left to right)):

• 4×2=84 \times 2 = 84×2=8


• 5+8=135 + 8 = 135+8=13

Result: 13

2) 4+2×52+9/3−1×84 + 2 \times 5^2 + 9 / 3 - 1 \times 84+2×52+9/3−1×8

Following the order of operations:

• 52=255^2 = 2552=25
• 2×25=502 \times 25 = 502×25=50
• 9/3=39 / 3 = 39/3=3
• 1×8=81 \times 8 = 81×8=8
• Now we have: 4+50+3−84 + 50 + 3 - 84+50+3−8
• 4+50=544 + 50 = 544+50=54
• 54+3=5754 + 3 = 5754+3=57
• 57−8=4957 - 8 = 4957−8=49

Result: 49
3) 9+5×7−62+9/39 + 5 \times 7 - 6^2 + 9 / 39+5×7−62+9/3

Following the order of operations:

• 5×7=355 \times 7 = 355×7=35


• 62=366^2 = 3662=36
• 9/3=39 / 3 = 39/3=3
• Now we have: 9+35−36+39 + 35 - 36 + 39+35−36+3
• 9+35=449 + 35 = 449+35=44
• 44−36=844 - 36 = 844−36=8
• 8+3=118 + 3 = 118+3=11

Result: 11

5) 8×2−1+7×58 \times 2 - 1 + 7 \times 58×2−1+7×5

Following the order of operations:

• 8×2=168 \times 2 = 168×2=16


• 7×5=357 \times 5 = 357×5=35
• Now we have: 16−1+3516 - 1 + 3516−1+35
• 16−1=1516 - 1 = 1516−1=15
• 15+35=5015 + 35 = 5015+35=50

Result: 50

Q8.Evaluate the following Postfix expression assume A=1, B=2, C=3

1) Postfix Expression: AB+C−BA+C−+


Substitute the values:
12+3−21+3−+

1. 12+: 1+2=3

• Stack: [3,3,−,2,1,+,3,−,+]

2. 3−: 3−3=0
• Stack: [0,2,1,+,3,−,+]

3. 21+: 2+1=3

• Stack: [0,3,3,−,+]
4. 31+: 3+1=4

• Stack: [5,4,∗]

5. 54∗: 5×4=20

Result: 20

You might also like