4 Arrays in 'C'
4 Arrays in 'C'
4 Arrays in 'C'
Arrays in ‘C’
22-Jul-2008 1
Arrays
) Introducing Arrays
) Declaring Array Variables, Creating Arrays,
and Initializing Arrays
) Passing Arrays to Methods
) Copying Arrays
) Multidimensional Arrays
) Search and Sorting Methods
22-Jul-2008 2
Introducing Arrays
Array is a data structure that represents a collection of the
same types of data.
int num[10];
22-Jul-2008 3
Declaring Array Variables
) datatype arrayname[index];
Example:
int list[10];
char num[15];
float hat[20];
22-Jul-2008 4
Creating Arrays
datatype array-name[size];
Example:
int num[10];
22-Jul-2008 5
Declaring and Creating
in One Step
) datatypearrayname[arraySize]=
{values seperated by comma};
Example :
22-Jul-2008 6
The Length of Arrays
) Once an array is created, its size is fixed. It
cannot be changed.
For example,
int arr[10];
You can not insert any number to arr[11]
location because it is not initialized.
22-Jul-2008 7
Initializing Arrays
) Declaring, creating, initializing in one step:
float hat[4] = {1.9, 2.9, 3.4, 3.5};
22-Jul-2008 8
Declaring, creating, initializing
Using the Shorthand Notation
float list[4] = {1.9, 2.9, 3.4, 3.5};
22-Jul-2008 9
CAUTION
Using the shorthand notation, you have to
declare, create, and initialize the array all
in one statement. Splitting it would cause
a syntax error. For example, the following
is wrong:
float list;
list = {1.9, 2.9, 3.4, 3.5};
22-Jul-2008 10
Example: Copying Arrays
The program simply creates two
arrays and attempts to copy one to the
other, using an assignment statement.
22-Jul-2008 11
Copying Arrays
list1 list1
Contents Contents
of list1 of list1
list2 list2
Contents Contents
of list2 of list2
Garbage
22-Jul-2008 12
Copying Arrays
With direct assignment:
array2 = array1;
22-Jul-2008 13
Multidimensional Arrays
Declaring Variables of Multidimensional Arrays and
Creating Multidimensional Arrays
int matrix[10][10];
float mat[5][5];
22-Jul-2008 14
Multidimensional Array Illustration
0 1 2 3 4 0 1 2 3 4 0 1 2
0 0 0 1 2 3
1 1 1 4 5 6
2 2 7 2 7 8 9
3 3 3 10 11 12
4 4 int[][] array ={
{1, 2, 3},
int matrix[5][5]; matrix[2][1] = 7; {4, 5, 6},
{7, 8, 9},
{10, 11, 12}};
22-Jul-2008 15
Shorthand Notations
You can also use a shorthand notation to declare, create and
initialize a two-dimensional array. For example,
int[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
This is equivalent to the following statements:
array[0][0] = 1; array[0][1] = 2; array[0][2] = 3;
array[1][0] = 4; array[1][1] = 5; array[1][2] = 6;
array[2][0] = 7; array[2][1] = 8; array[2][2] = 9;
array[3][0] = 10; array[3][1] = 11; array[3][2] = 12;
22-Jul-2008 16
Ragged Arrays
Each row in a two-dimensional array is
itself an array. So, the rows can have
different lengths. Such an array is
known as a ragged array. For example,
int[][] matrix = {
{1, 2, 3, 4, 5},
{2, 3, 4, 5},
{3, 4, 5},
{4, 5},
{5}
}; 22-Jul-2008 17
Exercise : Bubble Sort
int i[] = {2, 9, 5, 4, 8, 1, 6}; // Unsorted
Pass 1: 2, 5, 4, 8, 1, 6, 9
Pass 2: 2, 4, 5, 1, 6, 8, 9
Pass 3: 2, 4, 1, 5, 6, 8, 9
Pass 4: 2, 1, 4, 5, 6, 8, 9
Pass 5: 1, 2, 4, 5, 6, 8, 9
Pass 6: 1, 2, 4, 5, 6, 8, 9
22-Jul-2008 18
Created By,
) Mr. Tushar B Kute,
Lecturer in Information Technology,
K. K. Wagh Polytechnic, Nashik.
tbkute@gmail.com
22-Jul-2008 19