Professional Documents
Culture Documents
com
Array
Session 9
Arrays
yantox_ska@yahoo.com
Array index starts with zero The last index in an array is num 1 where num is the no of elements in a array int a[5] is an array that stores 5 integers a[0] is the first element where as a[4] is the fifth element We can also have arrays with more than one dimension float a[5][5] is a two dimensional array. It can store 5x5 = 25 floating point numbers The bounds are a[0][0] to a[4][4]
Structures
yantox_ska@yahoo.com
Structures are user defined data types It is a collection of heterogeneous data It can have integer, float, double or character data in it We can also have array of structures struct <<structname>> { members; }element; We can access element.members;
Arrays
yantox_ska@yahoo.com
Arrays in C are composed of a particular type, laid out in memory in a repeating pattern. Array elements are accessed by stepping forward in memory from the base of the array by a multiple of the element size.
/* define an array of 10 chars */ char x[5] = {t,e,s,t, \0}; /* accessing element 0 */ x[0] = T; /* pointer arithmetic to get elt 3 */ char elt3 = *(x+3); /* x[3] */ /* x[0] evaluates to the first element; * x evaluates to the address of the * first element, or &(x[0]) */ /* 0-indexed for loop idiom */ #define COUNT 10 char y[COUNT]; int i; for (i=0; i<COUNT; i++) { /* process y[i] */ printf(%c\n, y[i]); }
Brackets specify the count of elements. Initial values optionally set in braces. Arrays in C are 0-indexed (here, 0..9) x*3+ == *(x+3) == t
Whats the difference between char x[] and char *x?
(NOT s!)
Symbol char x [0] char x [1] char x [2] char x [3] char x [4] Addr 100 101 102 103 104 Value t e s t \0
Introduction to Arrays
yantox_ska@yahoo.com
Array definition:
A collection of data of same type
Declaring Arrays
yantox_ska@yahoo.com
Accessing Arrays
yantox_ska@yahoo.com
Array Usage
yantox_ska@yahoo.com
Example: for (idx = 0; idx<5; idx++) { cout << score[idx] << "off by " << max score[idx] << endl; }
Loop control variable (idx) counts from 0 5
Array indexes always start with zero! Zero is "first" number to computer scientists C++ will "let" you go beyond range
Unpredictable results Compiler will not detect these errors!
5-12
5-13
Always use defined/named constant for array size Example: const int NUMBER_OF_STUDENTS = 5; int score[NUMBER_OF_STUDENTS]; Improves readability Improves versatility Improves maintainability
In calculations involving size: lastIndex = (NUMBER_OF_STUDENTS 1); When passing array to functions (later)
Initializing Arrays
yantox_ska@yahoo.com
As simple variables can be initialized at declaration: int price = 0; // 0 is initial value Arrays can as well: int children[3] = {2, 12, 1};
Equivalent to following: int children[3]; children[0] = 2; children[1] = 12; children[2] = 1;
Arrays in Functions
yantox_ska@yahoo.com
As arguments to functions
Indexed variables
An individual "element" of an array can be function parameter
Entire arrays
All array elements can be passed as "one entity"
Indexed variable handled same as simple variable of array base type Given this function declaration: void myFunction(double par1);
Given previous example: In some main() function definition, consider this calls: int score[5], numberOfScores = 5; fillup(score, numberOfScores);
1st argument is entire array 2nd argument is integer value
Array Parameters
yantox_ska@yahoo.com
Summary
yantox_ska@yahoo.com
Indexed variables of array used just like any other simple variables
for-loop "natural" way to traverse arrays Programmer responsible for staying "in bounds" of array
Exercise 8.1
yantox_ska@yahoo.com
1. /*Materi : Type Data Array 2. Kasus : Menentukan bilangan terbesar dari bilangan-bilangan yang 3. tersimpan dalam variabel array. 4. */ 5. 6. #include<stdio.h> 7. #include<conio.h> 8. 9. void main() 10. { 11. int bil[15] ; 12. int x,Max ; 13. clrscr();
Exercise 8.2
yantox_ska@yahoo.com
14. //Memberi nilai variabel array 15. bil[1]=17; bil[2]=23; bil[3]=20;bil[4]=10;bil[5]=15; 16. bil[6]=5; bil[7]=33; bil[8]=95;bil[9]=8;bil[10]=88; 17. // Menampilkan Bilangan dari var Array 18. for(x=1;x<=10;x++) { 19. printf("Bilangan Array ke %2d adalah : %2d\n",x,bil[x]); 20. } 21. //Menentukan Bilangan Terbesar 22. Max=0; 23. for(x=1;x<=10;x++) { 24. if(bil[x] > Max) Max = bil[x]; 25. } 26. printf("\nBilangan Terbesarnya adalah : %d",Max); 27. getch(); 28. }
Task 8
yantox_ska@yahoo.com
Write a program to find the minimum number from n numbers that entries from keyboard.