You are on page 1of 35

Arrays

C Programming  Arrays
Data types classification

2
Arrays - Introduction
 Arrays are structured data types.
 Array is a fixed-sized sequence of elements of same data type.
 Arrays are derived data types.
 Allows to store several data items of same type in a single array.
 E.g: Processing 100 numbers in program, how will you do?
 Bad way: Declare 100 integer variables..! 
 Good way: Use one array of size 100 

3
ONE-DIMENSIONAL ARRAYS
• List of items with one variable name and one subscript such a variable is called single
subscript variable or one-dimensional array
• Declaration: General syntax
data-type variable-name[size];
• E.g. int a[5];
a[0]
Int a,b,c,d,e;
a[1]
a[2]
a[3]
a[4]
4
ONE-DIMENSIONAL ARRAYS
• Initializing array with data
• E.g.
int a[5];
a[0] 5
a[0] =5;
a[1] 10
a[1]=10;
a[2] 15
a[2]=15;
a[3] 20
a[3]=20;
a[4] 25
a[4] = 25;
• Where is a[5]??
5
ONE-DIMENSIONAL ARRAYS
• E.g. float b[5];

b[0]
b[1]
b[0]=10.5;
b[2]
B[1]=20.4;
b[3]
B[2]=32.5;
b[4]

6
ONE-DIMENSIONAL ARRAYS
• Initializing float array with data
• E.g.
float b[5]; b[0] 5.1
b[0] =5.1;
b[1] 10.2
b[1]=10.2;
b[2] 15.5
b[2]=15.5;
b[3] 20.7
b[3]=20.7;
b[4] 25.4
b[4] = 25.4;
• How good is this type of array 7
initialization?
1-D array initialization
• Two types of initialization:
1. Compile time – During compilation of code.
2. Run time initialization – During execution of code.

8
1-D array initialization
1. Compile time initialization
- If all array members not initialized explicitly.
int a[3] = {5,10}; a[0] 5
a[1] 10
A[0]=5;
a[2] 0
A[1]=10;
b[0] 5.1
b[1] 2.0
b[2] 0.0
b[3] 0.0
float b[4] = {5.1,2.0};
9
Example for compile time initialization
#include<stdio.h>
Output:
void main(){ Array a1[] = 4 5 6
int a1[3] = {4,5,6}; Array a2[] = 10 0
Array a3[] = 0 1027808688
int a2[2]={10}; Array b[] = 3.300000 4.500000
Int a3[2];
float b[2] ={3.3,4.5};
printf("Array a1[] = %d %d %d\n",a1[0],a1[1],a1[2]);
printf("Array a2[] = %d %d\n",a2[0],a2[1]);
printf("Array a3[] = %d %d\n",a3[0],a3[1]);
printf("Array b[] = %f %f\n",b[0],b[1]);
10
1-D array initialization
2. Run time initialization
int a[3],i;
for(i =0;i<3;i++)
a[0] 5
{
a[1] 5
a[i] = 5;
a[2] 5
}
OR
for(i =0;i<3;i++)
a[0] 0
a[1] 2
{
a[i] = i*2; a[2] 4

} 11
1-D array initialization
2. Run time initialization may read data from user.
int a[3],i,temp;
for(i =0;i<3;i++)
{
printf(“Enter no:”);
scanf(“%d”,&temp); //line 1
a[i] = temp; //line 2
}
• Line 1 & 2 may be combined as : scanf(“%d”,&a[i]);

12
Program: sum and avg of n numbers

Output:

13
Character Arrays
• Used to store characters.
• Can be treated as sequence of characters(like for int and float) or as a single string.
• A string is marked by a special character ‘\0’ at the last valid position of the array.
• Most often character arrays are used to store and process strings.
• E.g. char s [6] = {‘H’,’e’,’l’,’l’,’o’,’\0’};

s[0] s[1] s[2] s[3] s[4] s[5]

H e l l o \0
14
Character Array - Initialization
• char s [6] = {‘H’,’e’,’l’,’l’,’o’,’\0’}

s[0] s[1] s[2] s[3] s[4] s[5]

H e l l o \0

• char t [ ] = “Hello World”;

t[0] t[1] t[2] t[3] t[4] t[5] t[6] t[7] t[8] t[9] t[10] t[11]

H e l l o W o r l d \0

15
Example program
#include <stdio.h>
Output:
void main(void)
Array a1[] = t t i
{
Array a2[] = a b 3
char a1[3];
Array a3[] = 1
char a2[]={'a','b','3'};
Array a2[] as string: ab3
char a3[3]={'1'};
Array a4[] as string: Hello World
char a4[] = "Hello World";
printf("Array a1[] = %c %c %c\n",a1[0],a1[1],a1[2]);
printf("Array a2[] = %c %c %c\n",a2[0],a2[1],a2[2]);
printf("Array a3[] = %c %c %c\n",a3[0],a3[1],a3[2]);
printf("Array a2[] as string: %s\n",a2);
printf("Array a4[] as string: %s",a4);
} 16
Two-Dimensional Arrays
• 1-D arrays for linear list numbers, characters etc.
• Two Dimensional Arrays are known as matrix.
• General syntax:
data-type array-name[row-size][col-size];
• E.g.: int n; int a[10];
int mat[3][4]; // 3 rows 4 columns

17
Two-Dimensional Arrays
• E.g.
int a[3][4]; // 3 rows 4 columns

Col 0 Col 1 Col 2 Col 3

Row 0 a[0][0] a[0][1] a[0][2] a[0][3]

Row 1 a[1][0] a[1][1] a[1][2] a[1][3]

Row 2 a[2][0] a[2][1] a[2][2] a[2][3]

18
Two-Dimensional Arrays: Initialization
• Compile time initialization
int a[3][4] = { 0,1,2,3,3,4,5,6,6,7,8,9};

Col 0 Col 1 Col 2 Col 3

Row 0 0 1 2 3

Row 1 3 4 5 6

Row 2 6 7 8 9

19
Two-Dimensional Arrays: Initialization
• Compile time initialization
int a[3][4] = { {0,1,2,3},{3,4,5,6},{6,7,8,9}};

Col 0 Col 1 Col 2 Col 3

Row 0 0 1 2 3

Row 1 3 4 5 6

Row 2 6 7 8 9

20
Two-Dimensional Arrays: Initialization
• Compile time initialization
int a[3][4] = { {0,1},
{3},
{6,7} };

Col 0 Col 1 Col 2 Col 3

Row 0 0 1 0 0
Row 1 3 0 0 0
Row 2 6 7 0 0

21
Two-Dimensional Arrays: Initialization
• Run time initialization
int a[3][4] ,row,col;
for(row=0;row<3;row++)
{
for(col=0;col<4;col++)
{
scanf("%d",&a[row][col]);
}
}
22
Example program1:

Output:

23
Example program 2: Run-time initialization of 2-d array

Output:

24
• A= 12
34
• B=5 6
78
C=A+B= 6 8
10 12

25
Bubble sort

26
Bubble sort

10 2 11 1 5 6

27
2 10 1 5 6 11

28
Binary Search

29
Key=60

Mid=(low+high)/2 0 1 2 3 4 5
10 20 30 40 50 100

30
Transpose of a Matrix

31
Matrix addition

32
Matrix multiplication

33
34
1.Print the diagonal elements of the matrix???
2. C Program to Display Upper Triangular
Matrix?
3.C Program to Display Lower Triangular
Matrix?

35

You might also like