linear data structures using sequential organization...arrays initializing arrays ex-double...

73
Linear Data Structures using Sequential organization

Upload: others

Post on 24-Jun-2020

15 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Linear Data Structures using Sequential

organization

Page 2: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Classification s of Data Structures

Page 3: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Types of Data Structures

Page 4: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Arrays

Declaration of arrays

type arrayName [ arraySize ];

Ex-double balance[10];

Page 5: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Arrays

Initializing Arrays

Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write −

Ex-double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};

Page 6: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Arrays

Initializing Arrays

Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write −

Ex-double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};

Page 7: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Arrays

Initializing Arrays

You will create exactly the same array as you did in the previous example. Following is an example to assign a single element of the array −

Ex-balance[4] = 50.0;

Page 8: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Arrays

Shown below is the pictorial representation of the array:

Page 9: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Accessing Array Elements

double salary = balance[4];

Page 10: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Ex-Arrays#include <stdio.h> int main () { int a[10],i,size;

printf(“\nhow many no of elements u want to scan”);

scanf(“%d”,&size);

printf(“\nEnter the elements in the array”); for(i=0;i<size;i++)

{ scanf(“%d”,&a[i]);

} //end for

for(i=0;i<size;i++) {

printf(“The array is %d”,a[i]); //Displaying Array

}//end for

return 0;}

Page 11: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Output will be

12345

Page 12: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Multi-dimensional Arrays in C

type name[size1][size2]...[sizeN];

Page 13: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Two-dimensional Arrays in C

multidimensional array is the two-dimensional array

type arrayName [ x ][ y ];

Page 14: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Two-dimensional Arrays in C

Page 15: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Initializing Two-Dimensional Arrays

int a[3][4] = { {0, 1, 2, 3} , /* initializers for {4, 5, 6, 7} ,

{8, 9, 10, 11} /* initializers for row/* initializers for row indexed by 2 */ };

Page 16: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Accessing Two-Dimensional Array Elements

int val = a[2][3];

Page 17: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Three-dimensional Arrays in C

For example, the following declaration creates a three dimensional integer array −

Ex-int threedim[5][10][4];

Page 18: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

void myFunction(int param[10]) { . . . //Statement Excution }

Passing Arrays as Function Arguments in C

Page 19: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

ADT is useful tool for specifying the logical properties of a data type.

A data type is a collection of values & the set of operations on the values.

ADT refers to the mathematical concept that defines the data type.

ADT is not concerned with implementation but is useful in making use of data type.

Abstract Data Type

Page 20: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Arrays are stored in consecutive set of memory locations.

Array can be thought of as set of index and values.

For each index which is defined there is a value associated with that index.

There are two operations permitted on array data structure .retrieve and store

ADT for an array

Page 21: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

CREATE()-produces empty array. RETRIVE(array,index)->value Takes as input array and index and either returns appropriate value or an error. STORE(array,index,value)-array used to enter new index value pairs.

ADT for an array

Page 22: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Representation and analysisType variable_name[size]

Operations with arrays:CopyDeleteInsertSearchSortMerging of sorting arrays.

Introduction to arrays

Page 23: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

#include <stdio.h> int main() { int a[100],b[100] position, c, n;

printf("Enter number of elements in array\n");

scanf("%d", &n); printf("Enter %d elements\n", n);

for ( c = 0 ; c < n ; c++ ) scanf("%d", &a[c]);

printf("Enter %d elements\n", n);

for( c = 0 ; c < n - 1 ; c++ )

printf("%d\n", a[c]);

//Coping the element of array a to b

for( c = 0 ; c < n - 1 ; c++ )

{ b[c]=a[c]; }

} return 0; }

Copy operation

Page 24: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Output

Enter number of elements in array -4

Enter 4 elements 1 2 3 4

displaying array a 1 2 3 4 displaying array b 1 2 3 4

Page 25: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

#include <stdio.h> int main() { int array[100], position, c, n; printf("Enter number of elements in array\n");

scanf("%d", &n); printf("Enter %d elements\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); printf("Enter the location where you wish to delete element\n");

scanf("%d", &position); if ( position >= n+1 ) printf("Deletion not possible.\n"); else { for ( c = position - 1 ; c < n - 1 ; c++ ) array[c] = array[c+1]; printf("Resultant array is\n"); for( c = 0 ; c < n - 1 ; c++ ) printf("%d\n", array[c]); } return 0; }

Delete operation

Page 26: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Delete operation

Page 27: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

#include <stdio.h> int main(){ int array[100], position, c, n, value; printf("Enter number of elements in array\n"); scanf("%d", &n); printf("Enter %d elements\n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); printf("Enter the location where you wish to insert an element\n"); scanf("%d", &position); printf("Enter the value to insert\n"); scanf("%d", &value); for (c = n - 1; c >= position - 1; c--) array[c+1] = array[c]; array[position-1] = value; printf("Resultant array is\n"); for (c = 0; c <= n; c++) printf("%d\n", array[c]); return 0;}

Inserting an element

Page 28: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Inserting an element

Page 29: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Int a[10]={5,4,3,2,1}for(i=0;i<n;i++) for(j=i+1;j<n-i;j++) { if(a[i]>a[j]) { temp=a[i];

a[i]=a[j]; a[j]=temp; }

}

Sort an array

Page 30: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

a[6]={11,12,13,14,15,16}J=n-1;For(i=0;i<n/2;i++){ temp=a[i];

a[i]=a[j]; a[j]=temp; j--;}

Reverse array

Page 31: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Int a[10]={5,4,3,2,1}for(i=0;i<n;i++) for(j=i+1;j<n;j++) { if(a[i]>a[j]) { temp=a[i];

a[i]=a[j]; a[j]=temp; }

}

Sort element using array

Page 32: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

int main(){ int arr1[30], arr2[30], res[60];

int i, j, k, n1, n2;  printf("\nEnter no of elements in 1st array :");

scanf("%d", &n1);

for (i = 0; i < n1; i++) {scanf("%d", &arr1[i]);}  printf("\nEnter no of elements in 2nd array :");

scanf("%d", &n2);

for (i = 0; i < n2; i++) {scanf("%d", &arr2[i]);}   i = 0; j = 0;k = 0;  

Merging of two arrays

Page 33: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

// Merging startswhile (i < n1 && j < n2) { if (arr1[i] <= arr2[j]) { res[k] = arr1[i]; i++; k++; } else { res[k] = arr2[j]; k++; j++; }}  

Merging of two arrays

Page 34: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

/* Some elements in array 'arr1' are still remaining where as the array 'arr2' is exhausted */ while (i < n1) { res[k] = arr1[i]; i++; k++; } 

Merging of two arrays

Page 35: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

/* Some elements in array 'arr2' are still remaining where as the array 'arr1' is exhausted */ while (j < n2) { res[k] = arr2[j]; k++; j++; }

Merging of two arrays

Page 36: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Merging of two arrays 

//Displaying elements of array 'res'printf("\nMerged array is :");for (i = 0; i < n1 + n2; i++)printf("%d ", res[i]); return (0); }

Page 37: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

/* Some elements in array 'arr2' are still remaining where as the array 'arr2' is exhausted */

while (i < n2)

{res[k] = arr2[i];i++;k++;

}

for(i=0;i<k;i++) { printf(“%d”,res[i]); }

Merging of two arrays

Page 38: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Output:

Enter no of elements in 1st array : 411 22 33 44 Enter no of elements in 2nd array : 310 40 80 Merged array is : 10 11 22 33 40 44 80

Merging of two arrays

Page 39: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Merging of two arrays

Page 40: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Two-dimensional Arrays in C

multidimensional array is the two-dimensional array

type arrayName [ x ][ y ];

Page 41: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Two-dimensional Arrays in C

Page 42: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

m-no of rows n-no of columns Printf(“\n Enter the rows and columns”); Scanf(%d %d”,&m,&n); for(i=0;i<m;i++) {

for(j=0;j<n;j++){

Printf(“\n Enter the value of(%d)(%d)=“,i,j); Scanf(“%d”,&a[i][j]);

}

Page 43: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

For(i=0;i<m;i++) {

Printf(“\n”); For(j=0;j<n;j++) {

printf(“%d”,&a[i][j]); }

}

Page 44: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

int main () { /* an array with 5 rows and 2 columns*/ int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}}; int i, j; /* output each array element's value */ for ( i = 0; i < 5; i++ ) { for ( j = 0; j < 2; j++ ) { printf("a[%d][%d] = %d\n", i,j, a[i][j] ); } } return 0; }

Page 45: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Address Calculation in single (one) Dimension Array:

Page 46: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Address Calculation

Page 47: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Address Calculation

Array of an element of an array say “A[ I ]” is calculated using the following formula:

Address of A [ I ] = B + W * ( I – LB ) Where, B = Base address 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)

Page 48: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Address Calculation Ex-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: B = 1020, LB = 1300, W = 2, I = 1700

Address of A [ I ] = B + W * ( I – LB ) = 1020 + 2 * (1700 – 1300) = 1020 + 2 * 400 = 1020 + 800 = 1820 [Ans]

Page 49: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Address Calculation in Double (Two) Dimensional

Array: While storing the elements of a 2-D array in memory, these are allocated contiguous memory locations. Therefore, a 2-D array must be liberalized so as to enable their storage. There are two alternatives to achieve linearization: Row-Major and Column-Major.

Page 50: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Address Calculation in Double (Two) Dimensional

Array:

Page 51: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Address Calculation in Double (Two) Dimensional

Array:

Page 52: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Address Calculation in Double (Two) Dimensional

Array:

Address of an element of any array say “A[ I ][ J ]” is calculated in two forms as given:(1) Row Major System (2) Column Major System

Page 53: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Address Calculation in Double (Two) Dimensional

Array:

The address of a location in Row Major System is calculated using the following formula:

Address of A [ I ][ J ] = B + W * [ N * ( I – Lr ) + ( J – Lc )

B = Base addressI = Row subscript of element whose address is to be foundJ = Column subscript of element whose address is to be foundW = 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 matrixN = Number of column of the given matrix

Page 54: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Address Calculation in Double (Two) Dimensional

Array:

Address of A [ I ][ J ] Column Major Wise = B + W * [( I – Lr ) + M * ( J – Lc )]

Column Major System:The address of a location in Column Major System is calculated using the following formula:

B = Base addressI = Row subscript of element whose address is to be foundJ = Column subscript of element whose address is to be foundW = 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 matrixN = Number of column of the given matrix

Page 55: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Address Calculation in Double (Two) Dimensional

Array:

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:Number of rows (M) will be calculated as = (Ur – Lr) + 1Number of columns (N) will be calculated as = (Uc – Lc) + 1And rest of the process will remain same as per requirement (Row Major Wise or Column Major Wise).

Page 56: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Address Calculation in Double (Two) Dimensional

Array:

Examples:Q 1. An array X [-15……….10, 15……………40] requires one byte of storage. If beginning location is 1500 determine the location of X [15][20].

Solution:As you see here the number of rows and columns are not given in the question. So they are calculated as:Number or rows say M = (Ur – Lr) + 1 = [10 – (- 15)] +1 = 26Number or columns say N = (Uc – Lc) + 1 = [40 – 15)] +1 = 26

Page 57: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Address Calculation in Double (Two) Dimensional

Array:

(i) Column Major Wise Calculation of above equationThe given values are: B = 1500, W = 1 byte, I = 15, J = 20, Lr = -15, Lc = 15, M = 26Address of A [ I ][ J ] =B + W * [ ( I – Lr ) + M * ( J – Lc ) ]= 1500 + 1 * [(15 – (-15)) + 26 * (20 – 15)] = 1500 + 1 * [30 + 26 * 5] = 1500 + 1 * [160] = 1660 [Ans]

Page 58: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

= 1500 + 1* [26 * (15 – (-15))) + (20 – 15)] = 1500 + 1 * [26 * 30 + 5] = 1500 + 1 * [780 + 5] = 1500 + 785 = 2285 [Ans]

(ii) Row Major Wise Calculation of above equationThe given values are: B = 1500, W = 1 byte, I = 15, J = 20, Lr = -15, Lc = 15, N = 26Address of A [ I ][ J ] = B + W * [ N * ( I – Lr ) + ( J – Lc ) ]

Page 59: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Addition of Two 2-D Matrices:

If we add two matrices am*n & bm*n producing Cm*n for(i=0;i<m;i++) //for row { for(j=0;j<n;j++) //for column { c[i][j]=a[i][j]+b[i][j]; } }

Page 60: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Upper Triangular Matrix   A triangular matrix  of the form

Written explicitly,

Page 61: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Example #include<stdio.h> #include<conio.h> void main() { int a[3][3],i,j; clrscr(); printf("Enter the elements of 3x3 matrix:\n");

for(i=0;i<3;i++) {

for(j=0;j<3;j++) { scanf("%d",&a[i][j]); } } printf("\nThe entered matrix is:\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("%d\t",a[i][j]); } printf("\n"); } printf("\nUpper triangular matrix is:\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { if(i<=j) { printf("%d\t",a[i][j]); } else { printf("%d\t",0); } } printf("\n"); } getch(); }

Page 62: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Strictly Upper Triangular Matrix A strictly upper triangular matrix is an 

upper triangular matrix having 0s along the diagonal as well as the lower portion, i.e., a matrix  such that  for . Written explicitly,

Page 63: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Lower Triangular Matrix A triangular matrix  L of the form

Written explicitly,

Page 64: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Strictly Lower Triangular Matrix A lower triangular matrix having 0s along the 

diagonal as well as the upper portion, i.e., a matrix  such that  for . Written explicitly,

Page 65: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Assignment on

Strictly Upper Triangular Matrix Strictly Lower Triangular Matrix

Page 66: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

For example:Suppose two matrixes A and B of size of 2 x 2 and 2 x 3 respectively:

MULTIPLICATION OF TWO MATRICES

Page 67: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array
Page 68: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Transpose of a Matrix

A matrix which is formed by turning all the rows of a given matrix into columns and vice-versa. The transpose of matrix A is written AT.

Page 69: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Concept of Ordered List

It is a set of elements .such a list sometimes called as linear list.

Definition-EX-1.List of one one digit numbers. (0,1,2,3,4,5,6,7,8,9)Ex-2 Days in a week.(Sunday, Monday Tuesday, Wednesday, Thursday,Friday,Saturday)

Page 70: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Operations on ordered list

Create an ordered list Dispay of list Searching particular element from the list Insertion of any element in the list Deletion of any element from the list

Page 71: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Example of ordered list

Polynomial Operations

Page 72: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

While scanning the string doesn’t required

scanf(“%s”,str1) ; scanf needs the address of the variable to read into, and string buffers are already represented as addresses (pointer to a location in memory, or an array that decomposes into a pointer).

printf does the same, treating %s as a pointer-to-string.

Page 73: Linear Data Structures using Sequential organization...Arrays Initializing Arrays Ex-double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; If you omit the size of the array, an array

Scanning string

scanf needs the address of the variable to read into, and string buffers are already represented as addresses (pointer to a location in memory, or an array that decomposes into a pointer).

printf does the same, treating %s as a pointer-to-string.