1 / 14

Computer Programming Code: CS1112

Computer Programming Code: CS1112. Lecture No. 8 Bilal Ashfaq Ahmed. ARRAYS. Arrays. Definition: “A collection of variables which are all of the same type “. They are special kind of data type(composite). They are data structures in which identical data types

viveca
Download Presentation

Computer Programming Code: CS1112

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. Computer ProgrammingCode: CS1112 Lecture No. 8 Bilal Ashfaq Ahmed

  2. ARRAYS

  3. Arrays • Definition: “A collection of variables which are all of the same type “. • They are special kind of data type(composite). • They are data structures in which identical data types are stored(homogeneous in nature). • In C each array has • Name • Data type • Size • Corresponding Index or Subscript • They occupy contiguous /continuous area of memory.

  4. Declaration of Arrays arrayType arrayName[Size]; For example , int age [ 10 ] ;

  5. Name memory Age[0] 24 Age [1] 59 Age [2] 35 Age [3] ... Age [4] ... Age [5] ... Age [6] ... Age [7] ... Age [8] ... Age [9] ... Storage of an array in memory Value Index

  6. Declaration of Arrays arrayType arrayName [numberOfElements ]; For example , int age [ 10 ] ; • More than one array can be declared on a line int age [10] , height [10] , names [20] ; • Mix declaration of variables with declaration of arrays int i , j , age [10] ;

  7. Referring to Array Elements Array name[array element]; e.g. age index number age [ 4 ];

  8. Example1: Using Arrays for ( i = 0 ; i < 10 ; i ++ ) { cin >> age [ i ] ; }

  9. Example 2 totalAge = 0 ; for ( i = 0 ; i < 10 ; i ++ ) { totalAge + = age [ i ] ; }

  10. Initializing an Array int age [ 10 ] ; for ( i = 0 ; i < 10 ; i ++ ) { age [ i ] = 0 ; }

  11. Initializing an Array int age [ 10 ] = { 0,0,0,0,0,0,0,0,0,0 } ; int age[ 10 ] = { 0 } ;

  12. Initializing an Array int age [ ] = { 1,2,3,4,5,6,7,8,9,10 } ; for ( i = 0 ; i < 10 ; i ++ ) ‘ i ‘ will have value from 0 to 9

  13. Displaying Results

More Related