Saturday, August 15, 2020

Arrays in C language

So far we have used only the fundamental data types,namely char,int, float,double, and variations of the int and double.Although these types are very useful,they constrained by the fact that a variable of these types can store only one value at any given time. Therefore,they can be used only to handle limited amounts of data.In many applications,however,we need to handle a large volume of data in terms of reading,processing and printing. To process such large amounts of data, we need a powerful data type that would facilitate efficient storing.accessing and manipulation of data items. C supports a derived data type known as array that can ve used for such applications.

An Array is fixed-size sequenced collection of elements of the same data type. It is simply a grouping of like-type data. In its simplest form, an array can be used to represent a list of numbers, or list of names. Some examples where the concept of an array can be used:

·         List of temperatures recorded every hour in a day, or a month, or a year.

·         List of employess in an organization .

·         List of products and their cost sold by a store.

·         Test scores of a class of students.

·         List of customers and their telephone numbers.

·         Table of daily rainfall data.

and so on.

As we mentioned earlier, an array is a sequenced collection of related data items that share a common name. For instance, we can use an array name salary to represent a set of salaries of a group of employees in an organization. We can refer to the individual salaries by writing a number called index of  subscript in brackets after the array name. For example,

                  salary [10]

represents the salary of 10th employee. While the complete set of values is referred to as an array, individual values are called elements.

The ability to use a single name to represent a collection of items and to refer to an item by specifying the item number enables us to develop concise and efficient programs.For example,we can use a loop construct,discussed earlier, with the subscript as the control variable to readthe entire array,perform calculations, and print out results.

We can use arrays to represent not only simple lists of values but also tables of data in two, three or more dimensions. The following types of arrays:

·       1.  One-Dimensional Arrays.

·       2.  Two-Dimensional Arrays.   

·      3. Multi-Dimensional Arrays  

One-Dimensional Arrays  

A list of items may be given one variable name , and the individual elements may be accessed by the specification of their o their relative positions with respect to the start of the list.Like any other variable,arrays must be declared before they are used so that the compiler can allocate space for them in memory. The general form of array declaration is

        Type variable-name[size];

The type specifies the type of element that will be contained in the array,such as int,float or char and the size indicates the maximum number of elements that can be stored inside the array. For example,

        Float height[50];

declares the height to be an array containing 50 real elements. Any subscripts 0 to 49 are valid. Similarly,

         int group[10];

declares the group as an array to contain a maximum of 10 integer constansts. Remember:

·         Any reference to the arrays outside the declared limits would not necessarily cause an error. Rather, it might result in unpredictable program results.

·         The size should be either a numeric constant or a symbolic constant.

The C language treats character strings simply as arrays of characters. The size in a character string represents the maximum number of characters that the string can hold.For instance,

            char name[10];

declares the name as a character array(string) variable that can hold a maximum of 10 characters.      

Two-Dimensional Arrays

There could be situations where a table of values will have to be stored. Consider the following data table, which shows the value of sales of three items by four sales man:

 

Item 1

     Item 2

  Item 3

Salesman#1

       310

      275

  365

Salesman#2

       210

      190

  325

Salesman#3

       405

      235

  240

Salesman#4

       260

      300

  380

 The table contains a total of 12 values,three in each line. We can think of this table as a matrix consisting of four rows and three columns. Each row represents the values of sales by a particular salesgirl and each column represents the values of sales of a particular item.

Two-Dimensional arrays are declared as follows:

      type array_name[row_size][column_size]


No comments:

Post a Comment

Detail of Software and Hardware

  Topic   There are available only two things in computer system one is software and another is hardware.   Both is opposite of each other...