Content text Unit V - Arrays and Strings – C Programming.pdf
www.ckundan.com.np 1 Unit V: Arrays and Strings – C Programming Introduction to Arrays: When we want to store and display the age of 100 employees, we have to do the following steps: Declare 100 different variables to store the age of employees. Assign a value for each variable Display the value for each variable Handling so many variables is difficult and become lengthy. So, the concept of array is useful to such situations which we can group similar types of data. Arrays are a form of data structure that may hold a fixed-size collection of elements of the same type in a sequential order. Although an array is used to hold data, it is frequently more practical to consider of it as a collection of variables of the same type. Instead of declaring individual variables like number0, number1... and number99, we declare a single array variable called numbers and use numbers [0], numbers [1]... numbers[99] to represent individual variables. An index is used to access a specific element in an array. All arrays are made up of memory regions that are connected in some way. The first element is represented by the lowest address, while the last element is represented by the highest address. Properties of Array: The array contains the following properties. Each element of an array is of same data type and carries the same size, i.e., int = 4 bytes.
www.ckundan.com.np 2 Elements of the array are stored at contiguous memory locations where the first element is stored at the smallest memory location. Elements of the array can be randomly accessed since we can calculate the address of each element of the array with the given base address and the size of the data element. Advantages of Array: Code Optimization: Less code to the access the data. Ease of traversing: By using the for loop, we can retrieve the elements of an array easily. Ease of sorting: To sort the elements of the array, we need a few lines of code only. Random Access: We can access any element randomly using the array. Disadvantage of Array: Fixed Size: Whatever size, we define at the time of declaration of the array, we can't exceed the limit. So, it doesn't grow the size dynamically like LinkedList which we will learn later. Declaring Arrays: To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows: type arrayName [ arraySize ]; This is called a single-dimensional array. The arraySize must be an integer constant greater than zero and type can be any valid C data type. For example, to declare a 10 element array called balance of type double, use this statement: double balance[10]; Here balance is a variable array which is sufficient to hold up to 10 double numbers. Initializing Arrays: We can initialize an array in C either one by one or using a single statement as follows: double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
www.ckundan.com.np 3 The number of values between braces { } cannot be larger than the number of elements that we declare for the array between square brackets [ ]. If we omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if we write: double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0}; We will create exactly the same array as we did in the previous example. Following is an example to assign a single element of the array: balance[4] = 50.0; The above statement assigns the 5th element in the array with a value of 50.0. All arrays have 0 as the index of their first element which is also called the base index and the last index of an array will be total size of the array minus 1. Shown below is the pictorial representation of the array we discussed above: Accessing Array Elements: An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array. For example − double salary = balance[9]; The above statement will take the 10th element from the array and assign the value to salary variable. The following example shows how to use all the three above mentioned concepts viz. declaration, assignment, and accessing arrays: #include int main () { int n[ 10 ]; /* n is an array of 10 integers */ int i,j; /* initialize elements of array n to 0 */ for ( i = 0; i < 10; i++ ) { n[ i ] = i + 100; /* set element at location i to i + 100 */ } /* output each array element's value */ for (j = 0; j < 10; j++ ) { printf("Element[%d] = %d\n", j, n[j] ); } return 0; }