Chapter_6_Array_-_add_notes
Chapter_6_Array_-_add_notes
- Module 6 -
ARRAY
CCP203 - Computer Programming
Objectives
• To understand the definition and concepts of array in
manipulating data structures
• To understand how to declare an array, initialize an
array, and identify individual elements of an array
• To be able to pass arrays to functions
• To understand the use of arrays to store, sort and search
data via several techniques.
• To be able to declare and manipulate multi-dimensional
arrays
CCP203 - Computer Programming
Definition of Arrays
• An array – a group of memory locations
related by the fact that they all have the same
name and the same type.
int semester[4];
CCP203 - Computer Programming
grade 10 5 28 -95 …… 43
[0] [1] [2] [3] ……… [99]
Indexing in Array
• Indexing begins with 0 until i th
element.
e.g: int semester[4];
start with 0
semester[0] 20
semester[1] 100
semester[2] 585
semester[3] 1250
[i-1]
CCP203 - Computer Programming
name of array
semester[0] 20
semester[1] 100
semester[2] 585
semester[3] 1250
subscript
- must be an integer
- can consists of an expression
and the expression is evaluated
to determine the subscript
CCP203 - Computer Programming
grade[0] = 79 grade[1] = 80
grade[2] = 75 grade[3] = 50
grade[4] = 40 grade[5] = 55
cont’d…
CCP203 - Computer Programming
cont’d…
CCP203 - Computer Programming
cont’d…
CCP203 - Computer Programming
e.g:
Subscripted Variables
• Let say, if array declaration as below:
total = 0;
for (I = 0; I <= 3; ++i)
total = total + value[i];
CCP203 - Computer Programming
Example – Program 1
/*This program was developed to read 4 integer numbers */
#include <stdio.h>
void main(void)
{
int x[4],i;
for(i = 0; i < 4; i++)
{
printf(“Enter value for %d index :“, i);
scanf(“%d”, &(x[i]));
}
}
CCP203 - Computer Programming
Example – Program 2
/*This program was developed to print 5 integer numbers */
#include <stdio.h>
void main(void)
{
int x[5] = {20, 1, -4, 27, 500};
Example – Program 3
/*This program was developed to read 3 integer numbers and
print the total of the numbers.*/
#include <stdio.h>
void main(void)
{
int x[3], total, i;
for(i = 0; i < 3; i++)
{
printf(“x[%d] = “, i);
scanf(“%d”, &(x[i]));
total += x[i];
}
printf(“Total = %d\n”, total);
}
CCP203 - Computer Programming
Example – Program 4
/* This program was developed to read 3 integer numbers and
print the average of the numbers.*/
#include <stdio.h>
void main(void)
{
int x[3], total, i;
for(i = 0; i < 3; i++)
{
printf(“x[%d] = “, i);
scanf(“%d”, &(x[i]));
total += x[i];
}
printf(“Average = %d\n”, total/3);
}
CCP203 - Computer Programming
Example – Program 5
What is the output of this program?
Output:
#include <stdio.h> -15
void main(void) 44
{ 162
int x[4] = {199, -15, 44, 37}; 199 -15 44 37
int a = 5; 221 -13 46 39
printf(“%d\n”, x[1]);
printf(“%d\n”, x[7-a]);
printf(“%d\n”, x[0] – x[3]);
for(i = 0; i < 4; i++)
printf(“%d”, x[i]);
for(i = 0; i < 4; i++)
{
x[i] += 2;
printf(“%d”, x[i]);
}
for(i = 0; i < 4; i++)
printf(“%d”, x[i]);
}
CCP203 - Computer Programming
void main()
{
int WorkingHours[24];
:
modifyHours(WorkingHours, 24); /* function call */
}
/* function definition/header */
void modifyHours(int b[], int size)
{
:
}
CCP203 - Computer Programming
WorkingHours
b
Name
Nameof ofthe
thearray
arrayisis
When actually
actuallythe
thestarting
starting
Whenthethecalled
calledfunction
functionmodifies
modifies
array address
addressof
ofthe
thearray
arrayinin
arrayelements
elementsin inits
itsfunction
function
body, memory
memorylocations.
locations.
body,ititisismodifying
modifyingthe theactual
actual
elements
elementsof ofthe
thearray
arrayin intheir
their
original
originalmemory
memorylocations.
locations.
CCP203 - Computer Programming
Passing Arrays To Functions
Example – Program 6
What is the output from this program?
Output:
#include <stdio.h> The value in 0 index is 20
The value in 1 index is 12
void print1(int [4]); The value in 2 index is 7
}
void printNum(int nos[4])
{
int i;
for(i = 0; i < 3; ++i)
printf(“The value in %d index is %d\n”, i, nos[i]);
}
CCP203 - Computer Programming
Example – Program 7
What is the output from this program?
Output:
#include <stdio.h> The value in 0 index is 40
The value in 1 index is 24
void print1(int [4]); The value in 2 index is 14
Example – Program 8
What is the output from this program?
Output:
#include <stdio.h> The maximum value is 27
void main(void)
{
int nums[5] = {2, 18, 1, 27, 16};
findMax(nums);
}
void findMax(int vals[5]) /* find the maximum value */
{
int i, max = vals[0];
for(i = 0; i < 4; ++i)
if (max < vals[i])
max = vals[i];
printf(“The maximum vakue is %d”, max);
}
CCP203 - Computer Programming
Sorting Arrays
• Sorting data:- placing data into some
particular order such as ascending or
descending
e.g:
– AeU sorts all students list by their Student ID.
– Bank sorts all transactions by date.
• Several types of sorting techniques are
bubble sort, selection sort, heap sort,
insertion sort and etc.
CCP203 - Computer Programming
Sorting Arrays
• Algorithms:
– Involve several passes
– In each pass, successive pair of elements are
compared
– If a pair is in increasing order -> leave the values
– If a pair is in decreasing order -> swap the
values.
CCP203 - Computer Programming
Sorting Arrays
Data in original order (refer Fig 6.15 – page 224):
2 6 4 8 10 12 89 68 45 37
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
if(a[i]
if(a[i] >> a[i
a[i ++ 1]){
1]){
temp
temp == a[i];
a[i];
a[i]
a[i] == a[i+1];
a[i+1];
a[i+1]
a[i+1] == temp;
temp;
}}
Searching Arrays
• Searching:- process of finding a particular element
of an array.
• Two techniques of searching:-
– Linear search
– Binary search
Searching Arrays
Searching Arrays
• Algorithms for binary search:
e.g: find the search key, number 8 from these
following elements in an array:
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
2 4 6 8 10 12 14 16 18 20 22
2 4 6 8 10 12 14 16 18 20 22
middle element
CCP203 - Computer Programming
Searching Arrays
2. Compares the middle element to the search key
3. If the search key is equal to the middle element,
the array subscript is returned.
4. If the search key is not equal to the middle
element, continues searching on another half of
the array (sub-array) depending to these rules:
• If the search key is less than the middle
element -> search on the first half of the
array
• If the search key is more than the middle
element -> search on the second half of the
array
CCP203 - Computer Programming
Searching Arrays
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
2 4 6 8 10 12 14 16 18 20 22
8 < 12
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
2 4 6 8 10 12 14 16 18 20 22
8>6
CCP203 - Computer Programming
Searching Arrays
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
2 4 6 8 10 12 14 16 18 20 22
8 < 10
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
2 4 6 8 10 12 14 16 18 20 22
column subscript
row subscript
array name
CCP203 - Computer Programming
X[0][0]
X[0][0] == 1,
1, x[0][1]
x[0][1] == 2,
2, x[0][2]
x[0][2] == 3,
3, x[0][3]
x[0][3] == 44
X[1][0]
X[1][0] == 5,
5, x[1][1]
x[1][1] == 6,
6, x[1][2]
x[1][2] == 7,
7, x[1][3]
x[1][3] == 88
X[2][0]
X[2][0] == 9,
9, x[2][1]
x[2][1] == 10,
10, x[2][2]
x[2][2] == 11,
11, x[2][3]
x[2][3] == 12
12
CCP203 - Computer Programming
b[0][0] b[0][1]
b[1][0] b[1][1]
b[0][0] = 1 b[0][1] = 0
b[1][0] = 3 b[1][1] = 4
CCP203 - Computer Programming
Multi Dimensional Arrays
main()
{
int studentGrades[3][4];
:
maximum(studentGrades, 3, 4);/*function call*/
}
/*function definition*/
int maximum(int grades[][4], int pupils, int tests)
{
:
}
CCP203 - Computer Programming
Examples of Declarations & Function
Calls..Multidimensional Array
Assume that the array test, code and stocks are declared as:
Subscripted Variables
value[1][0] = value[0][1] – 7;
value[3][2] = 2 * (value[0][4] – 6);
value[0][4] = amount * val1;
amount = value[0][1] + value[2][2];
value[a - b][a + 1]; /* a = 4, b = 3 */
value[num * 4]; /* num is 2 */
CCP203 - Computer Programming
Example – Program 9
What is the output from this program?
Output:
#include <stdio.h> 6
The value is 6
2
void readPrint(int [2][3]); The value is 2
-13
void main(void) Thevalue is -13
{ 0
readPrint(nom); Thevalue is 0
} 589
void readPrint(int nos[2][3]) Thevalue is 589
{ 65
int row, column; Thevalue is 65
for(row = 0; row < 2; ++row)
for (column = 0; column < 3; ++column)
{
scanf(“&d\n”, &nos[row][column]);
printf(“The value is %4d\n”, nos[row][column]);
}
}
CCP203 - Computer Programming
Exercise 1 :
• Define suitable arrays to represent the
following data:
a) An array of 6 floats
b) An array of 5 characters
c) An array of 2 by 3 integers
Exercise 2:
What would be printed by the following program?
#include <stdio.h>
int main(void);
{
/* Local Definitions */
int list [10] = {2, 1, 2, 4, 1, 2, 0, 2, 1, 2};
intline[10];
/* Statements */
for (i=1; i<= 10; i++)
line[i] = list[9 – i];
for (i=1; i<= 10; i++)
printf (”%d %d\n”, list[i], line[i]);
return 0;
} /* main */
CCP203 - Computer Programming
Exercise 3 :
Trace and determine the output that will be generated by
each of the following C programs.
#include <stdio.h>
main(void);
{
int a,b = 0;
int c [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};