Array_Notes ARRAYS Definition of an Array: An array is a collection of elements of the same type that are referenced by a common name. • All the elements of an array occupy a set of contiguous memory locations. • Why need to use array type? • Consider the following issue: Instead of declaring 1000 variables we can declare one variable and store 1000 elements using array. int studmark[1000]; Here int specifies the type of the variable,and the word studmark specifies the name of the variable. The number 1000 tells how many elements of the type int will be in the array. The following diagram depicts how the element will be stored in memory 1 Array_Notes 2 Array_Notes • We can use index or subscript to identify each element or location in the memory. For example, studMark[0] will refer to the first element of the array and studMark[999] will refer to the last element of the array Types of an Array (i) single dimensional array (or) One dimensional array (or)1D (ii) Multi dimensional array One Dimensional array: (i) Single or one dimensional array is used to represent and store data in a linear form (ii) Array having only one subscript variable is called one dimensional array Declaration of single dimensional array A single or one dimensional array declaration has the following form, data_type array_name[array_size]; E.g float height[10]; Here, data_type define the base type of the array, which is the type of each element in the array. array_name is any valid C identifier name that obeys the same rule for the identifier naming. array_size defines how many elements the array will hold Here float specifies the type of the variable,and the word height specifies the name of the variable. The number 10 tells how many elements of the type int will be in the array. This number is often called the dimension of the array. The bracket( [ ]) tells the compiler that dealing with an array. 3 Array_Notes • Examples of the one­dimensional array declarations, int marks[20], age[40]; float price[10], weight[20]; char letter[50]; • The first example declares two arrays named marks and age of type int. Array ,marks can store up to 20 integer numbers while age can store up to 40 numbers. • The second line declares the array price of type float. It can store up to 10 floating­point values and weight is basic variable which shows array type can be declared together with basic type provided the type is similar and it can store up to 20 floating ­point values • The third line declares the array letter of type char. It can store a string up to 49 characters. • Why 49 instead of 50? Remember, a string has a null terminating character (\0) at the end, so we must reserve for it. 4 Array_Notes Array intialization • An array may be initialized at the time of declaration. • Giving initial values to an array. • Initialization of an array may take the following form, type array_name[size] = {a_list_of_value}; • For example: int idNum[7] = {1, 2, 3, 4, 5, 6, 7}; float FloatNum[5] = {5.6, 5.7, 5.8, 5.9, 6.1}; char Vowel[6] = {'a', 'e', 'i', 'o', 'u', '\0'}; • The first line declares an integer array idNum and it immediately assigns the values 1, 2, 3, ..., 7 to idNum[0], idNum[1], idNum[2],..., idNum[6] respectively. • The second line assigns the values 5.6 to FloatNum[0], 5.7 to FloatNum[1], and so on. • Similarly the third line assigns the characters 'a' to Vowel[0], 'e' to Vowel[1], and so on. Note again, for characters we must use the single apostrophe/quote (') to enclose them. • Also, the last character in Vowel is NULL character ('\0'). 5 Array_Notes • Initialization of an array of type char for holding strings may take the following form, char array_name[size] = "string_lateral_constant"; • For example, the array vowel in previous example could have been written more compactly as follows, char Vowel[6] = "aeiou"; • When the value assigned to a character array is a string (which must be enclosed in double quotes), the compiler automatically supplies the NULL character but we still have to reserve one extra place for the NULL. • For unsized array (variable sized), we can declare as follow, char Name[ ] = "STUDENT"; C compiler automatically creates an array which is big enough to hold all the initializer. 6 Array_Notes One dimensional array declaration and initialization int a[10] = {1,2,3,4,5} ­ These statements create and initialize the array variables in memory it will allocates the space in memory like as a[0] 1 a[1] 2 a[3] a[2] 3 4 a[9] 5 Print the values from the array for(row=0;row<=4;row++) { printf("%d",a[row]); } 7 Array_Notes Accept the values from the user Declaration int age[10]; Getting input values from the user through scanf statement Entering data into age array for(i=0;i<=2;i++) scanf("%d",&age[i]) The for loop causes the process of asking for and receiving a age from the user to be repeated 3 times. The first time through the loop i has a value 0, the scanf() statement will cause the value typed to be stored in the array element age[0],the first element of the array. This process will be repeated until i becomes 2. Printing the values from age array for(i=0;i<=2;i++) printf("%d",age[i]) 8 Array_Notes Multi dimensional array Array having more than one subscript variable is called multi dimensional array. Two dimensional Array • A two dimensional array has two subscripts/indexes. • The first subscript refers to the row, and the second, to the column. • The two dimensional array is also called a matrix • Its declaration has the following form, data_type array_name[row subscript][column subscript]; • For examples, int heiwei[3][4]; float a_matrix[20][25]; • The first line declares heiwei as an integer array with 3 rows and 4 columns. • Second line declares a_matrix as a floating­point array with 20 rows and 25 columns. • Two dimensional array initialization • int a[3][3] = { {1,2,3}, {4,5,6},{7,8,9 }} 9 Array_Notes Two dimensional Array Declaration int a[3][3]; a[0][0] a[1][0] a[2][0] a[0][1] a[1][1] a[2][1] a[0][2] a[1][2] a[2][2] declaration and initialization int a[3] [5] = { {1,2,3,4,5}, {6,7,8,9,10}, {11,12,45,34,12}} Print the values of array A for(i=0;i<=2;i++) for(j=0;j<=4;j++) { printf("%d",a[i][j]); } 10 Array_Notes Declaration int age[3][3]; Getting input values from the user through scanf statement store data into age array for(i=0;i<=2;i++) row for(j=0;i<=2;j++) column scanf("%d",&age[i][j]); Printing the values from age array for(i=0;i<=2;i++) for(j=0;i<=2;j++) printf("%d",age[i][j]); 11 Break statement: The break statement allows you to exit a loop from any point within its body, bypassing its normal termination expression. When the break statement is encountered inside a loop, the loop is immediately terminated, and program control resumes at the next statement following the loop. Continue statement: The continue statement is somewhat the opposite of the break statement. It forces the next iteration of the loop to take place, skipping any code in between itself and the test condition of the loop. void main() { /* local variable definition */ int a = 13; /* do loop execution */ do { if( a == 15) { /* skip the iteration */ a = a + 1; continue; } printf("value of a: %d\n", a); a++; }while( a < 17 ); } 12 13