Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 39

Module II

Array
One Dimensional Array
One Dimensional Character Array
Two Dimensional Array
Two Dimensional Character Array
Two Dimensional Character Array
Two Dimensional Character Array
#include <stdio.h>
int main()
{
char Name[6][10] = {"Mr. Bean","Mr. Bush","Nicole","Kidman","Arnold","Jodie"};
int Count = 0 ,i, j;
for(i = 0; i <= 5; i = i + 1)
{
for(j = 0; Name[i][j] != '\0'; j = j + 1)
{
if(Name[i][j] == 'a' || Name[i][j] == 'A')
Count = Count + 1;
printf("%c", Name[i][j]);
}
printf("\n");
}
printf("The number of a's is %d.\n\n", Count);
return 0;
}
Row Major – Address Calculation

• Row major order is a method of representing multi


dimensional array in sequential memory.
• In this method elements of an array are arranged sequentially
row by row.
Column Major – Address Calculation

• Column major order is a method of representing multi


dimensional array in sequential memory.
• In this method elements of an array are arranged sequentially
column by column.
Memory Management
Memory Management – Sequential Order
Address Calculation Formula – Row Major
• 3 x 4 integer array with the base address 1000
Address Calculation Formula – Row Major
• 3 x 4 integer array with the base address 1000
Address Calculation Formula – Column
Major
• 3 x 4 integer array with the base address 1000
Address Calculation Formula – Column
Major
• 3 x 4 integer array with the base address 1000
Operations on Arrays
Insertion: This refers to inserting an element in the array at a
particular index. This can be performed with O(n)
complexity.
Deletion: This refers to deleting an item at a particular index.
This operation requires shifting of elements after deletion
thus takes O(n) complexity.
Searching: This refers to accessing an item at a particular
index of an array.
Traversing: It refers to printing all the elements of an array
one after another.
Vectors
• Vectors are the same as dynamic arrays with the
ability to resize itself automatically when an
element is inserted or deleted, with their storage
being handled automatically by the container.

• Vector elements are placed in contiguous storage


so that they can be accessed and traversed using
iterators.
Arrays - Revisited
• Array is a linear data structure that is a collection of
similar data types.

• Arrays are stored in contiguous memory locations.

• It is a static data structure with a fixed size.

• It combines data of similar types.


Arrays – Simple Applications
•Arrays are used to implement data structures like a stack, queue, etc.
•Arrays are used for matrices and other mathematical implementations.
•Arrays are used in lookup tables in computers.
•Arrays can be used for CPU scheduling.
• Arrays are commonly used to store large amounts of data.
•Arrays are used in the implementation of various graph and tree data
structures.
•Arrays can be used to dynamically allocate memory for storing data.
•Arrays are used in computer graphics to store and manipulate images,
pixel data, and other visual elements.
Arrays – Real Time Applications
• Contact lists on mobile phones.
• Matrices use arrays which are used in different fields like image
processing, computer graphics, and many more.
• Arrays are used in online ticket booking portals.
• Pages of book.
• IoT applications use arrays as we know that the number of values in
an array will remain constant, and also that the accessing will be
faster.
• It is also utilised in speech processing, where each speech signal is
represented by an array.
• The viewing screen of any desktop/laptop is also a multidimensional
array of pixels.
Arrays – In C
• Arrays are used to implement vectors, and lists in C++ STL.
• Arrays are used as the base of all sorting algorithms.
• Arrays are used to implement other DS like a stack, queue, etc.
• Used for implementing matrices. 
• Data structures like trees also sometimes use the array
implementation since arrays are easier to handle than pointers. For
example, a segment tree uses array implementation.
• Binary search trees and balanced binary trees are used in data
structures such as a heap, map, and set, which can be built using
arrays.
• Graphs are also implemented as arrays in the form of an adjacency
matrix.
Arrays – In C
• Arrays are used to implement vectors, and lists in C++ STL.
• Arrays are used as the base of all sorting algorithms.
• Arrays are used to implement other DS like a stack, queue, etc.
• Used for implementing matrices. 
• Data structures like trees also sometimes use the array
implementation since arrays are easier to handle than pointers. For
example, a segment tree uses array implementation.
• Binary search trees and balanced binary trees are used in data
structures such as a heap, map, and set, which can be built using
arrays.
• Graphs are also implemented as arrays in the form of an adjacency
matrix.
Structure vs Arrays
• The structure can store different types of data whereas an array can
only store similar data types.
• Structure does not have limited size like an array.
• Structure elements may or may not be stored in contiguous locations
but array elements are stored in contiguous locations.
• In structures, object instantiation is possible whereas in arrays objects
are not possible.
Matrix Multiplication
• In matrix multiplication first matrix one row element is multiplied by second
matrix all column elements.
• Let's try to understand the matrix multiplication of 2*2 and 3*3 matrices by the
figure given below:
Matrix Multiplication – C Program
#include<stdio.h>    
#include<stdlib.h>  
int main()
{  
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;    

printf("enter the number of row=");    
scanf("%d",&r);    

printf("enter the number of column=");    
scanf("%d",&c);
Matrix Multiplication – C Program
printf("enter the first matrix element=\n");    
for(i=0;i<r;i++)    
{    
for(j=0;j<c;j++)    
{    
scanf("%d",&a[i][j]);    
}    
}    
printf("enter the second matrix element=\n");    
for(i=0;i<r;i++)    
{    
for(j=0;j<c;j++)    
{    
scanf("%d",&b[i][j]);    
}    
}    
Matrix Multiplication – C Program
printf("multiply of the matrix=\n");    
for(i=0;i<r;i++)    
{    
for(j=0;j<c;j++)    
{    
mul[i][j]=0;    
for(k=0;k<c;k++)    
{    
mul[i][j]+=a[i][k]*b[k][j];    
}    
}    
}    
Matrix Multiplication – C Program
//for printing result    
for(i=0;i<r;i++)    
{    
for(j=0;j<c;j++)    
{    
printf("%d\t",mul[i][j]);    
}    
printf("\n");    
}    
return 0;  
}  
Matrix Multiplication – C Program
Sparse Matrix
• A matrix is a two-dimensional data object made of m rows and n columns,
therefore having total m x n values.
• If most of the elements of the matrix have 0 value, then it is called a sparse matrix.
Sparse Matrix
Why to use Sparse Matrix instead of simple matrix ?

• Storage: There are lesser non-zero elements than zeros and thus lesser memory
can be used to store only those elements.

• Computing time: Computing time can be saved by logically designing a data


structure traversing only non-zero elements.

• Representing a sparse matrix by a 2D array leads to wastage of lots of memory as


zeroes in the matrix are of no use in most of the cases.
• So, instead of storing zeroes with non-zero elements, we only store non-zero
elements.
• This means storing non-zero elements with triples- (Row, Column, value). 
Sparse Matrix – Using Arrays
2D array is used to represent a sparse matrix in which there are three rows named as 

• Row: Index of row, where non-zero element is located

• Column: Index of column, where non-zero element is located

• Value: Value of the non zero element located at index – (row,column)


Sparse Matrix – Using Arrays
Sparse Matrix – C Implementation
# include <stdio.h>
int main()
{
    // Assume 4x5 sparse matrix
    int sparseMatrix[4][5] =
    {
        {0 , 0 , 3 , 0 , 4 },
        {0 , 0 , 5 , 7 , 0 },
        {0 , 0 , 0 , 0 , 0 },
        {0 , 2 , 6 , 0 , 0 }
    };
 
    int size = 0;
   
Sparse Matrix – C Implementation
    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 5; j++)
            if (sparseMatrix[i][j] != 0)
                size++;
 
 // number of columns in compactMatrix (size) must be
   

    // equal to number of non - zero elements in


    // sparseMatrix
    int compactMatrix[3][size];
 
   
Polynomial
A polynomial is composed of different terms where each of them holds a coefficient
and an exponent.

A polynomial p(x) is the expression in variable x which is in the form (ax n + bxn-1 +
…. + jx+ k), where a, b, c …., k fall in the category of real numbers and 'n' is non
negative integer, which is called the degree of polynomial.

An essential characteristic of the polynomial is that each term in the polynomial


expression consists of two parts:

• one is the coefficient


• other is the exponent
Polynomial
Example:

10x2 + 26x, here 10 and 26 are coefficients and 2, 1 is its exponential value.

Points to keep in Mind while working with Polynomials:

• The sign of each coefficient and exponent is stored within the coefficient and the
exponent itself
• Additional terms having equal exponent is possible one
• The storage allocation for each term in the polynomial must be done in ascending
and descending order of their exponent
Polynomial

Polynomial can be represented in the various ways. These are:


• By the use of arrays
• By the use of Linked List
Polynomial - Addition

You might also like