0% found this document useful (0 votes)
14 views22 pages

Arrays Pps

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views22 pages

Arrays Pps

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Introduction to Arrays

• An array is one of the derived data types.

• Arrays are useful to process large amounts of data

• For example, suppose we have a temperature reading for each day of the year
and need to manipulate their values several times within a program

• With simple variables,

•We would need to declare 365 variables.


•We would not be able to loop over these variables
• The above problem can be solved by using an array: Temp [365]

• The elements in an array share the same name and are distinguished from one
another by their numbers or subscripts: 0,1,2….. Size-1

• An array must be declared before it can be used.


• Array declaration and definition provides information to the compiler - the
name of the array, type of array and the size.
1
Characteristics of an Array

• An array represents a group of related data.

• An array has two distinct characteristics:

An array is ordered: data is grouped sequentially: element 0, element 1,… n-1

An array is homogenous: every value within the array must share the same
data type. In other words, an int array can only hold integers, but not other
data types.

2
How to Create an Array

• Before we create an array, we need to decide on two properties:

Element type: what kind of data will the array hold? int, double, char?
Remember, we can only choose one type.

Array size: how many elements will the array contain?

3
Types of Arrays

• Arrays can be classified based on how the data items will be arranged.

• Arrays are broadly classified into three categories.

1. One dimensional arrays

2. Two dimensional arrays

3. Multi dimensional arrays

14
One Dimensional Arrays
• One dimensional array is a linear list consisting of related and similar data
items.

• In memory, all the data items are stored in contiguous memory locations.

Declaring a one dimensional array


• To declare ordinary variables, we use the following notation:
int number;

• To declare an array, the following syntax can be used:

Syntax: type arrayname[size];

For example:
int number[5];
• Creates an array of 5 integer elements.

5
Operations on one Dimensional Array (1 of 5)

1. Initializing
• There are four options to initialize one dimensional arrays.

Option A: Basic Initialization


• If the values of all the data elements are known, the values must be
specified within the braces.

int temp [5] = {75, 79, 82, 70, 68};

Memory Layout
temp[0] temp[1] temp[2] temp[3] temp [4]

75 79 82 70 68
1000 1002 1004 1006 1008
6
Memory Representation of an Integer Array

7
Operations on one Dimensional Array (2 of 5)
Option B: Initialization without size
int temp [] = {75, 79, 82, 70, 68};
• The compiler allocates memory for an array of 5 elements. The array temp is
initialized as shown below:
temp[0] temp[1] temp[2] temp[3] temp[4]

75 79 82 70 68
1000 1002 1004 1006 1008 Address

OptionC: Partial Array Initialization


int temp [5] = {75, 79, 82}; // All values are not specified

temp[0]temp[1] temp[2]temp[3]temp[4]

75 79 82 0 0
1000 1002 1004 1006 1008 Address

• Values for first three elements have been specified


• The elements for which values are not specified, are initialized to zero’s.
8
Operations on one Dimensional Array (3 of 5)
Option D: Initialization to all zero’s
• If values of data elements are not known well in advance, then all elements
can be initialized to zero’s.

For example:

int temp [5] = {0};

temp[0] temp[1] temp[2] temp[3] temp [4]

0 0 0 0 0
1000 1002 1004 1006 1008 Address

9
Operations on one Dimensional Array (4 of 5)
2. Inputting values
• Another way to fill the array is to read the values from the keyboard or a file.
• This could be done by using a loop.
Ex:
for(i=0; i<9; i++)
scanf(“%d”,&scores[i]);
3. Accessing elements of an array
• Index is used to access individual elements of an array.
Ex: scores[0];
4. Printing/Output values
• Another common application is printing the contents of an array.
• This can be easily done by using a loop.
Ex.
for(i=0; i<9; i++)
printf(“%d”,scores[i]);

10
Operations on one Dimensional Array (5 of 5)
5. Assigning values to an array
• Suppose we have an array temperature, the readings of the temperature for
the year is represented in the below example .
• The subscripts would be 0, 1, …, 364.
Temperature array

75 79 82 70 68 65 58 63 67 61
temperature[4]

• We can loop through the elements of an array by varying the subscript. To set all the
elements of any array to 0:
for(i=0;i<365;i++)
temperature[i] = 0;

We cant use assignment statement directly with arrays.

if a[], b[] are two arrays then the assignment a=b is invalid

Example37 shows 1D array declaration & accessing 11


Inter-function Communication
• To process arrays in a large program, arrays are required to be passed to
functions.

• Arrays can be passed to functions in two ways:

• Passing Individual Elements

• Individual elements can be passed to a function either by passing the


data values or by passing the addresses

• Passing the whole Array

• By using this, an Array name can be passed to the called function

12
Inter-function Communication
Passing Array Elements

13
Inter-function Definition

Passing the Address of an Array Element

14
Inter-function Definition
Passing the Whole Array

15
Two Dimensional Array

• An array of arrays is called multi-dimensional array.


• A multidimensional array can have two dimensions, three dimensions, four
dimensions, etc.
• A Multi-dimensional array which has only two dimensions is called as a two
dimensional array.
• Two-dimensional array can be regarded as a table with rows and columns:
Example:

'J' 'o' 'h' 'n' is a 3  4 array: 'J' 'o' 'h' 'n'


3 rows, 4 columns
'M' 'a' 'r' 'y' 'M' 'a' 'r' 'y'

'I' 'v' 'a' 'n' 'I' 'v' 'a' 'n'

• This is a two dimensional array having 3 rows and 4 columns


16
Two Dimensional Array
DECLARATION
Syntax:

• Type arrayName [rows][cols];

Example

• char names[3][4];

In the above declaration

• Char (elementType)  specifies type of element in each slot

• Names (arrayName)  specifies name of the array

• [3] (rows)  specifies number of rows

• [4] (cols)  specifies number of columns

17
Two Dimensional Array
Initialization

• A two dimensional array can be initialized at the time of declaration:


char names [3][4] = {
{‘J’, 'o', 'h', 'n'},
{‘M’, 'a', 'r', 'y'},
{‘I’, 'v', 'a', 'n'}
};
• An integer array can be initialized to all zeros by using the following
statement:
int nums[3][4] = {0};
• The declaration of a two dimensional array should have the column size
so that ,the elements can be in the form of rows and columns.

• To access an element of a 2D array, requires both the row and the column.

2Dexmpl 38 , 2Dexmp 39.c demonstrate 2d array declaration and initialization through turbo c.
18
Applications of Arrays
1. Matrix Operations
Addition, multiplication, trace, transpose…etc

2. Sorting: One of the most common applications in computer science is


sorting—the process through which data is arranged according to their
values.

Sorting techniques

• Selection Sort

• Bubble Sort

• Insertion Sort

• Merge Sort

• etc
19
Multi Dimensional Array

• C allows arrays of multiple dimensions.

• The general form of a multidimensional array is

type arrayName[s1][s2][s3]….[sm];

• For example
int a[10][3][2];
• It is represented as “an array of ten arrays of three rows of two columns”

20
Multi Dimensional Array
3-D Array example.

Example 40.c will demonstrate 3D array….


21
Representation of a Multi Dimensional
Array
A Three-dimensional Array (3 x 5 x 4)

22

You might also like