T01 DS Introduction
T01 DS Introduction
Strictly for internal circulation (within KIIT) and reference only. Not for outside circulation without permission
Prerequisites
Programming in C
Mathematics for Computer Science
Mid Semester
3 Trees 10
Tree representation, Binary Trees, Binary search trees, Tree traversal, Expression manipulation, Symbol table
construction, Height balanced trees, AVL trees.
4 Graph 5
Graphs, Representation of graphs, BFS, DFS, Topological sort, String representation and manipulations, Pattern
matching.
5 Sorting and Searching 8
Sorting Techniques: Selection, Bubble, Insertion, Merge, Heap, Quick, Radix sort, Linear search, Binary search, Hash table
methods.
End Semester
Textbook
Ellis Horowitz, Sartaj Sahni, Susan Anderson-Freed, "Fundamentals of Data
Structures in C", Second Edition, University Press.
Reference Books
J. P. Tremblay, P. G. Sorenson, “An Introduction to Data Structures with Applications”,
Second Edition, Tata McGraw Hill, 1981.
M. Tenenbaum, Augestien, “Data Structures using C”, Third Edition, Pearson
Education, 2007.
Mark Allen Weiss, “Data Structures and Algorithm Analysis in C”, Second Edition,
Addison-Wesley Educational Publishers, 2006.
Grading:
?
School of Computer Engineering
Abstract Data Type
8
Abstract Data Type, abbreviated ADT, is a logical description of how to view the data
and the operations that are allowed without regard to how they will be implemented or
how it does the job. This means it is like a black box where users can only see the
syntax and semantics of operation and hides the inner structure and design of the data
type.
The definition of ADT only mentions what operations are to be performed but not how
these operations will be implemented. It does not specify how data will be organized in
memory and what algorithms will be used for implementing the operations. It is called
“abstract” because it gives an implementation independent view.
An ADT consists of 2 parts: (1) declaration of data, (2) declaration of operations
Commonly used ADTs: Arrays, List, Stack, Queues, Trees, Graphs etc along with their
operations.
It is basically a group of data elements that are put together under one name and
defines a particular way of storing and organizing data in a computer so that it
can be used efficiently.
For example, cricket player's name "Virat" & age 28. "Virat" is of string data type
and 28 is of integer data type. This data can be organized as a record like Player
record. Now players record can be collected and store in a file or database as a
data structure. For example: "Dhoni" 30, "Gambhir" 31, "Sehwag" 33.
Characteristics Description
Linear The data items are assessed in a linear sequence, but it is not compulsory
to store all elements sequentially. Example: Array
Non-Linear The data items are stored/accessed in a non-linear order. Example: Tree,
Graph
Homogeneous All the elements are of same type. Example: Array
Heterogeneous The elements are variety or dissimilar type of data Example: Structures
Static Are those whose sizes and structures associated memory locations are
fixed, at compile time. Example: Array
Dynamic Are those which expands or shrinks depending upon the program need
and its execution. Also, their associated memory locations changes.
Example: Linked List created using pointers
What we are doing is, for a given problem (preparing an omelette), we are
providing a step-by step procedure for solving it.
Data
ADT + Structure + Algorithm
Need?
1. Input. There are zero or more quantities that are externally supplied.
2. Output. At least one quantity is produced.
3. Definiteness. Each instruction is clear and unambiguous.
4. Finiteness. If we trace out the instructions of an algorithm, then for all cases, the algorithm
terminates after a finite number of steps.
5. Effectiveness. Every instruction must be basic enough to be carried out, in principle, by a
person using only pencil and paper. It is not enough that each operation be definite as in (3); it
also must be feasible.
Difference between an algorithm & program – program does not have to satisfy the 4th condition.
Describing Algorithm
1. Natural Language – e.g. English, Chinese - Instructions must be definite and effectiveness.
2. Graphics representation – e.g. Flowchart - work well only if the algorithm is small and simple.
3. Pseudo Language -
• Readable
• Instructions must be definite and effectiveness
4. Combining English and C
Start
Input
M1 to M5
Stop
School of Computer Engineering
Pseudo Language
18
The Pseudo language is neither an algorithm nor a program. It is an abstract form of a program. It consists of
English like statements which perform the specific operations. It employs programming-like statements to
depict the algorithm and no standard format (language independent) is followed. The statements are carried out
in a order & followings are the commonly used statements.
e.g.
e.g. IF (amount < 100) THEN
IF (amount < 100) THEN interestRate .06
interestRate .06 ELSE
ENDIF interest Rate .10
ENDIF
e.g. e.g.
count 0 count 0
REPEAT WHILE (count < 10)
ADD 1 to count ADD 1 to count
OUTPUT: count OUTPUT: count
UNTIL (count < 10) ENDWHILE
OUTPUT: “The End” OUTPUT: “The End”
Write only one statement per line Each statement in pseudo code should express just one action for the computer. If the task list is properly
drawn, then in most cases each task will correspond to one line of pseudo code.
Task List Pseudo code
Read name, hours worked, rate of pay INPUT: name, hoursWorked, payRate
gross = hours worked * rate of pay gross hoursWorked * payRate
Write name, hours worked, gross OUTPUT: name, hoursWorked, gross
Capitalize initial keyword In the example above note the words: INPUT and OUTPUT. These are just a few of the keywords to use,
others include: IF, ELSE, REPEAT, WHILE, UNTIL, ENDIF
Indent to show hierarchy Each design structure uses a particular indentation pattern.
Sequence - Keep statements in sequence all starting in the same column
Selection - Indent statements that fall inside selection structure, but not the keywords that form the
selection
Loop - Indent statements that fall inside the loop but not keywords that form the loop
INPUT: name, grossPay, taxes
IF (taxes > 0)
net grossPay – taxes
ELSE
net grossPay
ENDIF
OUTPUT: name, net
Guidelines Explanation
Watch the IF/ELSE/ENDIF as constructed above, the ENDIF is in line with the IF. The same applies for
WHILE/ENDWHILE etc…
Keep statements language Resist the urge to write in whatever language you are most comfortable with, in the long run you will save
independent time. Remember you are describing a logic plan to develop a program, you are not programming!
1. Problem - Design the pseudo code to add two numbers and display the average.
INPUT: x, y
sum x + y
average sum / 2
OUTPUT: ‘Average is:’ average
2. Problem - Design the pseudo code to calculate & display the area of a circle
INPUT: radius
area 3.14 * radius * radius
OUTPUT: ‘Area of the circle is ‘ area
2. Problem - Design the pseudo code to calculate & display the largest among 2 numbers
INPUT: num1, num2
max num1
IF (num2 > num 1) THEN
max num2
ENDIF
OUTPUT: ‘Largest among 2 numbers is’ max
Theorem
Function sort(a, n) correctly sorts a set of n>= 1 integers. The result
remains in a[0], ..., a[n-1] such that a[0]<= a[1]<=...<=a[n-1].
Proof
For i= q, following the execution of lines, we have a[q]<= a[r], q< r< =n-1.
For i> q, observing, a[0], ..., a[q] are unchanged.
Hence, increasing i, for i= n-2, we have a[0]<= a[1]<= ...<=a[n-1]
int func2(int n)
{
return func1(n);
}
#include<stdio.h>
void Fibonacci(int); //continuation of program
int main() void Fibonacci(int n)
{ {
int k,n; static long int first=0,second=1,sum;
long int i=0,j=1,f;
if(n>0) Base Criteria
printf("Enter the range of the Fibonacci series: "); {
scanf("%d",&n); sum = first + second;
first = second;
printf("Fibonacci Series: "); second = sum;
printf("%d %d ",0,1); printf("%ld ",sum);
Fibonacci(n); Fibonacci(n-1); Progressive Criteria
return 0; }
} }
Hence, many solution’s algorithms can be derived for a given problem. Next step
is to analyze those proposed solution algorithms and implement the best
suitable.
A priori analysis
School of Computer Engineering
Algorithm Complexity
32
Suppose X is an algorithm and n is the size of input data, the time and
space used by the Algorithm X are the two main factors which decide the
efficiency of X.
Space complexity of an algorithm represents the amount of memory space required by the
algorithm in its life cycle. Space required by an algorithm is equal to the sum of the following two
components −
A fixed part that is a space required to store certain data and variables, that are independent of
the size of the problem. For example simple variables & constant used, program size etc.
A variable part is a space required by variables, whose size depends on the size of the problem.
For example dynamic memory allocation, recursion stack space etc.
Space complexity SC(P) of any algorithm P is SC(P) = FP + VP (I) where FP is the fixed part and
VP(I) is the variable part of the algorithm which depends on instance characteristic I. Following is a
simple example that tries to explain the concept −
Algorithm: SUM(A, B) 3 variables and data type of each variable is int, So VP(3)
Step 1 - START = 3*2 = 6 bytes (No. of characteristic i.e. I = 3)
Step 2 - C ← A + B + 10 1 constant (i.e. 10) and it is int, so FP = 1*2 = 2 bytes
Step 3 - Stop So - SC(SUM) = FP + VP (3) = 2 + 6 = 8 bytes
Example 1 Example 2
int sum(int a[], int n)
int square(int a) {
{ int sum = 0, i;
return a*a; for(i = 0; i < n; i++) { sum = sum + a[i];}
} return sum;
In above piece of code, it requires 2 bytes of }
In above piece of code it requires -
memory to store variable 'a' and another 2
• 2*n bytes of memory to store array variable ‘a[]’
bytes of memory is used for return value. • 2 bytes of memory for integer parameter 'n‘
• 4 bytes of memory for local integer variables 'sum'
That means, totally it requires 4 bytes of and 'i' (2 bytes each)
memory to complete its execution. And this • 2 bytes of memory for return value.
4 bytes of memory is fixed for any input
value of 'a'. This space complexity is said to That means, totally it requires '2n+8' bytes of memory to
complete its execution. Here, the amount of memory depends
be Constant Space Complexity.
on the input value of 'n'. This space complexity is said to be
Linear Space Complexity.
If any algorithm requires a fixed amount of
space for all input values then that space If the amount of space required by an algorithm is increased
complexity is said to be Constant Space with the increase of input value, then that space complexity is
Complexity said to be Linear Space Complexity
For example, addition of two n-bit integers takes n steps. Consequently, the total
computational time is T(n) = c*n, where c is the time taken for addition of two bits.
Here, we observe that T(n) grows linearly as input size increases.
Asymptotic analysis
Asymptotic analysis of an algorithm, refers to defining the mathematical
foundation/framing of its run-time performance.
Asymptotic analysis are input bound i.e., if there's no input to the algorithm it is
concluded to work in a constant time. Other than the "input" all other factors are
considered constant.
School of Computer Engineering
Time Complexity cont… Asymptotic analysis
36
For example,
- Running time of one operation is computed as f(n)
- May be for another operation it is computed as g(n2)
Ω Notation – “Omega” - The Ω(n) is the formal way to express the lower
bound of an algorithm's running time. It measures the best case time
complexity or best amount of time an algorithm can possibly take to complete.
θ Notation – “Theta” - The θ(n) is the formal way to express both the lower
bound and upper bound of an algorithm's running time.
Sr # Notation Name
1 O(1) Constant
2 O(log(n)) Logarithmic
3 O(log(n)c) Poly-logarithmic
4 O(n) Linear
5 O(n2) Quadratic
6 O(nc) Polynomial
7 O(cn) Exponential
Note: c is constant
Time complexity of nested loops is equal to the number of times the innermost
statement is executed that is nothing but the multiplication of outer loop complexity
into inner loop complexity.
Sr # Program Segment Time Complexity Explanation
1 for (int i = 1; i <=m; i += c) O(m+n) First for outer i loop O(m), second for
{ If m=n, then inner i loop O(n) times, so total
// some O(1) expressions O(2n)=O(n) O(m+n) times
}
for (int i = 1; i <=n; i += c)
{
// some O(1) expressions
}
2 for (int i = 1; i <=n; i *= 2) O(n) First for outer i log n times, second inner i it
{ is n times. Now total=log n + n = O(n) as n
// some O(1) expressions is asymptotically larger than log n.
}
for (int i = 1; i <=n; i ++)
{
// some O(1) expressions
}
3 int i, j, k,l; O(n log n) Nested for-ij loop is executed n log
for (i = 1; i <=n; i ++) n times. k loop log n times, l loop
{ n times. So total= n log n + log n + n =
for (j = 1; j <=n; j=j*2) {p=i+j;} O(n log n) as n log n > n > log n
}
Arrays
Array is a container which can hold fix number of items and these items should be of
same type. Following are important terms to understand the concepts of Array. Arrays
are of one-dimensional or multi-dimensional (i.e. 2 or more than 2)
Element − Each item stored in an array.
Index − Each location of an element in an array has a numerical index which is used
to identify the element.
One-Dimensional array is also called as linear array and stores the data in a single row or column.
Array of an element of an array say “A[i]” is calculated using the following formula:
Address of A [i] = BA(A) + w * ( i – LB )
Where,
BA(A) = Base Address of array A
w = Storage Size of one element stored in the array (in byte)
i = Subscript of element whose address is to be found
LB = Lower limit / Lower Bound of subscript, if not specified assume 0 (zero)
Example
Problem: Given the base address of an array B[1300…..1900] as 1020 and size of each
element is 2 bytes in the memory. Find the address of B[1700].
Solution:
The given values are: BA(B) = 1020, LB = 1300, W = 2, i = 1700
Address of B [i] = BA(B) + w * ( i – LB )
= 1020 + 2 * (1700 – 1300)
= 1020 + 2 * 400
= 1020 + 800 = 1820
Note : C supports row major order and Fortran supports column major order
School of Computer Engineering
Row major & Column order Address Calculation
56
Row-Major Order: The address of a location in Row Major System is calculated as:
Address of A [i][j] = BA + w * [ n * ( i – Lr ) + ( j – Lc ) ]
Column-Major Order: The address of a location in Column Major System is
calculated as:
Address of A [i][j] = BA + w * [( i – Lr ) + m * ( j – Lc )]
Where:
BA = Base Address
i = Row subscript of element whose address is to be found
j = Column subscript of element whose address is to be found
w = Storage Size of one element stored in the array (in byte)
Lr = Lower limit of row/start row index of matrix, if not given assume 0 (zero)
Lc = Lower limit of column/start column index of matrix, if not given assume 0 (zero)
m = Number of row of the given matrix
n = Number of column of the given matrix
Important: Usually number of rows and columns of a matrix are given (like A[20][30] or
A[40][60] ) but if it is given as A[Lr- – – – – Ur, Lc- – – – – Uc]. In this case number of rows
and columns are calculated using the following methods:
Row-Major:
The given values are: B = 1500, W = 1 byte, i = 8, j = 20, Lr = -15, Lc = 15, N = 26
Address of A [ i ][ j ] = BA + w * [ n * ( i – Lr ) + ( j – Lc ) ]
= 1500 + 1* [26 * (8 – (-15))) + (20 – 15)]
= 1500 + 1 * [26 * 23 + 5] = 1500 + 1 * [598 + 5] = 1500 + 603 = 2103
Column-Major :
The given values are: BA = 1500, w = 1 byte, i = 8, j = 20, Lr = -15, Lc = 15, m = 26
Address of A [ i ][ j ] = BA + w * [( i – Lr ) + m * ( j – Lc )]
= 1500 + 1* [(8 – (-15)) + 26 * (20 – 15) ]
= 1500 + 1 * [23 + 26 * 5] = 1500 + 1 * [153] = 1653
An abstract data type (ADT) is a mathematical model for data types where a data
type is defined by its behavior (semantics) from the point of view of a user of the
data, specifically in terms of possible values, possible operations on data of this
type, and the behavior of these operations. When considering Array ADT we are
more concerned with the operations that can be performed on array.
Insertion Deletion
Insert operation is to insert one or more data elements Deletion refers to removing an existing element from
into an array. Based on the requirement, new element the array and re-organizing all elements of an array.
can be added at the beginning or end or any given
position of array. Consider LA is a linear array with N elements and K is
a positive integer such that K<=N. Below is the
Let LA is a filled linear array (unordered) with N elements algorithm to delete an element available at the Kth
and K is a positive integer such that K<=N. Below is the position of LA. Procedure - DELETE(LA, N, K)
algorithm where ITEM is inserted into the Kth position of
LA. Procedure – INSERT(LA, N, K, ITEM) 1. Start
2. Set J = K
1. Start
3. Repeat step 4 while J < N
2. Set J=N
4. Set LA[J] = LA[J+1] /* Move the element upward*/
3. Set N = N+1 /* Increase the array length by 1*/
5. Set N = N-1 /* Reduce the array length by 1 */
4. Repeat steps 5 and 6 while J >= K
6. Stop
5. Set LA[J+1] = LA[J] /* Move the element downward*/
6. Set J = J-1 /* Decrease counter*/
7. Set LA[K] = ITEM
8. Stop
Search Updation
You can perform a search for array element Update operation refers to updating an existing
based on its value or position. element from the array at a given position.
Consider LA is a linear array with N elements. Consider LA is a linear array with N elements
Below is the algorithm to find an element with a and K is a positive integer such that K<=N.
value of ITEM using sequential search. Below is the algorithm to update an ITEM
Procedure - SEARCH(LA, N, ITEM) available at the Kth position of LA. Procedure -
UPDATE(LA, N, K, ITEM)
1. Start
2. Set J=1 and LOC = 0 1. Start
3. Repeat steps 4 and 5 while J < N
2. Set LA[K] = ITEM
4. IF (LA[J] = ITEM) THEN LOC = J AND GOTO STEP 6
3. Stop
5. Set J = J +1
6. IF (LOC > 0) PRINT J, ITEM ELSE PRINT ‘Item not
found’
7. Stop
Traversal Sorting
Traversal operation refers to printing the Sorting operation refers to arranging the elements
contents of each element or to count the number either in ascending or descending way.
of elements with a given property
Consider LA is a linear array with N elements. Below
Consider LA is a linear array with N elements. is the Bubble Sort algorithm to sort the elements
in ascending order. Procedure - SORT(LA, N)
Below is the algorithm to print each element.
Procedure - TRAVERSE(LA, N)
1. Start
2. Set I = 1
1. Start 3. Set J = 1
4. Repeat steps 5 to 11 while I ≤ N
2. Set J=1
5. J = 1
3. Repeat steps 4 and 5 while J ≤ N 6. Repeat steps 7 to 10 while J ≤ N - I
4. PRINT LA[J] 7. IF LA[I] is > LA[J] THEN
8. Set TEMP = LA[I]; LA[I] = LA[J]; LA[J] = TEMP;
5. Set J = J +1
9. END IF
6. Stop 10. Set J = J+1
11. Set I = I+1
12. Stop
School of Computer Engineering
Basic Operation cont…
63
1. Start
/* Home Assignment1 */
2. Stop
LA is a linear array with N elements. Write the LA is a linear array with N elements. Write the
algorithm to finds the largest number and algorithm to copy the elements from LA to a
counts the occurrence of the largest number new array LB
1. Start 1. Start
/* Home Assignment 3 steps */ /* Home Assignment 4 steps*/
2. Stop 2. Stop
LA is a linear array with N elements. Write the LA is a linear sorted array with N elements.
algorithm to transpose the array Write the algorithm to insert ITEM to the array
1. Start 1. Start
/* Home Assignment 5 steps */ /* Home Assignment 6 steps */
2. Stop 2. Stop
Polynomial is an expression constructed from one or more variables and constants, using only the
operations of addition, subtraction, multiplication, and constant positive whole number exponents.
A term is made up of coefficient and exponent.
Examples –
Polynomial with single variable P(X) = 4X3 + 6X2+7X+9 (4,6,7 are coefficient & 3,2 are exponent)
Polynomial with 2 variables P(X, Y) = X3 - 2XY +1 (2 is coefficient & 3 is exponent)
Definition–
Polynomial with single variable P(X) =
A polynomial thus may be represented using arrays. Array representation assumes that the exponents of the
given expression are arranged from 0 to the highest value (degree), which is represented by the subscript of the
array beginning with 0. The coefficients of the respective exponent are placed at an appropriate index in the
array. Considering single variable polynomial expression, array representation is
Consider LA is a linear array with N elements and LB is a linear array with M elements.
Below is the algorithm for polynomial addition
1. Start
2. Set j= maximum of M or N
3. Create a sum array LSum[] of size J
4. IF (N is Greater Than or Equal to M) Then
Copy LA[] to LSum[]
else
Copy LB[] to LSum[]
5. IF (N is Greater Than M) then
Traverse array LB[] and LSum[i] = LSum[i] + LB[i]
else
Traverse array LA[] and LSum[i] = LSum[i] + LA[i] while i < j
6. PRINT LSum
7. Stop
Given two polynomials represented by two arrays, below is the illustration of the
multiplication of the given two polynomials.
Example :
Input: A[] = {5, 0, 10, 6} and B[] = {1, 2, 4}
Output: prod[] = {5, 10, 30, 26, 52, 24}
The first input array represents "5 + 0x1 + 10x2 + 6x3"
The second array represents "1 + 2x1 + 4x2"
And output is "5 + 10x1 + 30x2 + 26x3 + 52x4 + 24x5”
5 0 10 6 Coefficents 1 2 4 0 Coefficents
A B
0 1 2 3 Exponents 0 2 2 3 Exponents
AXB
5 10 30 26 52 24 Coefficents
Prod
0 2 2 3 4 5 Exponents
Algorithm:
prod[0.. m+n-1] Multiply (A[0..m-1], B[0..n-1])
1. Start
2. Create a product array prod[] of size m+n-1.
3. Initialize all entries in prod[] as 0.
4. Travers array A[] and do following for every element A[i]
Traverse array B[] and do following for every element B[j]
prod[i+j] = prod[i+j] + A[i] * B[j]
5. return prod[]
6. Stop
Home Assignment
Suppose A and B are two matrices and their order are respectively m x n and p x q. i, j and k are counters. And C to
store result.
Step 1: Start.
Step 2: Read: m, n, p and q
Step 3: Read: Inputs for Matrices A[1:m, 1:n] and B[1:p, 1:q].
Step 4: If n ≠ p then:
Print: Multiplication is not possible.
Else:
Repeat for i := 1 to m by 1:
Repeat for j := 1 to q by 1:
C[i, j] := 0 [Initializing]
Repeat k := 1 to n by 1
C[i, j] := C[i, j] + A[i, k] x B[k, j]
[End of for loop]
[End of for loop]
[End of for loop]
[End of If structure]
Step 5: Print: C[1:m, 1:q]
Step 6: Stop
In this representation, only non-zero values are considered along with their row and column index
values. In this representation, the 0th row stores total rows, total columns and total non-zero values
in the matrix. For example, consider a matrix of size 5 X 6 containing 6 number of non-zero values.
This matrix can be represented as shown in the image...
2 D Array Row Representation
2D array is used to represent a sparse matrix in which there are three
columns named as
• Rows: Index of row, where non-zero element is located
• Columns: Index of column, where non-zero element is located.
• Values: Value of the non zero element located at index – (row,
column)
In above example matrix, there are 6 non-zero elements ( those are 9, 8, 4, 2, 5 & 2) and matrix size is 5 X 6. We
represent this matrix as shown in the above image. The first row is filled with values 5, 6 & 6 which indicates that
it is a sparse matrix with 5 rows, 6 columns & 6 non-zero values. Second row is filled with 0, 4, & 9 which
indicates the value in the matrix at 0th row, 4th column is 9. In the same way the remaining non-zero values also
follows the similar pattern.
Rows 5 0 1 2 2 3 4
Columns 6 4 1 0 2 5 2
Values 6 9 8 4 2 5 2
2D array is used to represent a sparse matrix in which there are three rows named as
• Rows: Index of row, where non-zero element is located
• Columns: Index of column, where non-zero element is located.
• Values: Value of the non zero element located at index – (row, column)
In above example matrix, there are 6 non-zero elements ( those are 9, 8, 4, 2, 5 & 2) and matrix size
is 5 X 6. We represent this matrix as shown in the above image. The first column is filled with
values 5, 6 & 6 which indicates that it is a sparse matrix with 5 rows, 6 columns & 6 non-zero
values. Second column is filled with 0, 4, & 9 which indicates the value in the matrix at 0th row, 4th
column is 9. In the same way the remaining non-zero values also follows the similar pattern.
char str[4] = “MAM”; Address 8000 100 101 102 103 Address
char *ptr = str; Value Value
100 M A M \0
ptr
INT LENGTH(STR1)
1. SET LEN = 0 AND I = 0.
2. Repeat Steps 3 to 4 while STRING[I] IS NOT NULL
3. LEN = LEN + 1
4. SET I = I + 1
5. EXIT
Home Work
Check whether a string is binary string. A binary string is a special kind of
string made up of only two types of characters, such as 0 and 1. For example:
27. Write an algorithm that takes as input the size of the array and the elements
in the array and a particular index and prints the element at that index.
28. Write down the algorithm to sort elements by their frequency.
29. Write down the algorithm to add two polynomials of single variable.
30. Write down the algorithm to multiply two polynomials of two variables.
31. A program P reads in 500 random integers in the range [0..100] presenting
the scores of 500 students. It then prints the frequency of each score above
50. What would be the best way for P to store the frequencies?
32. Write down the algorithm to delete all the vowels in a character array.
33. Write down the algorithm to print all the elements below the minor diagonal
in a 2-D array.
34. Write an algorithm to find a triplet that sum to a given value
35. Given an array arr, write an algorithm to find the maximum j – i such that
arr[j] > arr[i]
36. Write an algorithm to replace every element in the array with the next
greatest element present in the same array.
School of Computer Engineering
Supplementary Reading
90
What is Array?
Array is a collection of variables of same data type that share a common
name.
Array is an ordered set which consist of fixed number of elements.
Array is an example of linier data structure.
In array memory is allocated sequentially so it is also known as
sequential list.
1-D Array address calculation
Address of A[i] = BA + w * (i) where BA is the base address, w is the word
length and i is the index
Row-Major order: Row-Major Order is a method of representing multi
dimension array in sequential memory. In this method elements of an array
are arranged sequentially row by row. Thus elements of first row occupies
first set of memory locations reserved for the array, elements of second row
occupies the next set of memory and so on.
Address of A [ i ][ j ] = BA + w * [ n * ( i – Lr ) + ( j – Lc ) ]
School of Computer Engineering
FAQ cont…
95
Ragged 2 D Arrays:
char *font[] = {
(char []) {65,8,12,1,0,0x18, 0x3C, 0x66, 0xC3, 0xC3, 0xC3, 0xC3, 0xFF, 0xFF},
(char []) {39,2,3,4,9,0xC0,0xC0, 0xC0},
(char []) {46,2,2,4,0,0xC0,0xC0}};