Download document

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

C Sharp (programming language) wikipedia , lookup

Go (programming language) wikipedia , lookup

Lua (programming language) wikipedia , lookup

Falcon (programming language) wikipedia , lookup

C Sharp syntax wikipedia , lookup

APL syntax and symbols wikipedia , lookup

Covariance and contravariance (computer science) wikipedia , lookup

Block sort wikipedia , lookup

Radix sort wikipedia , lookup

Sorting algorithm wikipedia , lookup

Selection algorithm wikipedia , lookup

Fisher–Yates shuffle wikipedia , lookup

Binary search algorithm wikipedia , lookup

Quicksort wikipedia , lookup

C syntax wikipedia , lookup

Array data structure wikipedia , lookup

Transcript
Arrays in C :
Overview :
In C programming, one of the frequently arising problem is to handle similar types of data. C
programming language provides a data structure called the array, which can store a fixed-size sequential
collection of elements of the same type. An array is used to store a collection of data, but it is often more
useful to think of an array as a collection of variables of the same type. All arrays consist of contiguous
memory locations. The lowest address corresponds to the first element and the highest address to the
last element.
Description :
Arrays in C act to store related data under a single variable name with an index, also known as a subscript. It
is easiest to think of an array as simply a list or ordered grouping for variables of the same type. As such,
arrays often help a programmer organize collections of data efficiently and intuitively.
Arrays are of two types:
1. One-dimensional arrays
2. Multidimensional arrays
10.1 Declaration of an Array :
To declare an array in C, it is necessary to specify the type of the element and the number of elements
required by an array as follows:
Type array_name [ array_size ];
This is called a single-dimensional or one dimensional array. The array_size must be an integer constant
greater than zero and type can be any valid C data type. For example, to declare a 10-element array called
Name of type char, use this statement:
char Name[10];
Now the character array is sufficient to hold upto 10 characters or integers. An element is accessed by
indexing the array name. This is done by placing the index of the element within square brackets after the
name of the array.
10.2 Initializing an Array :
Array initialization can be done in one of the following ways :
1) The number of values between braces { } cannot be greater than the number of elements that is declared
for the array between square brackets [ ] i.e the size of the array.
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
E.g : int Array[5] = {1,2,5,10,-3};
Consider the given program to initialize an array :
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
int array[5] = { 1, 3, -2, 35, 100};
return 0;
}
The above program shows another method to initialize the elements in an array. Here, the elements of array
are initialized during the declaration process by placing values between curly braces and separating them
using commas.
2) If the size of the array is omitted, an array just big enough to hold the initialization is created. Therefore, if
the user writes:
E.g : int Array[] = {1,2,5,10,-3};
An array similar to the one mentioned above is created with the declared elements. This is very useful in that
the size of the array can be controlled by simply adding or removing initialized elements from the definition
without the need to adjust the dimension. If the dimension is specified, but not all elements in the array are
initialized, the remaining elements will contain a value of 0. This is very useful, especially when we have very
large arrays.
3) It is also possible to assign a value to a single element of the array by specifying the array index.
E.g : Array[3]=100;
Array[0]=10;
Consider the given example :
int _tmain(int argc, _TCHAR* argv[])
{
int array[5];
array[0] = 10;
array[1] = 20;
array[2] = 30;
array[3] = 40;
array[4] = 50;
return 0;
}
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
The above program shows one method to initialize the elements in an array. Here, the elements of array are
individually initialized with values by specifying the array index as given above.
Consider the given example to sort an array of elements :
Figure 10.1
The above figure 10.3 shows an example to sort an array of 5 elements using the bubble sort technique. The
program shows the transition of the first iteration in the watch window. Since the first element of the array is
greater than the second element, we use a temporary variable to swap the two values.
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
Figure 10.2
The above figure 10.4 shows an example to sort an array of 5 elements using the bubble sort technique. The
program shows the output of the first iteration of the sorting process in the watch window. The first and the
second values of the array have been swapped and the second iteration is started which checks the next two
elements in the array. In this way, all the elements in the array are sorted in the required ascending order.
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)