Array is a
data structure in C++, which stores a fixed size sequential collection of
elements of the same type. An array is used to store a collection of data. It
is often useful to think of an array as a collection of variables of the same
type. Instead of declaring individual variables, such as number0, number1, ...,
and number99, you declare one array variable such as numbers and use
numbers[0], numbers[1], and ..., numbers[99] to represent individual variables.
A specific element in an array is accessed by an index. All arrays consist of
contiguous memory locations. The lowest address corresponds to the first
element and the highest address to the last element.
Declaring an
array in C, the programmer specifies the type of the elements and the
number of elements required by an array as follows:
type array-Name [
array-Size ];
This is called a single-dimension 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 4-element array called age of type
int, use following statement:
int num[4];
Array
initialization is just like a variable. To initialize an array, we provide
initializing values which are enclosed within curly braces in the declaration
and placed following an equals sign after the array name. Following is an
example of initializing an integer array at time of declaration.
int
num[4]={23,34,65,74};
Array can be initialized using for loop rather than at time of declaration. Following is an
example of initializing an array using for-loop.
int arr[10];
int i = 0;
for(i = 0; i < sizeof(arr);
i++)
{
arr[i] = i; // initializing an array }
To access an
array elements, loop can be used to print all or individual elements of an
array such as:
for(i = 0; i <
sizeof(arr); i++)
{
printf(“%d”,arr[i]); // initializing an array
}
Selection sort in
C arrange numbers of an array in ascending order. The idea of selection sort is
very simple: we repeatedly find the next largest element in the array and move
it to its final position in the sorted array. Assume that you wish to sort the array
in increasing order, i.e. the smallest element at the beginning of the array
and the largest element at the end. We begin by selecting the largest element
and moving it to the highest index position. We can do this by swapping the
element at the highest index and the largest element. We then reduce the effective size of the array by one
element and repeat the process on the smaller (sub) array. The process stops
when the effective size of the array becomes 1 (an array of 1 element is
already sorted).
Recursion is
also called as “circular definition”. Recursion are functions that call
themselves. It has a base case which means that beyond that point problem
cannot be solve further. Recursion solves the problem in parts and divides the
problem as:
•
What it can do
•
What it cannot do
•
The function launches a new copy of itself
(recursion step) to solve what it cannot do
Following is a factorial of a number using recursion
in which function calls itself again and again until base case appears as 1 in
case of factorial as shown below.
Comments
Post a Comment
if you have any confusion then ask hear.