PPCm3c12
PPCm3c12
Chapter 12 Arrays
12.1 to 12.12
Introduction
• An array is a collection of similar data
elements of the same data type.
• Stored in consecutive memory locations and
are referenced by an index(subscript).
• Subscript- ordinal number used to identify an
element of array.
12.2 Declaration of Arrays
• Like how we declare every variable we use, we
have to declare array variables too.
• Syntax:
type name[size];
• Example:
int marks[10];
Applications of Arrays
• #include<stdio.h>
#define N 100
main()
{
int arr[N];
..
12.3 Accessing the elements of an array
Array size: 10
Elements : 3 4 7 6 5 1 2 8 10 9
Ascending : 1 2 3 4 5 6 7 8 9 10
Descending : 10 9 8 7 6 5 4 3 2 1
12.5.6 Sorting an array in ascending or
descending order
C program to sort an array in an ascending order:
#include <stdio.h>
#include<conio.h>
int main()
{
int a[100],n,i,j;
printf("Array size: ");
scanf("%d",&n);
printf("Elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for (int i = 0; i < n; i++) //Loop for ascending ordering
{
for (int j = 0; j < n; j++) //Loop for comparing other values
{
if (a[j] > a[i]) //Comparing other array elements
{
int tmp = a[i]; //Using temporary variable for storing last value
a[i] = a[j]; //replacing value
a[j] = tmp; //storing last value
}
}
}
printf("\n\nAscending : ");
for (int i = 0; i < n; i++)
{
printf(" %d ", a[i]);
}
for (int i = 0; i < n; i++) //Loop for descending ordering
{
for (int j = 0; j < n; j++) //Loop for comparing other values
{
if (a[j] < a[i]) //Comparing other array elements
{
int tmp = a[i]; //Using temporary variable for storing last value
a[i] = a[j]; //replacing value
a[j] = tmp; //storing last value
}
}
}
printf("\n\nDescending : "); //Printing message
for (int i = 0; i < n; i++) //Loop for printing
array data after sorting
{
printf(" %d ", a[i]); //Printing data
}
&
Passing entire array
12.7 Two dimensional arrays
12.7.1Declaring two dimensional arrays
• Syntax:
• data_type array_name[row_size][col_size];
• m(rows)xn(col)array accessed using i and
j,i<=m and j<=n
• Marks of 3 students in 5 subjects..?
• marks[0][0]-1st element
• marks[1][0]-2nd element
Collection of 1D arrays
• Two ways of storing a 2D array in computer
memory:
– Row major order
– Column major order
Address
• Column major order
– Address(A[I][J])=B_A+w{M(J-1)+(I-1)}
• Row major order
• Address(A[I][J])=B_A+w{N(I-1)+(J-1)}
12.7.2 Initializing 2D arrays
• int marks[2][3]={10,20,30,40,50,60};
or
• int marks[2][3]={{10,20,30},{40,50,60}};
• marks[0][0]=10 marks[0][1]=10
• marks[0][2]=30 marks[1][0]=40
• marks[1][0]=50 marks[1][0]=60
Omitting size
• int marks[][3]={{10,20,30},{40,50,60}};
• int marks[2][3]={0};
• int marks[2][3]={{40,50,60}};
• marks[1][2]=79;
12.7.3 Accessing the elements of two
dimensional arrays
• Subscript
• for loop
• 1D array
• 2D array
12.8 Operations on two dimensional arrays
• Transpose
Bi,j=Aj,i
• Sum
Ci,j=Ai,j + Bi,j
• Difference
Ci,j=Ai,j - Bi,j
• Product
Ci,j=ΣAi,k Bk,j for k=1 ,k<n
12.9 Passing 2D arrays to functions
• Passing individual elements/ passing elements
of 1D array
• Passing a row/ passing entire 1D array
• Passing an entire 2D array
12.9.1 Passing a row
12.9.2 Passing an entire 2D array
• Use array name as actual parameter
• Program examples
12.10 Multidimensional Arrays
•Array of arrays
•n indices in an n-dimensional array/multidimensional array
•m1 x m2 x m3 x….mn array is a collection of m1 * m2 * m3 *… mn
elements.
•A[I1][I2][I3]….[In], I1<= M1….
12.12 Applications of Arrays
• Implement vectors,matrices and other
rectangular tables
• Databases- 1D-records
• Implement data structures strings, stacks,
queues, heaps….
• Used for sorting and searching