Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.

Similar presentations


Presentation on theme: "Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also."— Presentation transcript:

1 Lecture #15 ARRAYS By Shahid Naseem (Lecturer)

2 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also called “elements of array”. An array is represented in the computer memory by a consecutive group of storage locations. These locations are referenced by a single variable called “array name”. Each element of an array is referenced by its position in the array by an index value like [1]

3 3 USES OF ARRAYS Arrays are used to process a large amount of data of same type. The data is stored in an array. The array is accessed by a single variable name. The index values are used to access individual elements of the array. The use of arrays in a program reduces size of the program.

4 4 TYPES OF ARRAYS There are two types of arrays. 1. One-dimensional arrays. 2. Multi-dimensional arrays.

5 5 ONE-DIMENSIONAL ARRAY One dimensional array is also known as a list or a linear array. It consists of only one column or one row. For example, the temperature of each hour of a day is stored in an array. It has 24 elements. Defining the name of array, its types and the total number of elements of an array is called “Declaring of the array”. 22.2 23.5 19.7 - - 15.3 Temp[0] Temp[1] Temp[2] Temp[4] Temp[23]

6 6 ACCESSING DATA IN ONE DIMENSIONAL ARRAY Each element of an array is referenced of an array is referenced by its index. The index of the first element is always 0 and of the last element is n-1. The index value is written within square brackets after the array name. Data is entered in an array by using individual elements of the array. To print the data from an array, the output statements are used.

7 7 PROGRAM #include void main() { float a[5]; int i; a[0]=9.9; a[1]=12.9;for (i=0;i<=4;i++) a[2]=13.1;cout<<“value in a[“<<i<<“]=“a[i]<<endl; a[3]=8.9;} a[4]=10.6;

8 8 STRING VARIABLES String variables are declared in the same way as character type variables are declared. String variable is an array of character type. The length of the string is the total number of elements of the array. The syntax is char variable_name[n] e.g. char str[15]

9 9 PROGRAM \\write\\write a program to initialize a string with the word “Pakistan” and then print it on the screen. # include main () { char str[15]=“pakistan”; clrscr(); cout<<str; }

10 10 SORTING ARRAYS The process of arranging data is a specified order is called sorting. Numeric type data may be arranged either in ascending or in descending order. Character type data may be arranged in alphabetical order. There are different methods to sort data in a list. The most commonly used methods are 1. Bubble Sort 2. Selection Sort

11 11 SORTING ARRAYS Bubble Sort: The bubble short method is used to arrange values in ascending or in descending order. To arrange an array in ascending order, two neighbouring elements are compared. If one element is larger than the other, the two are exchanged. Through the exchange of elements, the larger value slowly floats or bubbles up to the top. In descending order, the smaller value slowly floats up to the top.

12 12 SORTING ARRAYS Selection Sort: This method is also used for sorting arrays in ascending or in descending order. Following method is used to sort an array in ascending order using selection sort. First replace 1 to 4, then 4 to 19 and then 13 and 19. 419113 14 19

13 13 MULTI-DIMENSIONAL ARRAYS A multi-dimensional array is an array of more than one array. An array of two one-dimensional arrays is called two dimensional array. It is also known as table or matrix. A two dimensional array consists of columns and rows. Each element of the two-dimensional array is referenced by its index values. The index value of a two-dimensional array consists of two subscripts. One represents row number and second represents the column number.

14 14 MULTI-DIMENSIONAL ARRAYS If “temp” is a two dimensional array having 7 rows and 3 column, its first element is temp [0][0] and last element is temp[6]][2]. Temp[0][0]Temp[0][1]Temp[0][2] Temp[1][0] --- --- --- --- Temp[6][0]Temp[6][2]

15 15 PROGRAM \\write\\write a program to initialize value to a table xyz having 4 columns and 3 rows at the time of declaration. Also prinnt data form the table in tabular form. # include main () { int xyz[3][4] = {{2,3,10,1},{6,12,16,3},{4,3,13,17}}; int r,c; clrscr(); for (r=0;r<=2;r++)

16 16 PROGRAM { for(c=0;c<=3;c++) { cout<<xyz[r][c]<<“\t”; } cout<<endl; Getch(); }


Download ppt "Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also."

Similar presentations


Ads by Google