Chapter7 Arrays
Chapter7 Arrays
Objectives
Introduction
One Dimensional Array
Declaring Array
Array Initialization
Array Referencing
Sorting an Array
Searching an Array
Two Dimensional Array
Two Dimensional Array Declaration
Two Dimensional Array Initialization
Two Dimensional Array Referencing
String
String Initialization
String Referencing
1
Objectives
What is an Array?
It is a collection of two or more adjacent
memory cells, called array elements that
associated with particular symbolic name.
70.3 48.4 52.67 56.1 54.12 50.78 63.26 68.65 74.37 73.61
3
Introduction(Cont..)
Example 1 :
Declaration:
Memory allocation :
exam_score1 exam_score2
exam_score3 exam_score4
exam_score5
4
Introduction(Cont..)
By using an array..
Declaration :
int exam_score[5];
array index /
Memory allocation : subscript
array name
Address
exam_score[0] 0x2000
exam_score[1] 0x2004
exam_score[2] 0x2008
exam_score[3] 0x200C
0x2014
exam_score[4]
Terms :
1. Array
a collection of data items of the same name & data type.
2. Array name
name of array is an identifier.
3. Number of array element
maximum number that can be stored in array / size of array
4. Array subscript/Index
a value/expression enclosed in [ ] brackets after the array
name, specifying which array element to access. Must start with
0.
5. Size of array
number of bytes used to store the total number of elements of 6
the array.
Declaring Array
When declaring arrays, we have to specify :
1. Array data type
2. Array Name
3. Number of elements
Format :
Example :
int exam_score[28];
i) int c[10];
9
third element is refer as exam_score[2]
Array Initialization (cont..)
2. Run-Time Initialization of Arrays
• An array can be initialized at run time explicitly.
Example :
Example :
int exam_score[4] = {88,85}; --> the 3rd & 4th element are 0
10
int exam_score[5] = {0}; All elements are 0
Array Referencing
• Each individual element is referred by specifying array name & index
Format:
arrayname[ position number ]
//output for the fragment code :
Example : //output
element for index 0 = 4.05
89
element for index 1 = 3.45
(i) exam_score[0] = 89;
element for index 2 = 3.21
printf( "%d", exam_score[ 0 ] ); element for index 3 = 6.55
element for index 4 = 10.23
11
Array Referencing (cont..)
index 0 1 2 3 4
12
Array Referencing (cont..)
• Manipulation Statement using array :
Perform operations in subscript (index)
Example :
Output ?
13
Array Referencing (cont..)
• Manipulation Statement using array :
Perform operations in subscript (index)
Example :
double Mark [8]={16.0,12.0,6.0,8.0,2.5,12.0,14.0,10.5};
Mark[3] =25.0;//Mark[3]=25.0
Mark[0]=16
sum= Mark[0] + Mark[1];//sum=16.0+12.0=28.0 Mark[1]=12
sum += Mark[2];//sum=sum+Mark[2]=28.0+6.0=34.0 Mark[2]=28
16
Two Dimensional Array Declaration (cont..)
Example :
int exam_score[3][4];
17
Two Dimensional Array Declaration (cont..)
• The right index of an array is increased first before the left
index.
• Example :
int exam_score[3][4]={70,80,90,72,82,92,71,81,91,67,78,98 };
exam_score[0][0] 70
exam_score[0][1] 80
exam_score[0][2] 90
exam_score[0][3] 72
exam_score[1][0] 82
exam_score[1][1] 92
exam_score[1][2] 71
exam_score[1][3] 81
exam_score[2][0] 91
exam_score[2][1] 67
exam_score[2][2] 78
exam_score[2][3] 98 18
Two Dimensional Array Initialization
2-D array can be initialized at :
1. compile time (while declaring)
2. run time
Example :
• The best way : nest the data in braces to show the exact nature
of the array.
Example :
19
Two Dimensional Array Initialization (cont..)
• We also can declare the far left dimension size
implicitly.
Example :
(iii) int exam_score[ ][4] =
{{70,80,90,72},{82,92,71,81},{91,67,78,98}};
for (row=0;row<3;row++)
for(column=0; column<4; column++)
scanf(“%d”,&exam_score[row][column]);
//end of inner for
//end of outer for row column value
0 0
• TRY: Trace the source code flow using trace table.. 1
2
3
1 0
1
: 21
Two Dimensional Array Referencing
• Example:
exam_score[0][2] = 99;
exam_score[1][4] = exam_score[0][3] + 10;
22
Two Dimensional Array Referencing (cont..)
Example Program
#include<stdio.h>
void main( ) {
int exam_score[3][4]={{70,80,90,72},{82,92,71,81},{91,67,78,98}};
for(int row=0; row<3; row++)
{
for(int column=0; column<4; column++){
exam_score[row][column]= exam_score[row][column]+1;
printf("[%d][%d] = %d\n",
row,column,exam_score[row][column]);
}
}
}
//Program Output
[0][0] = 71
[0][1] = 81
[0][2] = 91
[0][3] = 73
[1][0] = 83
[1][1] = 93
[1][2] = 72
[1][3] = 82
[2][0] = 92
[2][1] = 68
[2][2] = 79 23
[2][3] = 99
String
24
String (cont..)
• We can declare a string variable as a 1-D character
array.
• Example :
char college[15];
character array college can store a string from 0 to
14.
• Example :
char college[11]=“UTHM JOHOR”;
U T H M J O H O R \0
25
String Initialization
• can initialize string variables at compile-time.
(a) a string
char college[11]={‘U’,‘T’,‘H’,‘M’,‘ ’,‘J’,‘O’,‘H’,‘O’,‘R’,‘\0’};
26
String Referencing
• can use scanf and %s, for input of a string variables.
Example :
scanf(“%s”,string_var);
27
String Referencing (cont..)
Example :
char string1[ ] = "first";
• equivalent to
• Array name is address of array, so ‘&’ sign is not needed for scanf.
scanf( "%s", string2 );
28
String Referencing (cont..)
Example Program
/* Treating character arrays as strings using scanf*/
#include <stdio.h>
int main() {
char string1[20], string2[] = "string literal";
int i;
printf("Enter a string: ");
scanf( "%s", string1 );
printf("string1 is: %s\nstring2 is:
%s\n",string1,string2);
printf("string1 with spaces between characters is:\n");
29
String Referencing (cont..)
Example Program
/* Treating character arrays as strings using gets*/
#include <stdio.h>
int main() {
char string1[20], string2[] = "string literal";
int i;
printf("Enter a string: ");
gets(string1);
printf("string1 is: %s\nstring2 is:
%s\n",string1,string2);
printf("string1 with spaces between characters is:\n");
30
Exercise
1. What will be the values of array k after execution
of the code segment below. The data input is 2,0
and 1.
int k[6] = {0, 0, 0, 0, 0, 0};
int i, n;
for (i = 3; i < 6; ++i)
{
scanf("%d", &n);
k[n] = i;
}
1.1 What is C code segment to access/ display the
elements for array k?
31
Exercise
2. Answer the following questions regarding an array called fractions
a) Define a symbolic constant SIZE to be replaced with the replacement text 10.
b) Define an array with SIZE elements of type float and initialize the elements to 0.
g) Print array elements 6 and 9 with two digits of precision to the right of the decimal point, and show the output that
is displayed on the screen.
h) Print all the elements of the array, using a for repetition statement. Assume the integer variable x has been
defined as a control variable for the loop. Show the output.
32
Exercise
3. mean (average)
• The mean or average value is defined as the sum
of all elements divided by the number of
elements.
33
Exercise
4. Find minimum and maximum for
dataset. Using array variable to
hold the data.
34
Exercise
5. Sort the values in the elements of
an array into ascending order using
the bubble sort algorithm.
The original data:
3 21 0 -3 34 -14 45 18
The sorted data in each iteration:
-14 21 3 0 34 -3 45 18
-14 -3 21 3 34 0 45 18
-14 -3 0 21 34 3 45 18
-14 -3 0 3 34 21 45 18
-14 -3 0 3 18 34 45 21
-14 -3 0 3 18 21 45 34
-14 -3 0 3 18 21 34 45
35
The bubble sort algorithm
36
// Pseudocode for bubble sort
37
/* File: sort.cpp */
#include <stdio.h>
#define N 8
int main() {
double a[N] = {3, 21, 0, -3, 34, -14, 45, 18};
double temp;
int i, j;
> sort.cpp
The original data:
3 21 0 -3 34 -14 45 18
The sorted data in each iteration:
-14 21 3 0 34 -3 45 18
-14 -3 21 3 34 0 45 18
-14 -3 0 21 34 3 45 18
-14 -3 0 3 34 21 45 18
-14 -3 0 3 18 34 45 21
-14 -3 0 3 18 21 45 34
-14 -3 0 3 18 21 34 45
39
Pass Arrays of Fixed Length to Function
• Pass one dimensional array
– When an array is passed to a function, what is actually passed is
the address of the first element of the array.
– In calling function, specify array name without brackets
type name[10];
……
function(10, name);
The number of elements of the array is usually passed to
function.
– The definition of called function
return_type function(int n, type array_name[]){
/* … */
}
The array_name in the function definition is not necessary the
40
same as in the function call.
Example 1:
/* File: passarray.cpp */
#include <stdio.h>
int main() {
double d1[5] = {1.0, 2.0, 3.0, 4.0, 5.0};
double d2[5] = {1, 2, 3, 4, 5};
int i;