COMPUTER PROGRAMMING SKILLS (4800153-3)
CHAPTER 6: ARRAYS
Second Term (1437-1438)
Department of Computer Science Foundation Year Program Umm Al Qura University, Makkah
Table of Contents –
Objectives & OutlineObjectives
1. Use the array data structure to represent lists of values.
2. Define, initialize, and print array values.
3. Refer to individual elements of an array.
4. Apply different operations on arrays (summing, average, search).
Table of Contents –
Objectives & OutlineOutline
1. Defining Arrays 2. Array Examples 3. Array Input/Output
4. Basic operations on Arrays: sum, Average, search
Defining Arrays –
OverviewIntroduction
# C programming language provides a data structure called the array, which can store a fixed-size sequential collection of elements of the same type.
# An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
# Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables.
# A specific element in an array is accessed by an index.
Defining Arrays –
OverviewSpecification
# All arrays consist of contiguous memory locations.
# The figure.1 (next slide) shows an integer array calledc, containing 12 elements.
# Any one of these elements may be referred to by giving the array’s name followed by the position number of the particular element in square brackets ([]).
# The first element in every array is the zeroth element.
# An array name, like other variable names, can contain only letters, digits and underscores and cannot begin with a digit.
Defining Arrays –
OverviewFigure 1:12-element array
Defining Arrays –
Defining ArraysSyntax
# To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as presented in Listing.1
# This is called a single-dimensional array.
# ThearraySizemust be an integer constant greater than zero andtype can be any valid C data type.
1 type arrayName [ a r r a y S i z e ] ;
Listing 1:Declaring an array
Defining Arrays –
Defining ArraysEXAMPLE
# To declare a 10-element array called balance of type double, we use the statement presented in Listing.2
# balanceis available array which is sufficient to hold up to 10 double numbers.
1 double b a l a n c e [ 1 0 ] ;
Listing 2:Declaring an array balance
Defining Arrays –
Initializing Arrays# You can initialize array in C either one by one or using a single statement as follows:
1 double b a l a n c e [ 5 ] = { 1 0 0 0 . 0 , 2 . 0 , 3 . 4 , 7 . 0 , 5 0 . 0 } ; Listing 3:initialize an array balance (1)
# The number of values between braces { } cannot be larger than the number of elements that we declare for the array between square brackets [ ].
# If you omit the size of the array, an array just big enough to hold the initialization is created.
# If you define a table as follow, you will create exactly the same array as you did in the previous example.
double b a l a n c e [ ] = { 1 0 0 0 . 0 , 2 . 0 , 3 . 4 , 7 . 0 , 5 0 . 0 } ; Listing 4:initialize an array balance (2)
Defining Arrays –
Initializing Arrays# Following is an example to assign a single element of the array:
1 b a l a n c e [ 4 ] = 5 0 . 0 ;
Listing 5:initialize an array balance (3)
# The above statement assigns element number 5th in the array with a value of 50.0.
# All arrays have 0 as the index of their first element which is also called base index and last index of an array is the total size of the array minus 1.
# Following is the pictorial representation of the same balance array as discussed above:
Figure 2:The pictorial representation of the balance array
Defining Arrays –
Example# An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array. For example:
1 double s a l a r y = b a l a n c e [ 9 ] ;
Listing 6:Access to an element of the balance array
# The above statement will take 10th element from the array and assign the value to salary variable.
Defining Arrays –
ExampleThe following example use all the mentioned three concepts: declaration, assignment and accessing arrays.
1 # i n c l u d e < s t d i o . h>
i n t main ( )
3 {
i n t n [ 10 ] ; // D e c l a r a t i o n : n i s an a r r a y o f 10 i n t e g e r s
5 i n t i , j ;
// I n i t i a l i z e elements o f a r r a y n
7 f o r ( i = 0 ; i < 1 0 ; i ++ ) {
n [ i ] = i + 1 0 0 ; // s e t element a t l o c a t i o n i t o i + 100
9 }
//output each a r r a y element ‘ s value
11 f o r ( j = 0 ; j < 1 0 ; j ++ ) {
p r i n t f (" Element[%d ] = %d\n ", j , n [ j ] ) ;
13 }
r e t u r n 0 ;
15 }
Listing 7:Accessing Array Elements
Defining Arrays –
ExampleOutput of Listing.7:
Element[0] = 100 Element[1] = 101 Element[2] = 102 Element[3] = 103 Element[4] = 104 Element[5] = 105 Element[6] = 106 Element[7] = 107 Element[8] = 108 Element[9] = 109
Basic operations on Arrays –
Summing1 # i n c l u d e < s t d i o . h>
i n t main ( void )
3 {
i n t a [ 1 2 ] = { 1 , 3 , 5 , 4 , 7 , 2 , 9 9 , 1 6 , 4 5 , 6 7 , 8 9 , 45 } ;
5 i n t i ; // c o u n t e r
i n t t o t a l = 0 ; // sum o f a r r a y
7
f o r ( i = 0 ; i < 1 2 ; ++ i ) {
9 t o t a l += a [ i ] ; }
11 p r i n t f ( " T o t a l o f a r r a y element v a l u e s i s %d\n ", t o t a l ) ; r e t u r n 0 ;
13 }
Listing 8:Computing the sum of the elements of an array
Output of Listing.8:
Total of array element values is 383
Basic operations on Arrays –
Average1 # i n c l u d e < s t d i o . h>
i n t main ( void )
3 {
i n t a [ 1 2 ] = { 1 , 3 , 5 , 4 , 7 , 2 , 9 9 , 1 6 , 4 5 , 6 7 , 8 9 , 45 } ;
5 i n t i ; // c o u n t e r
i n t t o t a l = 0 ; // sum o f a r r a y
7 f l o a t avg ; //average o f a r r a y
9 f o r ( i = 0 ; i < 1 2 ; ++ i ) { t o t a l += a [ i ] ;
11 }
avg= (f l o a t) t o t a l / 1 2 ;
13 p r i n t f ( " average o f a r r a y element v a l u e s i s %f \n ", avg ) ; r e t u r n 0 ;
15 }
Listing 9:Computing the average of the elements of an array
Output of Listing.9:
average of array element values is 31.916666
Basic operations on Arrays –
SearchSearching an Array with Linear Search
# The process of finding a particular element of an array is called searching.
# Many searching techniques are available, the linear search technique, the binary search technique...
# The linear search compares each element of the array with the search key.
# Because the array is not in any particular order, it’s just as likely that the value will be found in the first element as in the last.
# On average, therefore, the program will have to compare the search key with half the elements of the array.
Basic operations on Arrays –
Search1 # i n c l u d e < s t d i o . h>
i n t main ( void )
3 { i n t T [ 1 0 ] = { 4 , 3 6 , 8 9 , 1 1 , 1 2 4 , 9 0 , 4 , 3 3 , 7 , 2 9 } ; // c r e a t e a r r a y T i n t n ; // c o u n t e r
5 i n t searchKey ; // value t o l o c a t e i n a r r a y a
i n t element= −1; // I t holds l o c a t i o n o f searchKey or −1
7
p r i n t f ( " E n t e r i n t e g e r s e a r c h key : " ) ;
9 s c a n f ( "%d ", &searchKey ) ;
11 f o r ( n = 0 ; n < 1 0 ; n++ ) { i f ( T [ n ] == searchKey ) {
13 element=n ; } }
15 i f ( element ! = −1 ) {
p r i n t f ( " Found value i n element %d\n ", element ) ; }
17 e l s e { p r i n t f ( " Value not found " ) ; } r e t u r n 0 ;
19 }
Listing 10:Search in an Array
Basic operations on Arrays –
SearchOutput of Listing.10 (example 1):
Enter integer search key:
36
Found value in element 1
Output of Listing.10 (example 2):
Enter integer search key:
37
Value not found
References –
# C How to Program, 7th Edition, Paul Deitel, Deitel & Associates, Inc.
Harvey Deitel. 2013 Pearson.