Session 5
Arrays (Lab Guide)
Objectives At the end of this chapter, you will be able to: Use a Single Dimensional Array Use a Two Dimensional Array The steps given in this session are detailed, comprehensive and carefully thought through. This has been done so that the learning objectives are met and the understanding of the tool is complete. Please follow the steps carefully. Part I – For the first 1 Hour and 30 Minutes: 5.1
Arrays Arrays can be classified into two types based on dimensions: Single dimensional or Multi dimensional. In this chapter, let us focus on how to create and use arrays.
5.1.1
Sorting a single dimensional array A single dimensional array can be used to store a single set of values of same data type. Consider a set of student marks in a particular subject. Let us sort these marks in descending order. The steps to sort a single dimensional array in descending order are: 1.
Accept the total number of marks to be entered. To do this, a variable has to be declared and the value for the same has to be accepted. The code will be: int n; printf(“\n Enter the entered : ”); scanf(“%d”,&n);
2.
total
number
of
marks
to
be
Accept the set of marks. To accept a set of values for an array the array should be declared. The code for the same is, int num[100]; The number of elements in the array is determined by the value entered for the variable n. The n elements of the array should be initialized with values. To accept n values, a for loop is required. An integer variable needs to be declared as the subscript of the array. This variable helps in accessing individual elements of the
Arrays
39