Cs201 11

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Programming Lecture 11 ARRAYS Arrays They are special kind of data type They are like data structures in which identical data types are stored In C each array has They occupy continuous area of memory – – – name data type size Storage of an array in memory Name C[0] C[1] C[2] C[3] C[4] C[5] C[6] C[7] C[8] memory 24 59 35 ... ... ... ... ... ... C[9] Index ... 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] ; Referring to Array Elements Array name e.g. age index number age [ 4 ] Example1: Using Arrays for ( i = 0 ; i < 10 ; i ++ ) { cin >> age [ i ] ; } Example 2 totalAge = 0 ; for ( i = 0 ; i < 10 ; i ++ ) { totalAge + = age [ i ] ; } Initializing an Array int age [ 10 ] ; for ( i = 0 ; i < 10 ; i ++ ) { age [ i ] = 0 ; } Initializing an Array int age [ 10 ] = { 0,0,0,0,0,0,0,0,0,0 } ; int age[ 10 ] = { 0 } ; Ini...
View Full Document

Ask a homework question - tutors are online