Download as pdf
Download as pdf
You are on page 1of 8
ee consider a situation where a program needs number of similar types of data elements to be stored, {A varabl is used to store one data element ata time, $0 to store multiple data elements; multiple variables are required. This solution has many problems : o As the list of variables increases, the length of program also increases, 0 Tomanipulate those variables, several assignment statements also needed. © Programmer needs to remember names of all the variables. ‘An alternate solution to above situation is to store similar type of data elements is create an array, Definition Array is a collection of elements of similar data type referred by the same variable name. Contiguous memory block is allocated to all these array elements. Array elements are of same data type i.e. once array is declared as integer, then all values which are present in an array will be of type integer only. with suitable example. (6 Marks) ; print ( (Arrays and Strings)....Page no. (5-3) “\n**** Weights of children ****"), ena (in Suvena = gd \n Chitra = %d \n Rajesh = 9ed } Pritesh = 96d \n Surya = Sed" Diitesh, surya) ; ",suvmna, chitra, rajesh, Tfcount of variables is small then this idea will work. Similarly to store weight of 100, 1000, or more children, programmer should declare that much ‘number of variables which is a bad idea, Also it is difficult to retrieve particular child's weight as the Programmer needs to remember all the variables. Second Scenario fu int weight; weight ‘oid main() Another idea is to use single variable to store these information as shown in following program. 25; ‘weight = 20; + Print ("\n weight of child = bd", weight) ; No doubt, this program will print the most recently updated value of the variable i.e. 20. Because initially the variable weight is assigned to 25; and when 20 is assigned to weight, previous value of weight i.e. 25 is lost and new value i.e. 20 is assigned to weight. A situation in which more than one values needs to be stored at atime, this idea will not work, Array can handle lots of elements using single’ name. Instead of declaring lots of variables, use a subscripted variable called as array to store the weights of children. Using the subscript we can manage random elements of the array. Example Consider the following group Weight = ( 25, 20,30, 19, 22) It represents. weights of five children. To refer the element 20 of the group the notation weight 2 is used, Similarly, to refer the element 22 of the group the notation weight 5 is used. But in the case of array, the element 25 is referred as wweight{0] ; because the array Scanned with CamScanner ‘woight [1] Ls suvseriot ‘Array name Fig. 5411 Element’s index counting starts with 0 instead of 1 Similarly, the element 22 is referred as weight{4}. In Beneral to refer any element of an array this notation should be used Here weight is the subscripted variable (array), ‘whereas iis its subscript. Where iis in the range 0-4 in our example. Need of array Let's consider the following scenario for proper understanding the need of arrays. Array variables are needed because of following requirements of user : © Store multiple values in the variable with same name. © Store multiple values (with same data type) together ie. store them one after another. © For easy access or retrieval of stored data elements. — Note that the elements should be of same data type ‘means it can be group of integers (int s), group of real numbers (float s or double s), group of characters (char 5), etc. Usually array of character is called as string 5.1.1 Characteristics of Array Characteristics of an Array Following are important characteristics of an array : 1. Anarray can hold only similar type of data, 2, Array is a subscripted variable which holds several elements at a time. 3. Using subscript random elements can be referred, 4. . Contiguous memory i allocated for elements of array. 5. Always the array_name is followed by [ ] which tell the compiler that we are dealing with an array, Between { and } bracket int type of value (size) 1, a sy 6, The first element in the aray is numbers g "elements 1 ess than the sizeof teary," 7, Before using an aay iS PE and dimen declared. “ ‘Types of Arrays = Amrays are categorized according 10 the din, used to define them. Here dimension ingae number of rows and columns used 0st seat = Array is categorized into following types, 1. One dimensional array 2. Two dimensional array ‘3. Multidimensional array Fig, 5.1.2 ‘Ypes of array YH 5.2 DECLARATION AND. INITIALIZATION OF ONE DIMENSIONAL ARRAYS One Dimensional Arrays @ An array with single subscript is called ® dimensional array, Declaration of one dimensional array Syntax ys? ~ Syntax for declaring one dimensional #9 | below: | shia ip ray nam [BHR Example ose Declare an array to store percentage of 10 7 float pereentagetians aE Z Scanned with CamScanner oe gement after declaring one dimer - Meme in Fig. 5.2.1. “nl yy ee spas ss [00 [1.70 [90.305 7 ena RpETRARe Fig. 5.2.1 of one dimensional array siaon syntax ing one dimensional array is given below? gai utc = jalues Example _ Hlere we initialize an array to store percentage of 10, students percentage[0] = 99.075 percentage[1] = 79.475 percentage[2] = 60.60; percentage[3] = 54.04; percentage[4] = 80.60; percentage{5] = 91.70; percentage[6] = 90.30; percentage(7] = 85.27; percentage(8] = 99.70; percentage(9] = 94.50; = Using the subseript we can assign value to specific The order of assignment statement fe example, It will form array element. doesn’t matter in abov following structure in memory. - Memory arrangement after declaring one dimensional array shown in Fig. 5.2.2. percentage [Siar Traa7 [60.60 [54.08 ]30.60]91.70 (50.30 [85.27 [99.70 [94.50 tor ps 2 5 6 3012 3014 3016 3018 3000 3002 3004 3006 3008 3010 SP pee Dans: Entering or Writing Data into One Dimensional Array We can enter data into array by array initialization or by accepting array elements from user. ‘We have seen how to initialize data, now we are going to study how to accept array element from user and store it in array. “The following code is used to accept ten elements from user and store into one dimensional array = for (i= 031 <10 si++) 2 { print ("\nEnter Percentage of student ded A") i41)$ seanf ("Zet", &percentageli] ) 5 + — Im this example for loop is used to repeat the statements 10 times. printf() function is used to request user to enter the data, scanf() function is used to accept input from percentage] tells the compiler the location wher the entered value to be stored. The value of i varie from 0 to 9 so percentagefi] specifies the positio where the entered elements are stored in the array. GQ. How element of 1-D array can be accessed? _(@ Marks user. D1 ans. : ‘Accessing or Reading Data from an One Dimension Array While accessing array elements we can use loop jj Tike used in writing the data into array. Only differen is that reading data from array doesn’t require | scanf () function. The following code is used to ace elements of one dimensional array : Heloyit+) for (i printf (*\n Percentage of student Zed \t 9 percentagelil) } z Se es ‘The for loop is used to repeat the statements 10 time printfQ function is used to display the array elem on soreen. ‘The ef specifies the compiler that the data whic going to be accessed is of type float type. Scanned with CamScanner ae declaration ts array elements from uset Cae es Faelide int main) { inarray[5]={33,30,34,31,39 i rint{(\n array elements ares\t")y for O5}<5;j+4) < Print{("26d\0".array{j))s } ‘int max=array[0]:——>| Sets the maximum number as the first element of array. Jmax) { Finds the largest max=array(jJ;, —p} element and store it in By max variable : pee 5 Print("\n largest number in S-element integer array is\%ed : max); retum 1; TESGeE See Output array elenents are: 33 30 34 2” Vargest number in S-elenent integer array R 4 } GQ. Write a program to find sum and average mark 1 obtained by a class of 20 students in a test, 7 ans: Program Finclude Hinclude int main() £ “int avg, sum ‘nt marks[20) 5 for (i=031< 2054+) C ” | prin ("\n Enter marks"); “ seant ('"%ed", &marks[i] ) ; } for(i=03;1<20;i++) { ‘sum = sum + marks J Calculates average marks avg = sum/20;—>| obtained by a class of 2. student printf ("n Average marks = %d", avg) ; marks marks. 70 90 39 97 76 67 63 59 72 72 87 8s 69 6s 60 66 98 86 marks marks marks, marks marks marks marks marks marks marks marks marks marks marks marks marks marks 75 marks 91 of marks = 1538 ge marks = 76 sum Avera = : an SQ. Write a program to find out yee legative and zero elements from a2!" yt Scanned with CamScanner Sets the maximum number as the first element of array. Finds the largest clement and store itn max variable. largest number in S-element integer roy ised”, Be max) ray lenapts. 30 4 areay 355 TEASERS {Eticrekdoe satdber accepts 10 elements fom store it in array’ } eT for(i=0; 7 (i 0s1<9,i+-+) ———p] Used to compare i” element with all the remaining elements { in the array. forj=i+1;j<10j++) { Le if(enay(j]] ascending order. } rewm 1; encar 10 eTenents for arrayt ordered array 5 peer eta aw OP Va. write a program to sort list elements descending order. Mee ee cuc ‘oR. Write a program to sort given nos in descending der. WZ ans.: Program Fielade “Pinelude int main) jtemPi ents for arrays jnt array[1O} Hs primfenter 10 elem LE coro Publications A SACHIN SHAH Venti Scanned with CamScanner Accepts 10 elements from user and store it in array. Used to compare i element with all the remaining elements in the array. If condition satisfies Swap the array[i] and array[j]. Prints the sorted array in ascending order, He OMtetere er 10 elenents for array: 89 67 54 347 32 76.99 55 § Descneding ordered array : o 6 7 3S MH My Scanned with CamScanner

You might also like