Legendary Arrays
Legendary Arrays
Jyoti Srivastava
data_type array_name[array_size];
Examples:
oint a[20], b[3],c[7];
ofloat f[5], c[2];
ochar m[4], n[20];
oint marks[5];
Initialization
Initialization of an array is the process of assigning initial values.
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
Example
Write a program to calculate and print the average of the following
array of integers.
( 4, 3, 7, -1, 7, 2, 0, 4, 2, 13)
#include<stdio.h>
#define size 10
int main()
{
int x[10]={4,3,7,-1,7,2,0,4,2,13}, i, sum=0;
float av;
for(i=0,i<=size-1;i++)
sum = sum + x[i];
av = (float)sum/size;
printf(“The average of the numbers=%.2f\n”, av);
return 0;
}
Passing Arrays to Functions
To pass an array argument to a function, specify the name of the array
without any brackets
functionname(arrayname);//passing array
Example:
int myArray[ 24 ];
myFunction( myArray);
Arrays passed call-by-reference
Name of array is address of first element
Function knows where the array is stored
Modifies original memory locations
Passing Arrays to Functions
• Function prototype
void modifyArray(int b[], int arraySize);
1. Traversal
2. Insertion
3. Deletion
4. Searching
5. Update
6. Merging
Traversal
STEP4: Set I = I + 1
[End of Loop]
STEP5: Exit
Example
45 23 34 12 56 20 20
45 23 34 12 56 56 20
45 23 34 12 12 56 20
45 23 34 100 12 56 20
45 23 34 12 56 20
• If the element has to be deleted from the end of the array, then quite
simple. Subtract 1 from upper_bound.
45 23 12 12 56 20
45 23 12 56 56 20
45 23 12 56 20 20
45 23 12 56 20
DELETE(Data, 5, 2)
45 23 34 12 56 20
Example Algorithm:
1. Start
2. Set LA[K] = ITEM
3. Stop
Merging
• Merging two arrays in a third array means first copying
contents of first array into the third array and then copying the
contents of the second array into third array.
Array -1 90 56 89 77 69
Array -2 45 88 99 58 81
Array -3 90 56 89 77 69 45 88 99 58 81
Array -1 20 30 40 50 60
Array -2 15 22 31 45 56
Array -3 15 20 22 30 31 40 45 50 56 60
Pointer of Array
int *ptr[10];
int p = 1, q = 2, r = 3;
ptr[0] = &p;
ptr[1] = &q;
ptr[2] = &r;
Two Dimensional Array
• The two dimensional array is represented in the form of rows and
columns, also known as matrix.
• data_type array_name[row_size][column_size];
• Initialization
2. Using loop
Two-Dimensional Array Initialization
Address(a[i][j]) = B. A. + (i * n + j) * size
Address(a[i][j]) = ((j*m)+i)*Size + BA
• Sum
• Difference
• Product
Sparse matrix
• Sparse matrix is a matrix which has many elements
with a value zero.
•Example:
00304
00570
00000
02600
Why to use Sparse Matrix instead of simple matrix ?
1
53
271
3142
92817
12345
6789
127
23
1
Sparse Matrix Representations
• 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.
•Array representation
•Linked list representation
Array representation
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)
Linked list representation
• In linked list, each node has four fields. These four fields are
defined 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)
• Next node: Address of the next node
Linked list representation
Structures in C
• One of the unique features of the C language.
•Example:
struct student A[90];