FP201 - PROGRAMMING FUNDAMENTALS Unit 4.1 - Understand the use of array PREPARED BY: MAZNAH AHMAD,...

download FP201 - PROGRAMMING FUNDAMENTALS Unit 4.1 - Understand the use of array PREPARED BY: MAZNAH AHMAD, JTMK PSIS.

If you can't read please download the document

description

O BJECTIVES At the end of this module, students should be able to: Declare and use an array Use the array statement in C++ program Accessing element in an array

Transcript of FP201 - PROGRAMMING FUNDAMENTALS Unit 4.1 - Understand the use of array PREPARED BY: MAZNAH AHMAD,...

FP201 - PROGRAMMING FUNDAMENTALS Unit Understand the use of array PREPARED BY: MAZNAH AHMAD, JTMK PSIS I NDEX Objective Introduction to Array One dimensional array Two dimensional array O BJECTIVES At the end of this module, students should be able to: Declare and use an array Use the array statement in C++ program Accessing element in an array A RRAY D EFINITION Array is a collection of data elements of the same type that are referenced by a common name. Used to process a collection of data all of which is of the same type, such as list of name, list of temperature. A RRAY D EFINITION Arrays element consist of memory allocation and identified by index. Array size : st Index Indexs Two types of arrays One-dimensional array Two-dimensional array D ECLARING O NE D IMENSIONAL A RRAY Will have a single row and can have any number of columns. Will have only one subscript. Subscript refers to the dimension of the array. Array declaration of 10 alphabet type array_name[size] Eg : char huruf[10]; I NITIALIZING O NE D IMENSIONAL A RRAY Initialization is the process of assigning values to the array you have created. To assign initial values to each one of arrays elements we must enclose the values in curly braces ({ }) and separate them with comma (,). Eg : char huruf[5] = {a, b, c, d, e}; I NITIALIZING O NE D IMENSIONAL A RRAY Eg: int nombor[3] = {3, 24, 31}; nombor[0];//3 nombor[1];//24 nombor[2];//31 nombor[0+1];//nombor[1];//24 nombor[3]; nombor first index A CCESSING E LEMENT OF O NE D IMENSIONAL A RRAY Element is accessed by its index Array index refers to the location of the values in an array. The first element will always have the array index as 0. Syntax : [Array index] = Value; For example: marks[0]=95; marks[1]=85; marks[2]=75; A CCESSING E LEMENT OF O NE D IMENSIONAL A RRAY Eg: int my_array[5] = {11, 22, 33, 44, 55}; to store the value 75 in the third element of my_array, we could write the following statement: my_array[2] = 75; to pass the value in 4 th element of my_array and store the value into temporary variable, temp_value : int temp_value = my_array[3]; // also equals to 44 A CCESSING E LEMENT OF O NE D IMENSIONAL A RRAY if the name of an array is name, then name[0] is the name of the element that is in position 0, name[1] is the name of the element that is in position 1, etc. in general, the nth element is in position n-1. So if the array has n elements, their names are name[0], name[1], name[2], , name[n- 1]. it is important to be able to clearly distinguish between the two uses that brackets [ ] have related to arrays: int name[5]; // declaration of a new array name[2] = 75; // access to an element of the array. E XAMPLE Program Student_Marks.cpp will illustrate how to declare an array, initialize and access its elements. #include using namespace std; void main() { int marks[]={95,85,75,80,65}; cout