Unit I - Arrays at CSJMU - 6 Slides Handouts
Unit I - Arrays at CSJMU - 6 Slides Handouts
Arrays 1 Introduction
Outline • Arrays
1 Introduction – Structures of related data items
2 Arrays
3 Declaring Arrays – Static entity – same size throughout program
4 Examples Using Arrays – Dynamic data structures
5 Passing Arrays to Functions
6 Sorting Arrays
7 Case Study: Computing Mean, Median and Mode Using Arrays
8 Searching Arrays
9 Multiple-Subscripted Arrays
Arrays Unit I Dr. Rabins Porwal Arrays Unit I Dr. Rabins Porwal
1 2
3 4
Name of array
2 Arrays (Note that all 2 Arrays
elements of this
• Array array have the • Array elements are like normal variables
same name, c)
– Group of consecutive memory locations c[ 0 ] = 3;
– Same name and type c[0] -45 printf( "%d", c[ 0 ] );
c[1] 6
– Perform operations in subscript. If x equals 3
• To refer to an element, specify c[2] 0
c[3] 72 c[ 5 - 2 ] == c[ 3 ] == c[ x ]
– Array name c[4] 1543
– Position number c[5] -89
c[6] 0
• Format: c[7] 62
• c[ 0 ], c[ 1 ]...c[ n – 1 ]
Position number
of the element
Arrays Unit I
within array c Porwal
Dr. Rabins Arrays Unit I Dr. Rabins Porwal
3 4
5 6
Arrays Unit I Dr. Rabins Porwal Arrays Unit I Dr. Rabins Porwal
5 6
Arrays Unit I Dr. Rabins Porwal Arrays Unit I Dr. Rabins Porwal
7 8
9 1 /* fig06_10.c 10
2 Treating character arrays as strings */ Outline
4 Examples Using Arrays 3 #include <stdio.h>
4 1. Initialize strings
5 int main()
• Character arrays 6 {
2. Print strings
– String “first” is really a static array of characters 7 char string1[ 20 ], string2[] = "string literal";
8 int i;
– Character arrays can be initialized using string literals 9
2.1 Define loop
char string1[] = "first"; 10 printf(" Enter a string: ");
• Null character '\0' terminates strings 11 scanf( "%s", string1 ); 2.2 Print characters
12 printf( "string1 is: %s\nstring2: is %s\n" individually
• string1 actually has 6 elements
13 "string1 with spaces between characters is:\n",
– It is equivalent to 14 string1, string2 ); 2.3 Input string
char string1[] = { 'f', 'i', 'r', 's', 't', '\0' }; 15
– Can access individual characters 16 for ( i = 0; string1[ i ] != '\0'; i++ ) 3. Print string
17 printf( "%c ", string1[ i ] );
string1[ 3 ] is character ‘s’
18
– Array name is address of array, so & not needed for scanf 19 printf( "\n" );
9 10
11 12
11 12
13 14
15 16
• Traversal – the operation of visiting/ printing all Let arr be a linear array with lower bound LB
elements of an array and upper bound UB. The following algo
traverses arr applying an operation PROCESS
to each element of arr.
• Insertion & Deletion – the operation of adding and 1. Initialize counter
removing an element from an array Set counter = LB
2. Repeat steps 3 and 4 while counter <= UB
• Sorting – the operation of rearranging the 3. Visit element
elements of an array in some specified order Apply PROCESS to arr[counter]
4. Increase counter
• Searching – the operation of searching an element Set counter = counter + 1
in an array 5. Exit
Arrays Unit I Dr. Rabins Porwal Arrays Unit I Dr. Rabins Porwal
15 16
17 18
17 18
19 20
21 22
21 22
1 /* fig06_16.c 23 33 24
2 This program introduces the topic of survey data analysis. Outline 34 void mean( const int answer[] ) Outline
3 It computes the mean, median, and mode of the data */ 35 {
4 #include <stdio.h> 36 int j, total = 0;
5 #define SIZE 99 1. Function prototypes 37 3. Define function mean
6 38 printf( "%s\n%s\n%s\n", "********", " Mean", "********" );
7 void mean( const int [] ); 39
8 void median( int [] );
1.1 Initialize array 40 for ( j = 0; j <= SIZE - 1; j++ )
3.1 Define function
9 void mode( int [], const int [] ) ; 41 total += answer[ j ];
median
10 void bubbleSort( int [] ); 2. Call functions mean, 42
11 void printArray( const int [] ); 43 printf( "The mean is the average value of the data\n"
median, and mode 3.1.1 Sort Array
12 44 "items. The mean is equal to the total of\n"
13 int main() 45 "all the data items divided by the number\n"
14 { 46 "of data items ( %d ). The mean value for\n" 3.1.2 Print middle element
15 int frequency[ 10 ] = { 0 }; 47 "this run is: %d / %d = %.4f\n\n",
16 int response[ SIZE ] = 48 SIZE, total, SIZE, ( double ) total / SIZE );
17 { 6, 7, 8, 9, 8, 7, 8, 9, 8, 9, 49 }
18 7, 8, 9, 5, 9, 8, 7, 8, 7, 8, 50
19 6, 7, 8, 9, 3, 9, 8, 7, 8, 7, 51 void median( int answer[] )
20 7, 8, 9, 8, 9, 8, 9, 7, 8, 9, 52 {
21 6, 7, 8, 7, 8, 7, 9, 8, 9, 2, 53 printf( "\n%s\n%s\n%s\n%s",
22 7, 8, 9, 8, 9, 8, 9, 7, 5, 3, 54 "********", " Median", "********",
23 5, 6, 7, 2, 5, 3, 9, 4, 6, 4, 55 "The unsorted array of responses is" );
24 7, 8, 9, 6, 8, 7, 8, 9, 7, 8, 56
25 7, 4, 4, 2, 5, 3, 8, 7, 5, 6, 57 printArray( answer );
26 4, 5, 6, 1, 6, 5, 7, 8, 7 }; 58 bubbleSort( answer );
27 59 printf( "\n\nThe sorted array is" );
28 mean( response ); 60 printArray( answer );
29 median( response ); 61 printf( "\n\nThe median is element %d of\n"
30 mode( frequency, response ); 62 "the sorted %d element array.\n"
31 return 0; 63 "For this run the median is %d\n\n",
Arrays Unit I Dr. Rabins Porwal Arrays Unit I Dr. Rabins Porwal
32 } 64 SIZE / 2, SIZE, answer[ SIZE / 2 ] );
23 24
25 26
126 27 28
********
127 printf( "%2d", a[ j ] ); Outline Mode
Outline
128 } ********
129 } Response Frequency Histogram
Program Output Program Output
********
Mean 1 1 2 2
******** 5 0 5 0 5
The mean is the average value of the data
items. The mean is equal to the total of 1 1 *
all the data items divided by the number 2 3 ***
of data items (99). The mean value for 3 4 ****
this run is: 681 / 99 = 6.8788 4 5 *****
5 8 ********
******** 6 9 *********
Median 7 23 ***********************
******** 8 27 ***************************
The unsorted array of responses is 9 19 *******************
7 8 9 8 7 8 9 8 9 7 8 9 5 9 8 7 8 7 8 The mode is the most frequent value.
6 7 8 9 3 9 8 7 8 7 7 8 9 8 9 8 9 7 8 9 For this run the mode is 8 which occurred 27 times.
6 7 8 7 8 7 9 8 9 2 7 8 9 8 9 8 9 7 5 3
5 6 7 2 5 3 9 4 6 4 7 8 9 6 8 7 8 9 7 8
7 4 4 2 5 3 8 7 5 6 4 5 6 1 6 5 7 8 7
Arrays Unit I Dr. Rabins Porwal Arrays Unit I Dr. Rabins Porwal
27 28
29 30
8 Searching Arrays: Linear Search and Binary Algorithm for Linear Search
Search
• Search an array for a key value LINEAR (A, N, ITEM, LOC)
(The algorithm finds the location LOC of ITEM in the array A,
• Linear search or sets LOC=0 if the search is unsuccessful)
– Simple 1. [Insert ITEM at the end of A]
– Compare each element of array with key value Set A[N+1] = ITEM
– Useful for small and unsorted arrays 2. [Initialize counter]
Set LOC = 1
3. [Search for ITEM]
Repeat while A[LOC] ≠ ITEM
Set LOC = LOC+1
[End of loop]
4. [Successful ?]
If LOC = N+1, then Set LOC = 0
5. Exit
Arrays Unit I Dr. Rabins Porwal Arrays Unit I Dr. Rabins Porwal
29 30
31 32
33 34
35 36
Arrays Unit I Dr. Rabins Porwal Arrays Unit I Dr. Rabins Porwal
37 38
39 40
Arrays Unit I Dr. Rabins Porwal Arrays Unit I Dr. Rabins Porwal
39 40
41 1 /* fig06_22.c 42
2 Double-subscripted array example */ Outline
Example 3
4
#include <stdio.h>
#define STUDENTS 3
5 #define EXAMS 4 1. Initialize variables
6
Let 7 int minimum( const int [][ EXAMS ], int, int );
1.1 Define functions to
8 int maximum( const int [][ EXAMS ], int, int );
int A[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; 9 double average( const int [], int ); take double scripted
10 void printArray( const int [][ EXAMS ], int, int ); arrays
Each row is a particular student,
Assume base address of the array is 100. 11
12 int main() each column is the grades on the
exam. 1.2 Initialize
Since elements are integer type, w = 2 13
14
{
int student; studentgrades[][]
Address of (2, 3)th element in the array A is 15
16
const int studentGrades[ STUDENTS ][ EXAMS ] =
{ { 77, 68, 86, 73 },
2. Call functions
17 { 96, 87, 89, 78 },
1. Using row major order 18 { 70, 90, 86, 81 } };
minimum, maximum, and
19 average
addr (2, 3) = 100 + 2[4(2-1) + (3-1)] 20 printf( "The array is:\n" );
21 printArray( studentGrades, STUDENTS, EXAMS );
112 22
23
printf( "\n\nLowest grade: %d\nHighest grade: %d\n",
minimum( studentGrades, STUDENTS, EXAMS ),
2. Using column major order 24
25
maximum( studentGrades, STUDENTS, EXAMS ) );
41 42
43 44
45 46
The array is:
Outline
[0]
studentGrades[0] 77
[1]
68
[2]
86
[3]
73
Program Output
Applications & Limitations of Linear Arrays
studentGrades[1] 96 87 89 78
studentGrades[2] 70 90 86 81
Arrays Unit I Dr. Rabins Porwal Arrays Unit I Dr. Rabins Porwal
45 46