Arrays



Arrays and Strings

6.1 Introduction

Consider a program that allows the user to enter the temperature for each day of a week and then prints the average temperature of that week.

#include

int main(void)

{

float SunTemp;

float MonTemp;

float TueTemp;

float WedTemp;

float ThuTemp;

float FriTemp;

float SatTemp;

float Average;

printf("Enter temperature for each day of the week of the day ");

printf("separated by space :: ");

scanf("%f %f %f %f %f %f %f",&SunTemp,&MonTemp,&TueTemp,&WedTemp,

&ThuTemp,&FriTemp,&SatTemp);

getchar();

Average= (SunTemp+MonTemp+TueTemp+WedTemp+ThuTemp+FriTemp+SatTemp)/7.0;

printf("Average temperature for the week :: %7.2f",Average);

return(0);

}

This program uses a collection of similar data elements (of data type float) where each data element has been given a unique variable name. If the number of data elements increase (e.g., to modify the program to provide the average temperature of a specific month), the programming effort increases significantly as the programmer has to declare individual data elements to store the temperature value for each day of the month.

If a collection of homogeneous values (i.e., of the same type) are to be used in a program, these are usually stored in arrays. More specifically, an array is a series of variables that share the same basic name and are distinguished from one another by a numerical tag (also known as the array index). To access or manipulate a specific variable in the array, the programmer has to specify the name of the array and the index value representing the position of the variable in the array. An interesting aspect of using arrays is that the index can also be an expression (i.e., a variable, constant or a combination of variables, constants and operators) with an integer value. This makes it convenient for the programmer to write powerful array access routines.

6.2 Defining One Dimensional Arrays

An array is a collection of variables of a certain type, placed contiguously in memory. Like other variables, the array needs to be defined, so the compiler knows what is the type of the array elements and how many elements are contained in that array. A declaration for an array is given below,

float Temperature[7];

Here, the float specifies the type of the variables in the array, just as it does with simple variables, and the word Temperature is the name of the array. In [7], the number tells how many variables of type float are contained in this array. Each of the separate variables in the array is called an element. The brackets tell the compiler that the variable being declared is actually an array. Figure 6-1 shows the array.

[pic]

Figure 6-1 : Representation of float Temperature[7];

6.3 Using Arrays in Programs

Once the array has been declared, its individual elements may be accessed with subscripts or indexes. These are the numbers in brackets following the array name. Note, however, that this number has a different meaning when referring to an array element than it does when defining the array, when the number in brackets is the size of the array. When referring to an array element, this number specifies the element’s position in the array. All the array elements are numbered or indexed starting at 0. The element of the array Temperture with the index 2 would be referred to as Temperature[2]. Note that, because the numbering starts with 0, this is not the second element of the array, but the third. Thus, the index of the last array element is one less than the size of the array. This is shown in figure 6-2.

[pic]

Figure 6-2 : Referencing Individual Array Elements

As mentioned earlier, the array subscript can be an integer constant, variable or an expression with an integral value.

The following program uses an array to store the temperature of every day of the week entered by the user. Then it calculates the average temperature for the week and prints it for the user.

#include

int main(void)

{

float Temperature[7];

float Average;

float Sum = 0.0;

int DayOfWeek;

for (DayOfWeek=0;DayOfWeek ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download