11. ARRAYS
Definition An ARRAY is a group of memory locations related by the same name and same data type. An array uses a single identifier, together with an integer index to create a variable that can hold many values. Each value in the array is called an element and is referenced by the identifier and its index. 2
Definition Visual representation of an array: nbr[0]
25
1st element
0
2nd element
nbr[2]
-8
3rd element
nbr[3]
43
4th element
nbr[1] same name
An array of integers
SIZE = 4
indices 3
Array Basics An array has a fixed number of elements based on its creation. The elements are ALWAYS numbered from 0 to 1 less than the array’s size. Array indices must fall within bounds and must resolve to type integer. In C, referencing an element outside of the created bounds is possible but not recommended. 4
Arrays in C:Declaring and Referencing Arrays To declare an array, specify type, name, size. type Ex. int eightnum[8] name size
float nbrs[SIZE]
• To access the element, refer to name and index. Ex. printf(“first number is %d”, eightnum[0]); x = eightnum[4] + eightnum[6] *eightnum[1]; eightnum[3]=4; 5
Arrays in C: Initializing Arrays Arrays may be initialized when they are declared. An array is initialized using a code of block containing comma-delimited values which match in position the elements in the array. EX: float nbrs[5]={0.02, 1.6, 3, 5.9, 3.086}
If there are values in the initialization block but not enough to fill the array, all the elements in the array without values are initialized to 0 in the case of float or int, or '\0' in the case of char. EX. int x[7] = {1, 2, 3} x[0]=1, x[1]=2, x[2]=3, x[3]=x[4]=x[5]=x[6]=0
6
Example 1: Initializing the elements of an array to even integers #include<stdio.h> #define SIZE 10 int main() { int even[SIZE], ctr; for (ctr=0; ctr<=SIZE-1; ctr++) even[ctr]= 2 + 2*ctr;
}
for (ctr=0; ctr<=SIZE-1; ctr++) printf(“even[%d] %d”, ctr, even[ctr]);
7
Example 2: Simulating balldrawing experiment A box contains 4 balls (red, blue, green and yellow). Write a program that will simulate the drawing of balls from the box (with replacement) and determining the frequency of occurrence of each color. Example: for a draw of 100 balls, Red – 23 times Blue – 27 times Green – 22 times Yellow – 28 times 8
Example 2: (cont’n) int colors, draw, frequency[4]
1.For each draw of a ball, to for (draw = 0; draw<=100; draw++) the 100th draw, rand() – used to generate a random 1.1 Generate a random number number. color=rand()%4; 1.2 Determine which color it represents. 1.3 Increase the frequency of ++frequency[color]; represented color. 2. Print the frequency of for (color = 0; color<=3; color++) occurrence of each color. printf(“%d%10d\n”, color, frequency[color]);
9
Example 2: Complete C Program #include <stdio.h> #include<stdlib.h> #include #define size 4 int main(void) { int color, draw, frequency[size]= {0}
for (draw = 0; draw<=100; draw++){ color=rand()%4; ++frequency[color]; } for (color = 0; color<=3; color++) printf(“%d%10d\n”, color, frequency[color]); return 0;
srand[time[NULL]]; } printf(“0-red, 1-blue, 2-green, 3-yellow\n”) 10
Array applications Sorting Algorithms Sorting data is placing the data into a particular order such as ascending or descending. This is one of the most important computing applications. Bubble Sort/Sinking Sort is one of the most popular sorting algorithm. The smaller values gradually “bubble” their way upward to the top of the array. 11
Bubble Sort Algorithm 1. Repeat the following procedure until the list is sorted. 2. Compare two successive values until the last value is reached. 2.1 Compare the adjacent two values. 2.2 Swap the position of the values if the second value is lower. If not, maintain the position. 12
Implementation of Bubble Sort in C #include <stdio.h> #define SIZE 5 int main() { int num[SIZE]; int i, swapped = 0, temp; printf("Enter the integers to be sorted:\n"); for(i=0; i<SIZE; i++) { scanf("%d",&num[i]); printf("num[%d] = %d\n",i,num[i]); } printf("\nData in original order:\n"); for ( i=0; i<SIZE; i++ ) { printf("%d ",num[i]); } 13
Implementation of Bubble Sort in C do { Swapped = 0;
/* reset */
for (i=0; i<SIZE-1; i++) { if ( num[i] > num[i+1] ) { temp = num[i]; num[i] = num[i+1]; num[i+1] = temp; Swapped = 1; } }
/* swapped */
} while( swapped ); 14
Implementation of Bubble Sort in C printf("\nData in sorted order:\n"); for ( i=0; i<SIZE; i++ ) { printf("%d ",num[i]); } printf("\n"); return 0; }
15
Exercises Ask the user to input 5 integers then display the maximum and minimum in the list.
16