Module 1
Module 1
These tasks can be achieved with the help of Data structures and
the Algorithms
p=&arr[0] = 1024
p+1= &arr[1] = 1028
p+2= &arr[2] = 1032
p+3=& arr[3] = 1036
p+4=& arr[4] = 1028
Thus, *(p) gives the value at arr[0], *(p+2) gives value at arr[2]
Dept. of CSE, YIT Moodbidri
Questions?.....
Syntax of calloc()
ptr = (castType*) calloc(n,size);
Example:
ptr = (float*) calloc(25, sizeof(float));
Example:
ptr = realloc( ptr, 50);
Malloc calloc
Allocated memory locations are not Initializes all bytes in the allocated
initialized block to zero
The first array defines five integers, while the second defines five
pointers to integers.
In C all arrays start at index 0, so list[0], list[l], list[2], list[3], and
list[4] are the names of the five array elements, each of which
contains an integer value.
ARRAY IN DETAIL
One-dimensional Array
Two-dimensional Array
Output:
Output:
#include <stdio.h>
#include<stdlib.h>
void main()
{
int **array;
int rows, cols, i, j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
array = (int**) malloc(rows * sizeof(int*)); // Allocate memory for
rows
Definition of structure
Declare/Define Structure
Declare Structure Variable
Assign values to members of structure
Create own structures using typedef
Structures and Arrays
Structures and Pointers
Union
Difference between Structure and union
We can create a new data type for the exisiting data type using the
keyword struct and typedef .
Syntax: Example:
typedef struct structure_name typedef struct Student
{ {[
datatype member1; int rollNo;
datatype member2; char name[20];
…… int sem;
datatype memberN float marks;
}; }Engg,Med;
structure_name var1,var2; Engg e1, Med m1;
Definition of polynomial
Polynomial Representation
Program to add two poly with n terms
The term with exponent equal to zero does not show the variable
In the array representation of a sparse matrix ,only the non zero
elements are stored so, storage space can be reduced.
Each non zero elements in the matrix are represented as triplet:
<row,col,value>
For this the two dimensional array containing 3 columns can be
used.
The first column is for storing the row numbers
The second column is for storing the column numbers
The third column is for storing the value (corresponding to the
non zero element at (row,column))
Transpose of a Matrix:
Definition: A matrix which is obtained by changing the row
elements into column elements and column elements into row
elements is called transpose of a matrix.
Example:
MATRIX TRANSPOSE MATRIX
It is very easy and simple method to find the transpose of sparse matrix
Time complexity is O (num of col *num of non zero values)
Steps
1. Obtain the triplet form of sparse matrix
2. Inititalze currentB to 1
3. Traverse triplet matrix from second row and consider the column
element
4. Check if column number is equal to i, then swap its row and column
and add it to transpose matrix then increment currentB
5. Repeat above steps for all rows
6. Repeat step 3 and 4 for the column number values 1,2,3,4( total
number of columns)
Steps
1. Obtain the triplet form of sparse matrix
2. Create two 1-D arrays: row_terms and starting_pos
3. Size of row_terms array is the total number of columns in the original
matrix
4. At every index of row_terms,put the number of times respective index
appear in second column of sparse matrix
5. Size of starting_pos array: size of row_terms+1
6. Assign starting_pos[0]=1 and starting_pos[i]=starting_pos[i-
1]+row_terms[i-1]
7. Now traverse the sparse matrix from second row and consider column
element
8. J = starting_pos[col_no]
9. Jth index of transpose matrix = swapped row from original matrix
10.Increase starting_pos[col_no] by 1
11.Repeat steps from 7 to 10 for the remaining triplets of original matrix
Definition of Stack
Operations Performed on Stack
Applications of Stack
Stack using Dynamic Array
Evaluation of Expression
Expression
Evaluating Postfix Expression
Converting infix to Postfix
Definition:
Algorithm:
1. Scan the infix expression from left to right
2. If scanned character is operand then push it into postfix expression.
3. If the scanned character is operator then push it on to the stack.
4. If the scanned operator is greater than stacked operator then the
scanned operator is pushed into the stack.
5. If the scanned operator is less than or same than stacked operator then
the stacked operator will be popped out from stack and pushed into
postfix expression and scanned operator is pushed into the stack.
6. If the scanned operator is right parenthesis then till left parenthesis of
stack all operators from stack are popped and put into postfix expression
and cancel the parenthesis.
7. Repeat steps from 1 to 6 until end of expression.
Algorithm:
1. Scan the postfix expression from left to right
2. If scanned character is operand then push it into stack.
3. If the scanned character is operator then pop top two operands from the
stack perform the operation and later push result back into the stack.
4. Repeat steps from 1 to 3 until end of expression.