DSA Initial
DSA Initial
• Data Structures – These are like the ingredients you need to build efficient
algorithms. These are the ways to arrange data so that they (data items) can
be used efficiently in the main memory.
• Examples: Array, Stack, Linked List, and many more. You don't need to worry about
these names. These topics will be covered in detail in the upcoming tutorials.
• Big data – Analysis of too large or complex data, which cannot be dealt with
the traditional data processing applications.
Memory Layout of C Programs:
• When the program starts, its code gets
copied to the main memory.
• The stack holds the memory occupied by
functions. It stores the activation records
of the functions used in the program. And
erases them as they get executed.
• The heap contains the data which is
requested by the program as dynamic
memory using pointers.
• Initialized and uninitialized data segments
hold initialized and uninitialized global
variables, respectively.
What is Data Structure & Algorithm:
The int, char, float, double, and pointer are the primitive data
structures that can hold a single value.
Types of Data Structures:
Non-Primitive Data structure
The non-primitive data structure is divided into two types:
Data can be organized in a data structure in such a way that all items may not
be required to be searched, and the required data can be searched almost
instantly.
Data structure Fundamentals:
Data Structure is a systematic way to organize data in order to
use it efficiently. Following terms are the foundation terms of a data
structure.
Average Case − This is the scenario depicting the average execution time
of an operation of a data structure. If an operation takes ƒ(n) time in
execution, then m operations will take mƒ(n) time.
Best Case − This is the scenario depicting the least possible execution
time of an operation of a data structure. If an operation takes ƒ(n) time in
execution, then the actual operation may take time as the random number
which would be maximum as ƒ(n).
Data Definition:
Data Definition defines a particular data with the following
characteristics.
• Atomic − Definition should define a single concept.
int main(void)
{
int studMark1, studMark2, studMark3,
studMark4, …, …, studMark998, stuMark999,
studMark1000;
…
…
return 0;
}
ARRAYS
By using an array, we just declare like this,
int studMark[1000];
This will reserve 1000 contiguous memory locations for storing the
students’ marks.
Graphically, this can be depicted as in the following figure.
ARRAYS
This absolutely has simplified our declaration of the
variables.
We can use index or subscript to identify each
element or location in the memory.
Hence, if we have an index of jIndex,
studMark[jIndex] would refer to the jIndexth
element in the array of studMark.
For example, studMark[0] will refer to the first
element of the array.
Thus by changing the value of jIndex, we could refer
to any element in the array.
So, array has simplified our declaration and of course,
manipulation of the data.
ARRAYS
One Dimensional Array: Declaration
The first example declares two arrays named xNum and yNum of type int. Array
xNum can store up to 20 integer numbers while yNum can store up to 50
numbers.
The second line declares the array fPrice of type float. It can store up to 10
floating-point values.
fYield is basic variable which shows array type can be declared together with
basic type provided the type is similar.
The third line declares the array chLetter of type char. It can store a string up
to 69 characters.
Why 69 instead of 70? Remember, a string has a null terminating character (\0)
at the end, so we must reserve for it.
ARRAYS
Array Initialization
For example, the array chVowel in the previous example could have been written
more compactly as follows,
When the value assigned to a character array is a string (which must be enclosed in
double quotes), the compiler automatically supplies the NULL character but we still
have to reserve one extra place for the NULL.
For unsized array (variable sized), we can declare as follow,
C compiler automatically creates an array which is big enough to hold all the
initializer.
Q: Largest Element in an array
Q: Largest Element in an array
ARRAYS
Arrays allow programmers to group related items of the same
data type in one variable.
However, when referring to an array, one has to specify not only
the array or variable name but also the index number of interest.
Program example 1: Sum of array’s element.
Notice the array's element which is not initialized is set to 0
automatically.
ARRAYS
Program example 2: Searching the smallest value.
Finding the smallest element in the array named fSmallest.
First, it assumes that the smallest value is in fSmallest[0] and
assigns it to the variable nSmall.
Then it compares nSmall with the rest of the values in fSmallest,
one at a time.
When an element is smaller than the current value contained in
nSmall, it is assigned to nSmall. The process finally places the
smallest array element in nSmall.
ARRAYS
Program example 3: Searching the biggest
value. By modifying the previous example we
can search the biggest value.
ARRAYS
Two Dimensional/2D Arrays
For examples,
int xInteger[3][4];
float matrixNum[20][25];
The first line declares xInteger as an integer array with 3 rows and
4 columns.
Second line declares a matrixNum as a floating-point array with 20
rows and 25 columns.
ARRAYS
If we assign initial string values for the 2D array it will look
something like the following,
array_name[x][y];
ThreeDimArray[2][4][7] = 2 x 4 x 7 = 56.
And if you want to illustrate the 3D array, it could be a cube with wide, long
and height dimensions.
Matrix Multiplication
Matrix Multiplication
Matrix Multiplication
Matrix Multiplication
Matrix Multiplication
Matrix Multiplication
Algorithm of Matrix Multiplication
1. Start.
2. Enter the value of m and n (or) order of the first matrix.
3. Enter the value of p and q (or) order of the second matrix.
4. Create a matrix of size a[m][n] and b[p][q].
5. Enter the element of matrices row wise using loops.
6. If a number of columns of first matrix is not equal to the number of rows of second matrix,
print matrix multiplication is not possible and exit. If not, proceed to next step.
7. Create a third matrix, c of size m x q to store the product.
8. Set a loop from i=0 to i=m.
9. Set an inner loop for the above loop from j=0 to j=q.
10. Initialize the value of element (i, j) of new matrix to 0.
11. Set an inner loop inside the above loop from k=0 to k=p.
12. Using the add and assign operator (+=) store the value of a[i][k] * b[k][j] in the third matrix,
c[i][j].
13. Print the third matrix.
14. Stop.
Flow chart of Matrix Multiplication
End-of-C-arrays