1 / 21

ARRAYS CHAPTER#04

ARRAYS CHAPTER#04. BS COMPUTER SCIENCE UNIVERSITY OF SWAT. ARRAYS. Array is a sequence of objects of same data type. The objects in an array are also called elements of the array. Represented in the computer memory by a consecutive group of storage locations.

elin
Download Presentation

ARRAYS CHAPTER#04

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. ARRAYSCHAPTER#04 BS COMPUTER SCIENCE UNIVERSITY OF SWAT

  2. ARRAYS • Array is a sequence of objects of same data type. • The objects in an array are also called elements of the array. • Represented in the computer memory by a consecutive group of storage locations. • Referenced by a single variable called array name.

  3. Each element is referenced by its position in the array. • The position of an element in an array an index value or subscript. • In an array with n elements, the index values are 0,1,2,…,n-1. • 0 represent the index value of first element and so on and n-1 represent the index value of last element.

  4. How elements of array is accessed? • An element of an array is accessed by its subscript value. The index or subscript value is written inside a pair of square brackets [ ] with the name of the array.

  5. For which purpose array is used? • Array is used to process a large amount of data of same data 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. • As few statements are used to process data in an array, so the use of array in a program reduce size of the program.

  6. Types of array: • The are two types: • 1.One-dimensional array • 2.Multidimensional array

  7. One-dimensional array • Also known as a list or linear array. • Consists of only one column or one row. • E.g., array • array[0] • array[1] • . • . • array[4]

  8. The above array contain real type data. • It has five elements • The first element is array[0] that is in position 0 and the last element is array[4] that is in position 5-1=4.

  9. Declaring one-dimensional array: • Defining the name of the array, its type and the total number of elements of the array is called declaring of the array. • Syntax: • type array-name[n]; • Where ‘n’ is an unsigned integer value, represent the total number of elements of the array.

  10. E.g., • double temp[24]; • intabc[5]; • char name[15]; • etc

  11. Accessing data in one-dimensional array: • Each element of an array is referenced by its index. • In an array of n elements, each element has an index value. • The index of the first element is 0 and so on and index of the last element is n-1.

  12. Input/Output data in one-dimensional array: • Data is entered in an array using the input statement “cin” or “assignment” statements. • It is entered into individual elements of the array . • Separate input statement is used to enter data into each element of the array. • Similarly to print the data the output statements are used. The data from each individual element of the array is accessed.

  13. As similar input/output statement is used to access data in each element of the array, so the statement is written only once and a loop structure is used to repeat the input/output statement to access data of the element of the array. • Note : See examples(4.01—4.06)

  14. Initializing one-dimensional array: • The assigning of values to the elements of the array at the time of its declaration is called initializing of the array. • E.g., • double temp[4]={66.3, 23.2, 88.5, 45.6}; • Note: See examples(4.07—4.09)

  15. Multi-dimensional array: • A multi-dimensional array is an array of more than one array. • Two-dimensional array consists of columns and rows. • The index value of two-dimensional array consists of two subscripts. • One subscript represents the rows number and the second subscript represents the column number.

  16. Declaration of two-dimensional array: • A two dimensional array is declared by giving two indexed values enclosed in square brackets. • Syntax: • type array-name[r][c]; • e.g., intabc[5][3];

  17. Accessing data in two-dimensional array: • Data is entered into individual elements of a two-dimensional array. • To enter data, the element is referenced by its index or subscript value. • Similarly, data is retrieved from an array from individual elements of the array. • Note: See examples(4.15 & 4.16)

  18. Initializing Tables: • Assigning values to the elements of a table at the time of its declaration is called initializing of the table. • e.g., • intabc[2][3]={{3,4,5}{6,7,3}{2,8,9}}; • See example 4.17

  19. Initializing character type tables: • The values in a table of “char” type is also assigned in the same way as in int, float or double type tables. • e.g., char st[5][3]= {{‘a’, ‘x’, ‘y’}, • {‘p’, ’e’, ’t’}, • {‘p’, ‘a’, ‘k’}, • {‘a’, ‘b’, ‘c’}, • {‘3’, ‘1’, ‘6’}};

  20. Also: • char st[5][4]={“axy”, “pet”, “pak”, “abc”, • “316”}; • See example 4.18 • Here variable “st” can be treated as a string array. It has the capacity to store five strings each of 3 characters including null characters.

  21. Thus only two characters can be stored in each string and the null character is automatically inserted at the end. • ( END OF CHAPTER 04 )

More Related