Arrays New
Arrays New
Definition of Array: Array is the collection of similar data types or collection of similar entity
stored in contiguous memory location.
Types of Arrays:
1. Single Dimensional Array
2. Two Dimensional Array
3. Multi Dimensional Arrays
Initialization of Arrays:-
The different types of initializing arrays:
1. At Compile time
(i) Initializing all specified memory locations.
(ii) Partial array initialization
(iii) Initialization without size.
(iv) String initialization.
2. At Run Time
Ex:- int a[3]={9,2,4,5,6}; //error: no. of initial vales are more than the size of array
Because size of an array is 3 but we have initialized 5 values
(ii) Partial array initialization:- Partial array initialization is possible in c language. If the
number of values to be initialized is less than the size of the array, then the elements will be
initialized to zero automatically.
Ex:-
int a[5]={10,15};
Even though compiler allocates 5 memory locations, using this declaration statement; the
compiler initializes first two locations with 10 and 15, the next set of memory locations are
automatically initialized to 0's by compiler as shown in figure
(iii) Initialization without size:- Consider the declaration along with the initialization.
Ex:-
char b[]={'C','O','M','P','U','T','E','R'};
In this declaration, even though we have not specified exact number of elements to be used in
array b, the array size will be set of the total number of initial values specified. So, the array size
will be set to 8 automatically. The array b is initialized as shown in figure.
Ex:- int ch[]={1,0,3,5} // array size is 4
(iv) Array initialization with a string: -Consider the declaration with string initialization.
Ex:-
char b[]="COMPUTER";
The array b is initialized as shown in figure.
Even though the string "COMPUTER" contains 8 characters, because it is a string, it always ends
with null character. So, the array size is 9 bytes (i.e., string length 1 byte for null character).
Ex:-
char b[9]="COMPUTER"; // correct
char b[8]="COMPUTER"; // wrong
Two-dimensional Array:
An array consisting of two subscripts is known as two-dimensional array.
These are often known as array of the array.
In two dimensional arrays the array is divided into rows and columns.
These are well suited to handle a table of data.
In 2-D array we can declare an array as:
Declaration Syntax:
data_type array_name[row_size][column_size];
Ex: - int arr [3][3];
Where first index value shows the number of the rows and second index value shows the number
of the columns in the array.
While initializing we can group the elements row wise using inner braces.
for example:-
int mat[4][3]={{11,12,13},{14,15,16},{17,18,19},{20,21,22}};
And while initializing, it is necessary to mention the 2nd dimension where 1 st dimension is
optional.
int mat[][3];
int mat[2][3];
int mat[][];
int mat[2][]; invalid
Memory allocation for a 2D array:
Suppose we have an array named arr having 3 rows and 3 columns then it can be stored in the
memory in the following manner:
int arr[3][3];
Multidimensional arrays
are often known as array of the arrays. In multidimensional arrays the array is divided
into rows and columns, mainly while considering multidimensional arrays we will be discussing
mainly about two dimensional arrays and a bit about three dimensional arrays.
Syntax:
data_type array_name[size1][size2][size3]------[sizeN];
In 3-D we can declare the array in the following manner:
int arr[3][3][3] = { 1, 2, 3,
4, 5, 6,
7, 8, 9,
10, 11, 12,
13, 14, 15,
16, 17, 18,
void main( )
{
int a[10][10],b[10][10],c[10][10];
int i,j,m,n,p,q;
if(m!=p || n!=q)
{
printf("Matrix addition not possible.");
exit(0);
}
f(0)=0;
f(1)=1;
Series is as follows
011
(1+0)
2 (1+1)
3 (1+2)
5 (2+3)
8 (3+5)
13 (5+8)
21 (8+13)
34 (13+21)
...and so on
#include<stdio.h>
#include<conio.h>
void main()
{
//array fib stores numbers of Fibonacci series
int i, fib[25],n;
Linear search is also called as sequential search. All the elements need not be in sorted order
like binary search.
A simple approach is to do a linear search, i.e
Start from the leftmost element of arr[] and one by one compare x with each element of
arr[]
If x matches with an element, print position of the element
If x doesn’t match with any of elements, print element not found
#include <stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int a[50], i, item,n;
OUTPUT1:
Enter number of elements of an array:
8
Enter elements:
23578641
Enter item to search: 1
Item found at location 8
Output2:
Enter number of elements of an array:
8
Enter elements:
23578641
Enter item to search: 9
Item does not exist
Binary Search:
Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval
covering the whole array. If the value of the search key is less than the item in the middle of the
interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly
check until the value is found or the interval is empty.
Example:
/*C program to Search a key element using Binary search technique.*/
#include <stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
low = 0;
high = n - 1;
while (low <= high)
{
mid = (low+high)/2;
if(array[mid] == key )
{
printf("%d found at location %d.n", key, mid+1);
exit(0);
}
else if (array[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
printf("Not found! %d isn't present in the list.n", key);
getch();
}
Bubble Sort:
Bubble sort is a simple method that sorts the elements of an array into either increasing or
decreasing order. It works by comparing the adjacent elements and swapping them if they are out
of order. Multiple passes through the array are necessary.
The following are the steps to sort an array of size N in ascending order using bubble sort:
Passthrough #1:
Compare arr[0] with arr[1]. If arr[0] > arr[1], swap them.
Compare arr[1] with arr[2]. If arr[1] > arr[2], swap them.
Finally, compare a[N-2] with arr[N-1], if arr[N-2] > arr[N-1], swap them.
This completes the first passthrough.
After, the first passthrough, the highest value in the array will be at the end.
Passthrough #2:
Compare arr[0] with arr[1]. If arr[0] > arr[1], swap them.
Compare arr[1] with arr[2]. If arr[1] > arr[2], swap them.
Finally, compare a[N-3] with arr[N-2], if arr[N-3] > arr[N-2], swap them.
This completes the second passthrough. After, this passthrough, the second highest element will
be at the second highest index in the array.
Note that in the final step of the second pass through, we are not comparing the second last
element i.e a[N-2] with the last element i.e arr[N-1], this is because the last element is already in
its correct position.
Passthrough #3:
Compare arr[0] with arr[1]. If arr[0] > arr[1], swap them.
Compare arr[1] with arr[2]. If arr[1] > arr[2], swap them.
Finally, compare a[N-4] with arr[N-3], if arr[N-4] > arr[N-3], swap them.
This completes the third pass through. After, this pass through, the third highest element will be
at the third highest index in the array.
In this way, we keep passing through the array. We stop when we encountered a pass through
that's hasn't swapped any elements.
Example with Tracing: int arr[5] = {80, 60, 90, 10, 40}
Passthrough #1:
| 60 | 80 | 90 | 10 | 40 |
| 60 | 80 | 90 | 10 | 40 |
| 60 | 80 | 10 | 90 | 40 |
| 60 | 80 | 10 | 40 | 90 |
This completes the first pass through. The highest element i.e 90, is now at the end of the array.
We have made three swaps in this pass through. So, we need to perform another pass through.
Keep in mind that we keep passing through the array until we encounter a pass through that
hasn't swapped any elements.
Passthrough #2:
| 60 | 80 | 10 | 40 | 90 |
| 60 | 10 | 80 | 40 | 90 |
Step 3: Compare 80 and 40. Since, 80 > 40, swap them:
| 60 | 10 | 40 | 80 | 90 |
This completes the second pass through. The second highest element i.e 80, is now at the second
highest index in the array. Also, note that we haven't compared 80 with 90. This is because the
element 90 is already in its correct position from pass through #1.We have made 2 swaps in this
pass through. So, we need to perform another one.
Passthrough #3:
| 10 | 60 | 40 | 80 | 90 |
Step 2: Compare 60 and 40. Since, 60 > 40, swap them:
| 10 | 40 | 60 | 80 | 90 |
This completes the third pass through. The third highest element i.e 60, is now at the third
highest index in the array. Also, note that we haven't compared 60 with 80. This is because the
element 80 is already in its correct position from pass through #2.We have made 2 swaps in this
pass through. So, we need to perform another one.
Passthrough #4:
| 10 | 40 | 60 | 80 | 90 |
This completes the fourth pass through. We haven't made any swaps in this pass through. So, we
need don't need to perform another one. All the elements in the array are now sorted in ascending
order.
/* C program to sort array elements I n ascending order using
Bubble sort Technique*/
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[10], i, j, temp,n;
}
printf("\nSorted Array is:\n");
for(i=0; i<n; i++)
printf("%d\n ", arr[i]);
getch();
}
Selection Sort:
Selection sort is another algorithm that is used for sorting. This sorting algorithm, iterates
through the array and finds the smallest number in the array and swaps it with the first element if
it is smaller than the first element. Next, it goes on to the second element and so on until all
elements are sorted.
Example of Selection Sort:
Consider the array:
[10,5,2,1]
The first element is 10. The next part we must find the smallest number from the remaining
array. The smallest number from 5 2 and 1 is 1. So, we replace 10 by 1.
The new array is [1,5,2,10] Again, this process is repeated.
Finally, we get the sorted array as [1,2,5,10].
Let us continue with this article on Selection Sort in C and see how the algorithm works,
Algorithm for Selection Sort:
Step 1 − Set min to the first location
Step 2 − Search the minimum element in the array
Step 3 – swap the first location with the minimum value in the array
Step 4 – assign the second element as min.
Step 5 − Repeat the process until we get a sorted array.
Finally it prints the sorted array on the screen using for loop as given below:
The Sorted array in ascending order: 1 3 4 6 9