2_Array
2_Array
• Need of Array
• Introduction
• One-Dimensional Array
• Two-Dimensional Array
• Sorting Technique
– Bubble Sort
• Multi-Dimensional Array
• Sparse Matrix
• Question Answer & Discussion
Can you imagine how long we have to write the declaration part by using normal
variable declaration?
int main(void)
{
int studMark1, studMark2, studMark3,
studMark4, …, …, studMark998,
stuMark999, studMark1000;
…
…
return 0;
}
int arr[5];
C.V.Raman College of Engineering
One-Dimensional Array
int a[5]={1,2,3,4,5}
index element
Ex:
int marks[]={99,67,78,56,88,90,34,85}
Calculate the address of mark[5] if the base
address=1000.
Ans:
marks[5]=1000+2*(5-0)=1010
printf(“%d”,arr[i]); 20
}
30
}
40
50
a[0] 10
a[1] 20
a[2] 30
a[3] 70
a[4] 25
a[5]
a[6]
a[0] 10
Insert 45
a[1] 20
Position
a[2] 30
a[3] 70
a[4] 25
a[5]
a[6]
a[0] 10
Insert 45
a[1] 20
Position
a[2] 30
a[3] 70
a[4] 25
a[5]
a[6]
a[0] 10
Insert 45
a[1] 20
Position
a[2] 30
a[3] 70
a[4]
a[5] 25
a[6]
a[0] 10
Insert 45
a[1] 20
Position
a[2] 30
a[3]
a[4] 70
a[5] 25
a[6]
a[0] 10
Insert 45
a[1] 20
a[2] 30
a[3]
a[4] 70
a[5] 25
a[6]
a[0] 10
Insert
a[1] 20
a[2] 30
a[3] 45
a[4] 70
a[5] 25
a[6]
a[0] 20
53
a[1] 14
40
a[2] 50
10
a[3] 70
8
a[4] 25
a[5] 65
35
a[6]
a[0] 20
53
Insert
Delete
a[1] 14
40
a[2] 50
10 Position
a[3] 70
8
a[4] 25
a[5] 65
35
a[6]
a[0] 20
53
Insert
Delete
a[1] 14
40
a[2] 50
10 Position
a[3] 70
8
a[4] 25
a[5] 65
35
a[6]
a[0] 20
53
Insert
Delete
a[1] 14
40
a[2] 50
70 Position
a[3] 8
a[4] 25
a[5] 65
35
a[6]
a[0] 20
53
Insert
Delete
a[1] 14
40
a[2] 50
70 Position
a[3] 25
8
a[4] 25
a[5] 65
35
a[6]
a[0] 20
53
Insert
Delete
a[1] 14
40
a[2] 50
70 Position
a[3] 25
8
a[4] 25
35
a[5] 65
a[6]
a[0] 20
53
a[1] 14
40
a[2] 50
70
a[3] 25
8
a[4] 25
35
a[5] 65
a[6]
Bubble Sort
Insertion Sort
Selection Sort
Quick Sort
Merge Sort
Radix Sort
Heap sort
EXAMPLE
PASS 1 11 15 2 13 6
PASS 2 11 2 13 6 15
PASS 3 2 11 6 13 15
PASS 4 2 6 13 11 15
RESULT 2 6 11 13 15
ASSIGNMENTS
1.Program to take an integer array and calculate the sum and average of all elements.
2.Program to input and array and check how many even and odd numbers are
present in the array.
4.Program to take input for an array and calculate the number of prime elements
present in an array.
int MyArray[][] = {
{0,0,0},
{1,1,1},
{2,2,2}
}
Initialization:
for(i=0;i<row;i++)
for(j=0;j<col;j++)
matrix[i][j]=10;
2
• Row-major Order Representation: a[0][0]
Row 0 a[0][1] 3
– All the matrix elements 1
a[0][2]
are stored row-by-row basis. a[1][0] 15
Row 1 a[1[1] 25
a[1][2] 13
a[2][0] 20
Row 2 a[2[1] 4
a[2][2] 7
a[3][0] 21
Row 3 a[3][1] 18
a[3][2] 14
C.V.Raman College of Engineering
Memory Representation of Matrix
2
• Column-major Order Representation: a[0][0]
a[1][0] 15
– All the matrix elements Col 0
a[2][0] 20
are stored column-by-column basis. a[3][0] 21
a[0][1] 3
a[1][1] 25
Col 1
a[2][1] 4
a[3][1] 18
a[0][2] 1
a[1][2] 13
Col 2
a[2][2] 7
a[3][2] 14
C.V.Raman College of Engineering
Address Calculation of Matrix Elements in
Memory
• Row-major Order Representation:
A[r][c] is a matrix
where r=Row size(ranges from 0 to r-
1)
c=Column size(ranges from 0
to c-1)
B=base address of the array A
Address(A[i][j])=B+(i*c + j) *w
where w=size of each element
2.Program for the addition of two matrices of order m*n and display the resultant
matrix.
3.Program for the multiplication of two matrices of order m*n and n*p and display
the resultant matrix.
Int MyArray[2][3][4];
row4 91 0 0 0 0 0
5*3 row5 0 0 28 0 0 0
6*6
15/15
8/36
5 3
11 7 9
2 12 10 1
3 5 10
9 7
11 3 5
10 9 7
2 1
a[0] 6 6 8
[1] 0 0 15
[2] 0 3 22
[3] 0 5 -15
[4] 1 1 11
[5] 2 5 28
[6] 2 3 -6
[7] 4 0 91
[8] 5 2 28
a. Floor Address
b. Foundation Address
c. First Address
d. Base Address
a. Linear Arrays
b. Linked Lists
c. Both
d. None of these
a. Linear Arrays
a. Arrays
a. Strings
b. Lists
c. Queues
d. All of above
a) Arrays
b) Linked lists
c) Both of the above
d) None of the above
a) Linear
b) Non linear
c) Linked list
d) Trees
a)Linear