Sunday, October 14, 2007

Arrays in "c"

The Indigenous Automobiles in Hyderabad is the largest car-manufacturing company. Its managing director wanted to find out how many cars were sold in the previous year. His idea was to assess the performance of the company and make future projections. He wanted the programmers at his sister concern, the Indigenous Software Solutions to generate the annual sales report for the previous year. He provided them with the following data.


Month Sales

1 100
2 150
3 127
4 246
… ….
… ….
11 193
12 200









If, you were a programmer at Indigenous Software Solutions, how would you go about this task?

To generate a report like the one you saw above, first of all you should store the data in the computer memory and then display it in the form of a table. There are two ways to do this.

In the first method, you will use 12 variables, say, sales1, sales2,…,sales12, store the values in them, and display the values. The code you write would be as shown below:


What would happen if there were so many variables! Wouldn’t it become tedious?

To overcome this problem C provides a data structure (a way of organizing data in the computer’s memory) called Array.

An array is a group of adjacent memory locations, referred to by a single name. This is used to store a set of similar data items. These similar data items could be all ints, floats, and chars, but not a combination of these.

Understand the concept of arrays with the help of this example.

Suppose there is a set of 5 books (B):

B={Computers, History, Math, Electronics, Physics}

If you want to refer to the History book in this set, how do you do it? You will say B2. Here the suffix 2 indicates that it is the second book in the set.

C uses a similar scheme to refer to individual elements of an array. This number called index or subscript is written inside square brackets ([]). The index number in C always begins with a zero (0).

Now, let us come back to our example of generating annual sales report. To store the 12 sales figures in an array you will write a statement as follows:

int sales[12];

This statement is called array declaration statement. When the C compiler sees this statement, it will reserve 12 adjacent memory locations as shown in the figure below.



sales
0 1 2 3 4 5 6 7 8 9 10 11

In this figure, sales refers to the name of the array, and the numbers 0 through 11 are referred to as index or subscript. You will use this number to refer to a particular memory location in the array. For example, if you want to store a number in the 1st cell of this array, you will write a statement sales[0]=120; similarly, if you want to store a number in the 8th cell, you will write sales[7]=300; and so on.

Storing and printing data in an array

One way to fill the sales figures in the array is to initialize the array just as other variables are initialized. This is very simple as you can see from the following statement.

int sales[12]={100, 150, 127, 246, 267, 298, 278,265, 255, 124, 193, 200};

However, you need not mention the array’s size. You could simply say:

int sales[ ]={100, 150, 127, 246, 267, 298, 278, 265, 255, 124, 193, 200};

NOTE: Till specific values are given, the array contains unpredictable values.


Another way of storing data in an array is to enter it using the keyboard while the program is running.

The program below shows you how to write the code that accepts data from the user and stores it in an array, while the program is being executed.

No comments: