Professional Documents
Culture Documents
Outline
1.
2.
3.
4.
5.
6.
7.
Introduction
Arrays of Data
Array Declaration
Array Initialization
Operations on Array
Multidimensional Arrays
Index out of bound
2
What is Array?
From the dictionary:
An orderly arrangement of a
particular type of things
display or arrange (things) in a
particular way
What is an Array?
The variables that we have used so far have all
common characteristics:
Each variable could only store a single value at
a time.
Example:
int iCount, iLength, iNum;
double dAverage, dTotal;
char cSelection, cChoice;
An array is a collection of a fixed number of
components wherein all of the components are of
the same type(e.g: int, float, char)
What is an Array?
(Example)
aiNum
5
10
15
20
25
aiNum[0
]
aiNum[
1]
5 components or elements
in this array.
aiNum[
2]
aiNum[3
]
aiNum[
4]
Arrays
Array
Name of array
(Note that all
elements of this
array have the
same name, c)
Format:
arrayname[ position number ]
-45
6
0
c[3]
72
c[4]
1543
c[5]
-89
c[6]
c[7]
62
c[8]
-3
c[9]
c[10]
6453
c[11]
78
Position number
of the element
within array c
Arrays of Data
Engineering applications usually involve
large chunk of data (of common type)
Arrays provide easy and efficient concept
for data storage or management
Arrays are usually processed through
loops (processing is very common)
Arrays are accessed by indicating an
address or index
Arrays in C
Arrays can assume any type
(including the primitive data types)
int, char, string, double, float, etc.
Like any other instances, arrays must
be declared before use.
Two types of Arrays;
- One dimension
- Multi Dimensional
9
Array Declaration
Format:
Example:
int aiList[5];
const int Max_List_Size = 10;
int aiHours[Max_List_Size];
const int SIZE = 100;
double adAmount[SIZE];
const int Max_List_Size = 6;
char acAlphas[Max_List_Size];
#define N 10
double adB[N];
10
// array
int aiValue[3];
// process or display
// process or display
11
Arrays are
allocated bulk
memory
Single reference
used for multiple
locations
Items are accessed
based on index
(address) with
reference to first
int aiValue[8];
index
aiValue
aiValue[0]=23;
23
0
aiValue[1]=56;
56
aiValue[2]=100; 1
100
2
aiValue[3]=0;
0
3
aiValue[4]=12;
12
aiValue[5]=234; 4
234
aiValue[6]=666; 5
6
aiValue[7]=4;
666
7
4
12
Arrays Arithmetic
Operations on arrays are similar to
that on basic variables.
iSum = aiNum[0] + aiNum[1] +
aiNum[2] + aiNum[3];
iMult = 3 * aiNum[1];
iRemainder = aiNum[3] % 3;
iTotal = aiNum[1] * aiNum[2];
13
Array Initialization
Arrays can be initialized directly, but
assignments are done using loops
Like any other simple variable, arrays can also
be initialized while they are being declared.
double adSales[5] = {12.25, 32.50, 16.90, 23,
45.68};
adSales[0]=12.25, adSales[1]=32.50,
adSales[2]=16.90, adSales[3]=23.00,
adSales[4]=45.68;
14
Simple Program
Operations on Array
Reading data in an array
for (iIndex = 0; iIndex < 10; iIndex++)
scanf (%d, &aiSale[iIndex]);
Printing an array
for (iIndex = 0; iIndex < 10; iIndex++)
printf (%d , aiSale[iIndex]);
17
A simple example
The program declares and initializes the
array aiY. It uses a for loop with index
iLoop
to
access
the
successive
elements of aiY. For each loop iteration,
the value accessed id is added to the
variable iTotal which is finally displayed.
Note that the loop index iLoop starts
from 0 to 4 (not from 1 to 5). Also, note
that the array size n is declared in the
define statement.
18
19
Notes
The defined constants, #define is used to
ease any future amendments of the codes,
for instance, if the array is to be widen to
an n of 10 instead of 5, it would be
adequate by modifying the line:
#define n 5 #define n 10
there is no need to make any other changes
to the program, thus making the life of
programmer easier.
20
Sample Program
#include <stdio.h>
int main()
{
int aiA[3]= {11,22}, aiB[]={44, 55, 66},iLoop;
double adX[2],adY[10];
21
Sample Program
Output:
aiA[0]=11, aiA[1]=22, aiA[2]= 0
aiB[0]=44, aiB[1]=55, aiB[2]=66
Please enter two real numbers
77.0 88.0
adX[0] = 77.0 adX[1] = 88.0
adY[0]=0.00
adY[1]=100.00
adY[2]=200.00
adY[3]=300.00
adY[4]=400.00
adY[5]=500.00
adY[6]=600.00
adY[7]=700.00
adY[8]=800.00
adY[9]=900.00
22
Parallel Arrays
Two (or more) arrays are called
parallel if their corresponding
components hold related information.
int aiStudentId[50];
char acStudentGrade[50];
24
Multi-Dimensional Arrays
Arrays can have multiple dimensions
Most used is the 2-dimensional array
(for matrix implementation)
Actual implementation is a single
array (segmented)
Nested loop structure usually used
to access items
25
2-Dimensional Array
(Example)
int aiValue[4][2];
aiValue[2][1]=5;
Column
0
0
1
Row 2
3
Row
0
Row
1
index
aiValue
0
1
2
3
4
5
5
6
7
Multi-Dimensional Arrays
(cont..)
A collection of the same type of data
stored in contiguous and increasing
memory locations.
array_nam
Array dimension =
array_type
Declaration
of multi-dimensional
e
2
array:
two rows
three columns
first row
initial
values
second row
initial
values
27
Multi-Dimensional Arrays
(cont..)
Multiple subscripted arrays
Tables with rows and columns (m by n
array)
Like matrices: specify row, then column
Column 0 Column 1 Column 2 Column 3
Row 0
Row 1
Row 2
a[ 0 ][ 0 ] a[ 0 ][ 1 ] a[ 0 ][ 2 ] a[ 0 ][ 3 ]
a[ 1 ][ 0 ] a[ 1 ][ 1 ] a[ 1 ][ 2 ] a[ 1 ][ 3 ]
a[ 2 ][ 0 ] a[ 2 ][ 1 ] a[ 2 ][ 2 ] a[ 2 ][ 3 ]
Column subscript
Array
name Row subscript
28
Multi-Dimensional Arrays
(cont..)
Multi-dimensional array can be initialized
directly in the declaration statement.
For example:
int aiB[2][3] = {51, 52, 53, 54, 55, 56};
which initializes the elements to be
aiB[0][0] = 51
aiB[0][1] = 52aiB[0][2] = 53
aiB[1][0] = 54
aiB[1][1] = 55aiB[1][2] = 56
* note that C begins its subscripts at 0. The
rightmost subscript is incremented first.
29
Notes on Arrays
Arrays enable better and easier data
management system
Closely related to loops
Indexing is zero-based
(0 to n-1 for an array with n
locations)
Multi-dimensional arrays require
nested loop structure
(e.g. 2-dimensional array)
31
End of Class