4 Arrays in 'C'

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Day – 3.

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];

num reference num [0]


num [1]
num [2] An Array of 10
num [3]
Elements
num [4]
num [5]
of type int
num [6]
num [7]
num [8]
num [9]

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];

num[0] references the first element in the


array.
num[9] references the last element in the
array.

22-Jul-2008 5
Declaring and Creating
in One Step
) datatypearrayname[arraySize]=
{values seperated by comma};
Example :

char c[5] = {‘a’,’F’,’4’,’’=‘,’n’};

int i[4] = {12,15,0,2};

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};

This shorthand syntax must be in one


statement.

22-Jul-2008 8
Declaring, creating, initializing
Using the Shorthand Notation
float list[4] = {1.9, 2.9, 3.4, 3.5};

This shorthand notation is equivalent to


the following statements:
float list[4];
list[0] = 1.9;
list[1] = 2.9;
list[2] = 3.4;
list[3] = 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

Before the assignment After the assignment


list2 = list1; list2 = list1;

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:

int array1[5] = {2, 3, 1, 5, 10};


int array2[5];

array2 = array1;

22-Jul-2008 13
Multidimensional Arrays
Declaring Variables of Multidimensional Arrays and
Creating Multidimensional Arrays

int matrix[10][10];

for (i=0; i<10; i++)


for (j=0; j<10; j++)
{
matrix[i][j] = i * j;
}

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy