An array is a collection of data items, all of the same type, accessed using a common name. A one-dimensional array is like a list; A two dimensional array is like a table; The C language places no limits on the number of dimensions in an array, though specific implementations may.
1. ARRAY TECHNIQUES
Provides us with a very simple way of referring to and performing
computations on collection of data that share some common attribute
Array name and suffix.
A one-dimensional array.
Perform the same computations on collections of data.
Deep Prajapati
8866188126
2. ARRAY ORDER REVERSAL
Problem:
Rearrange the element in an array so that they appear in reverse order.
Algorithm Description:
Establish the array of [1,…n] of n element to be reversed.
Compute r the number of exchanges needed to be reverse the
array.
While there are still pairs of array element to be exchanged
(a) Exchange the ith
element with the [n-i+1]th
element.
Return the reversed array.
3. ARRAY COUNTING OR HISTOGRAMING
Problem :
Given a set of n students’ examination marks (in the range 0 to 100 )
make count of the number of student that obtained each
possible mark.
Algorithm Description :
Prompt and read in n the number of marks to be processed.
Initialize all elements of the counting array a(0,..100) to zero.
While there are still marks to be processed, repeatedly do
(a) Read next mark m,
(b) Add one to the count in location m in the counting array.
Write out the marks frequency count distribution.
4. FINDING THE MAXIMUM NUMBER IN A SET
Problem :
Find the maximum number in a set of n numbers.
Algorithm Description :
Establish an array a[1,…n] of n elements where n>=1.
Set temporary maximum max to first array element.
While less than n array elements have been considered do.
(a) If next element greater than current maximum max then assign
to max.
Return maximum max for the array of n elements.
5. REMOVAL OF DUPLICATES FROM AN ORDERED
ARRAY
Problem :
Remove all duplicates from an ordered array and construct the array
accordingly.
Algorithm description :
Establish the array a[1…n] of n elements.
Set loop index I to 2 allow correct termination.
Compare successive pairs of elements until a duplicate is
encountered then set unique element count j.
While all pairs have not been examined do
(a) If next pair not duplicate then
(a.1) add one to unique element count j.
(a.2) move later element of pairs to array position determined by the
unique element count j.
6. PARTITIONING AN ARRAY
Problem :
Given a randomly ordered array. of n elements, partition the elements
into two subset such that elements <=x are in one subset and
elements >x are in the other subset.
Algorithm Description :
Establish the array a[1…n] and the partitioning value x.
Move the two partitions towards each other until a wrongly
placed pair of elements is encountered. Allow for special cases of
x being outside the range of array values.
While the two partitions have not met or crossed over do
(a) Exchange the wrongly partitioned pair and extend both partitions
inwards by one element;
(b) Extend left partition while elements less than or equal to x;
(c) Extend the right partition while elements are greater than x.
Return the partitioning index p and the partitioned array.
7. FINDING THE kth
SMALLEST ELEMENT
Problem :
Given a randomly ordered array of n elements determine the kth
smallest element in the
set.
Algorithm Description :
Establish a[1…n] and the requirement that the kth
smallest element is sought.
While the left and right partitions do not overlap do
(a) Choose a[k] as the current partitioning value x;
(b) Set I to the upper limit l of the left partition;
(c) Set j to the lower limit u of of the right partition;
(d) While I has not advanced beyond k and j is greater than or equal to k do
(d.1) extend the left partition while a[I]<x;
(d.2) extend the right partition while z<a[j];
(d.3) exchange of [I] and reduce j by 1;
(e) If kth
smallest in right partition, update upper limit u of left partition;
(f) If kth
smallest in right partition update lower limit l of right partition.
(3) Return the partitioned array with elements <=a[k] in the first k position in the array.
8. ARRAYS
Introduction :
An array is a group of related data items that share a common name.
Declaration of Arrays :
Type variable-name[size];
The maximum number of elements that can be stored inside the array.
An array to contain a maximum of 10 integer constants.
When declaring character arrays. We must always allow one extra
element space for the null terminator.
9. INITIALIZATION OF ARRAYS
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
array is
Static type array-name [size] = {list of values};
The values in the list are separated by commas. For example, the
statement
Static float total[5] {0,0,0};
There is no convenient way to initialize only selected elements.
There is no shortcut method for initializing a large number of array
elements like the one available in FORTRAN.
10. TWO DIMENSIONALARRAYS
So far we have discussed the array variable that can store a list of values.
There will be situation where a table of values will have to be stores.
Considered the following data table, which shows the value of sales of
three items by four sales girls;
Two-dimensional arrays arrays declared as follows:-
Type array_name [row_size][column_size];
Note that unlike most other languages, which use one pair of parentheses
with commas to separate sizes, C places each size in its own set of
brackets.
11. INITIALIZING TWO-DIMENSIONALARRAYS
Static int table[2][3] = {0,0,0,1,1,1};
Initializes the elements of the first row to zero and the second row to
one. The initialization is done row by row. The above statement can be
equivalently written as
Static int table[2][3] = {{0,0,0}, {1,1,1};
By surrounding the elements of each row by braces.
We can also initialize a two-dimensional array in the form of a matrix as
shown below:
Static int table[2][3] = {
{0,0,0}
{1,1,1}
};