1. 1. Introduction to Array 2. Arrays of Data 3. Array Declaration 4. Array Initialization 5....

51
EKT120 COMPUTER PROGRAMMING Arrays (Part I) Dr. Nik Adilah Hanin Bt. Zahri [email protected] 1

Transcript of 1. 1. Introduction to Array 2. Arrays of Data 3. Array Declaration 4. Array Initialization 5....

Week 6 Array (1)

EKT120COMPUTER PROGRAMMING

Arrays(Part I)

Dr. Nik Adilah Hanin Bt. [email protected]

11OutlineIntroduction to ArrayArrays of DataArray DeclarationArray InitializationOperations on ArrayMultidimensional ArraysIndex out of boundPassing Arrays to FunctionDisplaying Array in a FunctionHow Arrays are passed in a function call2What is Array?The variables that we have used so far have all common characteristics: Each variable could only store a single value at a time. Example: int iCount, iLength, iNum; double dAverage, dTotal; char cSelection, cChoice;

An array is a collection of a fixed number of components wherein all of the components are of the same type

3What is Array? (Example)Example: Suppose that there is a list of five integers: 5, 10, 15, 20, and 25

Previously we would declare five variables: int iNum1,iNum2,iNum3,iNum4,iNum5;

By using array, since they are all of the same data type, we could just write:int aiNum[5];

4What is Array? (Example)5101520255aiNumaiNum[0]aiNum[1]aiNum[2]aiNum[3]aiNum[4]int aiNum[5];

5 components or elements in this array

Elements are referred to index

Element aiNum[2] has index 2 and value 15

Arrays of DataEngineering applications usually involve large chunk of data (of common type)Arrays provide easy and efficient concept for data storage or managementArrays are usually processed through loopsArrays are accessed by indicating an address or index6Arrays in CArrays can assume any type (including the primitive data types)int, char, double, float, etc.

Like any other instances, arrays must be declared before use.

7Array DeclarationFormat:data_type array_name[int value];

Example:int aiList[5];const int Max_List_Size = 10;int aiHours[Max_List_Size];const int SIZE = 100;double adAmount[SIZE];const int Max_List_Size = 6;char acAlphas[Max_List_Size];#define N 10double adB[N];8Multiple Instances vs. Array9// multiple instanceint iValue1, iValue2, iValue3;

printf (Enter value 1: );scanf (%d, &iValue1);

printf(Enter value 2: );scanf(%d, &iValue2);

printf (Enter value 3: );scanf(%d, &iValue3);

// process or display// arrayint aiValue[3];int iCount;

for(iCount=0;iCount aiN[5] = aiN[6] = 0All elements = 0 int aiN[5] = {0} ; If size is omitted, initializers determine the sizeint aiN[ ] = { 1, 2, 3, 4, 5 }; 5 initializers, therefore 5 element array

13When declaring and initializing arrays, it is not necessary to specify the size of the array. The size of the array is determined by the number of initial values in the braces. double adSales[]={12.25,32.50, 16.90,45.68};14Array Initialization during Declaration (cont)Sample Program 1#include int main() { int aiA[3]= {11,22}, aiB[]={44, 55, 66};

printf(aiA[0]=%2d, aiA[1]=%2d, aiA[2]=%2d \n aiB[0]=%2d, aiB[1]=%2d, aiB[2]=%2d \n\n", aiA[0],aiA[1],aiA[2],aiB[0],aiB[1],aiB[2]); return 0;}15Initializes the first 2 elements of the aiA[]array. All the other elements are then automatically set to zero Because no array size is given (the brackets are empty) and three values are given in braces, the array is automatically declared to have a size of 3 with the value shown being the initial element values. Sample Program 1#include int main() { int aiA[3]= {11,22}, aiB[]={44, 55, 66};

printf(aiA[0]=%2d, aiA[1]=%2d, aiA[2]=%2d \n aiB[0]=%2d, aiB[1]=%2d, aiB[2]=%2d \n\n", aiA[0],aiA[1],aiA[2],aiB[0],aiB[1],aiB[2]); return 0;}16Initializes the first 2 elements of the aiA[]array. All the other elements are then automatically set to zero Because no array size is given (the brackets are empty) and three values are given in braces, the array is automatically declared to have a size of 3 with the value shown being the initial element values. aiA[0]=11, aiA[1]=22, aiA[2]= 0aiB[0]=44, aiB[1]=55, aiB[2]=66

Output:Sample Program 2#include

int main() { int listA[2],listB[5],iLoop; printf("Please enter two integers\n"); scanf("%d %d",&listA[0], &listA[1]); printf(\n listA[0] = %d listA[1] = %d\n\n", listA[0], listA[1]);

printf("Please enter five integers\n");

for(iLoop=0;iLoop arraySize - 1)It is a run-time error, happens when an index is outside the valid boundaries of the array. Example:int aiA[10]; int iX = 10 aiA[9]=3; //ok aiA[iX]=4;//10 is not within the range 0..9 In C, no guard against this problemDoes not check whether index value is within range or notCan result in accessing data of wrong memory location

35How to overcome?Use defined loops

for (iLoop=0; iLoop