1 / 16

INTRODUCTION TO COMPUTER

INTRODUCTION TO COMPUTER. Lecture #15 ARRAYS. By Shahid Naseem (Lecturer). ARRAYS DEFINITION. An array is a sequence of objects of same data type. The objects in an array are also called “elements of array”.

tucker
Download Presentation

INTRODUCTION TO COMPUTER

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. INTRODUCTION TO COMPUTER Lecture #15 ARRAYS By ShahidNaseem (Lecturer)

  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. 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. TYPES OF ARRAYS • There are two types of arrays. 1. One-dimensional arrays. 2. Multi-dimensional arrays.

  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 numberof elements of an array is called “Declaring of the array”. Temp[0] Temp[1] Temp[2] Temp[4] Temp[23]

  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. PROGRAM #include<iostream.h> void main() { float a[5]; inti; 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. 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. PROGRAM \\write a program to initialize a string with the word “Pakistan” and then print it on the screen. # include<iostream.h> #include<conio.h> main () { char str[15]=“pakistan”; clrscr(); cout<<str; }

  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. 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. 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.

  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. 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].

  15. PROGRAM \\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<iostream.h> #include<conio.h> main () { int xyz[3][4] = {{2,3,10,1},{6,12,16,3},{4,3,13,17}}; intr,c; clrscr(); for (r=0;r<=2;r++)

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

More Related