Arrays Pps
Arrays Pps
• 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
• 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 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
Element type: what kind of data will the array hold? int, double, char?
Remember, we can only choose one type.
3
Types of Arrays
• Arrays can be classified based on how the data items will be arranged.
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.
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.
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
temp[0]temp[1] temp[2]temp[3]temp[4]
75 79 82 0 0
1000 1002 1004 1006 1008 Address
For example:
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
12
Inter-function Communication
Passing Array Elements
13
Inter-function Definition
14
Inter-function Definition
Passing the Whole Array
15
Two Dimensional Array
Example
• char names[3][4];
17
Two Dimensional Array
Initialization
• 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
• Selection Sort
• Bubble Sort
• Insertion Sort
• Merge Sort
• etc
19
Multi Dimensional Array
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.
22