Arrays programming | Structured data types | One dimentional array | Sorting | Searching | Two-dimentional array | Multi dimentional array | Static memory allocation | Static arrays | Dynamic memory allocation | Dynamic arrays.
Arrays in C
So far we have used only the fundamental data types, namely char, int, float, double and variations of int and double. Although these types are very useful, they are 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 : derived data type known as array that can be used for such applications.
An array is a 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 a 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 employees in an organization.
- Test scores of a class of students.
- List of products and their cost sold by a store.
- List of customers and their telephone numbers.
- Arrays – Table of daily rainfall data.
Since an array provides a convenient structure for representing data, it is classified as one of the data structures in C. Other data structures include structures, lists, queues and trees. A complete discussion of all data structures is beyond the scope of this text. However, we shall consider structures in next Chapter.
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 or subscrirt 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 read the entire array, perform calculations, and print out the results.
We can use arrays to represent not only simple lists of values but also tables of data in two, three or more dimensions. In this chapter, we introduce the concept of an array and discuss how to use it to create and apply the following types of arrays.
- One-dimensional arrays
- Two-dimensional arrays
- Multidimensional arrays
Data Structures – Arrays Programming
C supports a rich set of derived and user-defined data types in addition to a variety of fundamental types as shown below:
- Arrays
- Functions
- Pointers
- Integral Types
- Float Types
- Character Types
- Structures
- Unions
- Enumerations
Arrays and structures are referred to as structured data types because they can be used to represent data values that have structure of some sort. Structured data types provide an organizational scheme that shows the relationships among the individual elements and facilitate efficient data manipulations. In programming parlance, such data types are known as data structures.
In addition to arrays and structures, ansi C supports creation and manipulation of the following data structures: Linked Lists, Stacks, Queues, Trees
One Dimensional Arrays Programming
A list of items can be given one variable name using only one subscript and such a variable is called a single subscripted. To calculate the average of n values of x. The subscripted variable xi refers to the ith element of x. In c, single subscripted variable xi, can be expressed as x[1],x[2],x[3],…….x[n]
The subscripts of an array can be integer constants, integer variables like i, or expressions that yield integers. C performs no bounds checking and, therefore, care should be exercised to ensure that the array indices are within the declared limits.
Declaration of One Dimensional Arrays Programming
Like any other variable, arrays must be declared before they are used so that the compiler can allocated 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 cintaining 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 constants.
- 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 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. Suppose we read the following string constant into the string name. “WELL DONE”
Program main() { int i ; float x[10], value, total ; /*...... READING VALUES INTO ARRAY.....*/ printf(ENTER 10 REAL NUMBERS\n") ; for( i = 0 ; i < 10 ; i++ ) { scanf("%f", &value) ; x[i] = value ; } /*.....COMPUTATION OF TOTAL .....*/ total = 0.0 ; for(i = 0 ; i < 10 ; i++ ) total = total + x[i] * x[i] ; /*....PRINTING OF x[i] VALUES AND TOTAL....*/ printf("\n"); for( i = 0 ; i < 10 ; i++ ) printf("x[%2d] = %5.2f/n", i+1, x[i]) ; printf("\ntotal = %.2f\n", total) ; } Output ENTER 10 REAL NUMBERS 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 10.10 x[ 1] = 1.10 x[ 2] = 2.20 x[ 3] = 3.30 x[ 4] = 4.40 x[ 5] = 5.50 x[ 6] = 6.60 x[ 7] = 7.70 x[ 8] = 8.80 x[ 9] = 9.90 x[10] = 10.10 Total = 446.86
Initialization of One Dimensional Arrays
After an array is declared, its elements must be initialized. Otherwise, they will contain “garbage”, An array can be initialized at either of the following stages:
- At compile time
- At run time
Compile Time Initialization
We can initialize the elements of arrays in the same way as the ordinary variables when they are declared. The general form of initialization of arrays is: type array-name[size] = {list of values }; the values in the list are separated by commas. for example, the statement int number [3] = { 0,0,0 }; will declare the variable number as an array of size 3 and will assign zero to each element. If the number of values in the list is less than the number of elements, then only that many elements will be initialized. The remaining elements will be set to zero automatically.
float total[5] = {0.0,15.75,-10}; Will initialize the first three elements to 0.0, 15.75, and -10.0 and the remaining two elements to zero. The size may be omitted. In such cases, the compiler allocates enough space for all initialized elements.
int counter[] = {1,1,1,1}; will declare the counter array to contain four elements with initial values 1. This approach works fine as long as we initialize every element in the array. Character arrays may be initialized in a similar manner. Thus, the statement char name [ ] = {‘J’,’o’, ‘h’, ‘n’, ‘\10’}; however, if we have more initializers than the declared size, the compiler will produce an error. That is, the statement int number [3] = {10, 20, 30, 40} ; will not work. It is illegal in C.
Searching and Sorting
Searching and sorting are the two most frequent operations performed on arrays. Computer scientists have devised several data structures and searching and sorting techniques that facilitate rapid access to data stored in lists.
Sorting is the process of arranging elements in the list according to their values, in ascending or descending order. A sorted list is called an ordered list. Sorted lists are especially important in list searching because they facilitate rapid search operations. Many sorting techniques are available.
- Bubble sort
- Selection sort
- Insertion sort
Other sorting techniques include Shell sort, Merge sort and Quick sort.
Searching is the process of finding the location of the specified element in a list. The specified element is often called the search key. If the process of searching finds a match of the search key with a list element value, the search said to be successful; otherwise, it is unsuccessful. The two most commonly used search techniques are: Arrays Programming
- Sequential search
- Binary search
A detailed discussion on these techniques is beyond the scope of this text. Consult any good book on data structures and algorithms.
Read More Topics |
Wireless LANs |
The Point-to-Point Protocol (PPP) |
Carrier Sense Multiple Access with Collision Avoidance |
ALOHA in Computer Network |
Interactions Between IPv4 and the Datalink Layer |