Thursday, 27 December 2018

ARRAYS


Use of Arrays

  • Storing more than one value at a time under a single name.
  • Reading, processing and displaying the array elements is far easier

ARRAYS


Arrays are variables that are capable of having more than one value at a time. “An array is a collection of homogenous elements of same data type stored in adjacent locations.” The same data type could be all ints (or) floats (or) chars etc., An array is an user-defined data type

Syntax: datatype array_name[size];

size is the maximum number of elements that can be stored in the array.
Ex:     int a[100]
          Float rates[5];
          Char name[20];

Arrays are of 3 types:-
  1. One Dimensional Array
  2. Two Dimensional Array
  3. Multi-Dimensional Array

Declaration of 1-D Array

Ex: int a[10];

The above statement tells the compiler to reserve sufficient amount of space to store 10 integers and specify a name ‘a’ to the entire collection.

Initialization of 1-D Array:


Like a variable an array can also be initialized at the time of declaration.
int num[5]={1,2,3,4,5};
to access the number 3 we can simply write a[2]
int digits[ ] = {1,2,3,4,5,6,7,8,9,10};
char name[5]={‘e’,’i’,’o’,’u’,’a’};
float rate[3]={20.5,34.5,67.9};

//program to accept and display the array
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i;
printf("How many nos do u want to enter:");
scanf("%d",&n);                         //size of the array
printf("Enter the Numbers:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);                      //reading elements into the array
printf("The elements in the array are...");
for(i=0;i<n;i++)
printf("%d\t",a[i]);                     //displaying the elements in the array
getch();       
}
Output:
How many nos do u want to enter:5
Enter the Numbers:1
2
3
4
5
The elements in the array are...1       2       3       4       5

//Program to read and display the array in reverse
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i;
printf("How many nos do u want to enter:");
scanf("%d",&n);                         //size of the array
printf("Enter the Numbers:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);                      //reading elements into the array
printf("The elements in the array are...");
for(i=n-1;i>=0;i--)
printf("%d\t",a[i]);                     //displaying the elements in the array in reverse order
getch();       
}
Output:
How many nos do u want to enter:5
Enter the Numbers:1
2
3
4
5
The elements in the array are...5       4       3       2       1

//Program to find the sum of array elements
#include<stdio.h>
#include<conio.h>
void main()
{
          int a[10],sum=0,n,i;
          clrscr();
          printf("Enter the size of array:");
          scanf("%d",&n);
          printf("\nEnter the elements into the array:");
          for(i=0;i<n;i++)
          scanf("%d",&a[i]);
          for(i=0;i<n;i++)
          sum=sum+a[i];
          printf("\nThe sum of the array elements are...%d",sum);
          getch();
}
Output:
Enter the size of array:5
Enter the elements into the array:1 2 3 4 5
The sum of the array elements are...15


//Program for Bubble Sort
#include<stdio.h>
void main()
{
          int a[10],n,i,j,t;
          clrscr();
          printf("How many numbers:");
          scanf("%d",&n);
          printf("Enter the numbers :");
          for(i=0;i<n;i++)
          scanf("%d",&a[i]);
          printf("The sorted list ...");
          for(i=0;i<n-1;i++)                       //for ‘n’ elements n-1 passes
          {
                   for(j=0;j<n-1-i;j++)           //and each time n-1-I comparisons
                   {
                             if(a[j]>a[j+1])
                             {
                                      t=a[j];
                                      a[j]=a[j+1];
                                      a[j+1]=t;
                             }
                   }
          }
          for(i=0;i<n;i++)
          printf("\n\t\t%d",a[i]);
          getch();
}
Output:
How many numbers:5
Enter the numbers :9 0 2 1 7
The sorted list ...     0       1       2       7       9

Searching:

Searching is a technique which searches for the given element in the array. Ex: Google Search Engine, Searching for a file, etc are based on this concept. Searching’s are of two types namely:
  1. Linear Search
  2. Binary Search

Linear Search:

This technique searches for the element sequentially i.e., it searches for the search element starting from the first element in the array. If the search values do not match with the first element in the array then the search element is compared with the second element in the array. If the values still do not match then the required element is compared with the third element in the list. This process continues, until the required element is found or the end of the list is reached.
Ex: Searching for the file starting with the letter ‘s’, searching for files having extensions like .doc,.xls etc.

//program for linear search
#include<stdio.h>
void main()
{
          int a[10],n,i,search,flag=0;
          clrscr();
          printf("How many numbers:");
          scanf("%d",&n);
          printf("\nEnter the numbers :");
          for(i=0;i<n;i++)
          scanf("%d",&a[i]);
          printf("\nEnter the element to be searched:");
          scanf("%d",&search);                 //reading search element
          for(i=0;i<n;i++)
          {
if(a[i]==search)        // comparing search element to the
                   {                              //element in the array list.
                             flag=1;
                             break;                   //if element is found make flag=1
                   }
          }
          if(flag==1)                                 //if flag=1 it means element found
          printf("\nElement found at position %d",i+1);
          else                                          //i+! is the element position
          printf("\nElement Not Found");
          getch();
}

Output:
How many numbers: 5
Enter the numbers: 2 8 4 7 1
Enter the element to be searched: 4
Element found at position 3

Output 2:
How many numbers: 5
Enter the numbers: 2 8 4 7 1
Enter the element to be searched: 6
Element Not Found

Explanation:
Initially flag=0
Elements in the array:
1        8        9        4        5
a[0]  a[1]    a[2]    a[3]     a[4]

Assume Search element as 9
First compare 9 with a[0] i.e., 1 (not equal)
Next compare 9 with a[1] i.e., 8  (not equal)
Next compare 9 with a[2] i.e., 9  (equal)
Now assign flag=1 and break the loop and check whether flag=1 or not if yes print the message “element found”.
Assume Search element as 6
First compare 6 with a[0] i.e., 1 (not equal)
Next compare 6 with a[1] i.e., 8  (not equal)
Next compare 6 with a[2] i.e., 9  (not equal)
Next compare 6 with a[3] i.e., 4  (not equal)
Next compare 6 with a[4] i.e., 5  (not equal)
Now it reaches to the end of the list so the flag value will not change it remains 0 so it prints “element not found”

Binary Search:
In this search, to search the element first the list of elements should be in the sorted order.  The searching is done by finding the middle element in the array. If the middle element is equal to the search element the procedure stops.  Otherwise it splits the list into 2 halves, if search element is greater than the middle element then it search in the lower list, if the search element is less than the middle element then it search in the upper list. This procedure repeats until the search element is found.
For Ex: Searching for a word in a dictionary comes under this concept. Suppose we want to search the meaning for knowledge we directly move to the page containing k letter and then to the letters kn and so on. The words will be in the sorted order in the dictionary. So we can directly move to that page that containing the alphabet.
Ex-2:  Telephone Directory, etc

//Program for Binary Search
#include<stdio.h>
#include<conio.h>
void main()
{
   int a[10],flag=0,i,n,top,mid,bottom,key;
   clrscr();
   printf("\nEnter size of the array:");
   scanf("%d",&n);
   printf("\nEnter elements into the array in sorted order:");
   for(i=0;i<n;i++)
   scanf("%d",&a[i]);
   printf("\nEnter element to be searched:");
   scanf("%d",&key);
   top = 0;
   bottom = n-1;
   while(top <= bottom && flag == 0)
   {
       mid = (top + bottom) /2 ;
       if(a[mid] == key)
            flag = 1;
          else if(key>a[mid])
             top = mid + 1;
          else
             bottom = mid - 1;
    }
    if(flag == 1)
       printf("\nSearch element %d found at position %d",key,mid+1);
    else
       printf("\nSearch element %d is not found.",key);
}

Output
Enter size of the array: 5
Enter elements into the array in sorted order: 10  20  30  40  50
Enter element to be searched: 40
Search element 40 found at position 4

Now search element is 10

top


mid



bottom
2
5
6
7
8
9
10
11
0
1
2
3
4
5
6
7
Key=10
Top=0;bottom=n-1=7
Mid=top+bottom/2=0+7/2=3
A[3]=7
key>a[3]       so, top=mid+1 i.e., it breaks the list into 2 now search will be in the bottom part of the list as key > mid element.






top
mid

bottom
2
5
6
7
8
9
10
11
0
1
2
3
4
5
6
7
Key=10
Top=4;bottom=n-1=7
Mid=top+bottom/2=4+7/2=5
A[5]=9
key>a[5]       so, top=mid+1 i.e., it breaks the list again into 2 now search will be in the bottom part of the list as key > mid element.






top
mid
bottom
2
5
6
7
8
9
10
11
0
1
2
3
4
5
6
7
Key=10
Top=5;bottom=n-1=7
Mid=top+bottom/2=5+7/2=6
A[6]=10   // a[mid] is equal to key so                                                                      search found
Now search element is 12

top


mid



bottom
2
5
6
7
8
9
10
11
0
1
2
3
4
5
6
7
Key=12
Top=0;bottom=n-1=7
Mid=top+bottom/2=0+7/2=3
A[3]=7
key>a[3]       so, top=mid+1 i.e., it breaks the list into 2 now search will be in the bottom part of the list as key > mid element.






top
A[mid]

bottom
2
5
6
7
8
9
10
11
0
1
2
3
4
5
6
7
Key=12
Top=4;bottom=n-1=7
Mid=top+bottom/2=4+7/2=5
A[5]=9
key>a[5]       so, top=mid+1 i.e., it breaks the list again into 2 now search will be in the bottom part of the list as key > mid element.






top
A[mid]
bottom
2
5
6
7
8
9
10
11
0
1
2
3
4
5
6
7
Key=12
Top=5;bottom=n-1=7
Mid=top+bottom/2=5+7/2=6
A[6]=10  
key>a[6]       so, top=mid+1 i.e., it breaks the list again into 2 now search will be in the bottom part of the list as key > mid element.







top
bottom
2
5
6
7
8
9
10
11
0
1
2
3
4
5
6
7
Key=12
Top=6;bottom=n-1=7
Mid=top+bottom/2=6+7/2=6
A[6]=10  
key>a[6]       so, top=mid+1 i.e., it breaks the list again into 2 now search will be in the bottom part of the list as key > mid element.








Top,bottom
2
5
6
7
8
9
10
11
0
1
2
3
4
5
6
7
Key=12
Top=7;bottom=n-1=7
Mid=top+bottom/2=7+7/2=7
A[7]=11  
key>a[6]       so, top=mid+1 i.e., it breaks the list again into 2 now search will be in the bottom part of the list as key > mid element.








Top,bottom
2
5
6
7
8
9
10
11
0
1
2
3
4
5
6
7
Key=12
Top=8;bottom=n-1=7 as top>bottom it stops searching and prints element not found  

Two-Dimensional Array

A 2-D array is nothing but a collection of 1-D Array. 2-D arrays are mainly used when values are to be stored in the table form or to represent in rows and columns format. 2-D arrays are used to represent any data in table or rows and columns format.

Declaration of 2-D Array

Syntax:        type arrayname[row size][col size];
int m[5][4];

Here m is an array variable which has 5 rows and 4 columns. Here 40 bytes (20 integers) gets allocated to ‘m’.



//Program for reading & printing a 2-D array
#include<stdio.h>
#include<conio.h>
void main()
{
 int a[10][10],m,n,i,j;
 printf("Enter the size of matrix:");
 scanf("%d%d",&m,&n);
 printf("\nEnter the elements into array:");
 for(i=0;i<m;i++)
 for(j=0;j<n;j++)
 scanf("%d",&a[i][j]);
 printf("\nElements in the array are:\n");
 for(i=0;i<m;i++)
 {
 for(j=0;j<n;j++)
 {
 printf("%3d",a[i][j]);
 }
 printf("\n");
 }
 getch();
 }
Output
Enter the size of matrix:2 2
Enter the elements into array:1 2 3 4
Elements in the array are:
  1  2
  3  4

//sum of 2 matrices
#include<stdio.h>
#include<conio.h>
void main()
{
          int a[5][5],b[5][5],c[5][5],i,j,m,n,p,q;
          clrscr();
          printf("Enter the size of matrix A:");
          scanf("%d%d",&m,&n);
          printf("\nEnter the elements into matrix A:");
          for(i=0;i<m;i++)
          for(j=0;j<n;j++)
          scanf("%d",&a[i][j]);
          printf("\nEnter the size of matrix B:");
          scanf("%d%d",&p,&q);
          printf("\nEnter the elements into matrix B:");
          for(i=0;i<p;i++)
          for(j=0;j<q;j++)
          scanf("%d",&b[i][j]);
          if((m!=p) && (n!=q))
                   printf("\nMatrix Addition is not possible");
          else
          {
                   for(i=0;i<m;i++)
                   {
                             for(j=0;j<n;j++)
                             {
                                      c[i][j]=a[i][j]+b[i][j];
                             }
                   }
                   printf("\nAddition of 2 matrices are...\n");
                   for(i=0;i<m;i++)
                   {
                             for(j=0;j<n;j++)
                             {
                                      printf("%3d",c[i][j]);
                             }
                             printf("\n");
                   }
          }
          getch();
}
Output:
Enter the size of matrix A: 3 3
Enter elements into matrix-A:
1 2 3 4 5 6 7 8 9
Enter the size of matrix B: 3 3
Enter elements into matrix-B:
1 2 3 4 5 6 7 8 9
Addition of 2 matrices are...
   2    4    6
   8  10  12
 14  16  18

//multiplication of 2 matrices
#include<stdio.h>
#include<conio.h>
void main()
{
          int a[5][5],b[5][5],c[5][5],k,i,j,m,n,p,q;
          clrscr();
          printf("Enter the size of matrix A:");
          scanf("%d%d",&m,&n);
          printf("\nEnter the elements into matrix A:");
          for(i=0;i<m;i++)
          for(j=0;j<n;j++)
          scanf("%d",&a[i][j]);
          printf("\nEnter the size of matrix B:");
          scanf("%d%d",&p,&q);
          printf("\nEnter the elements into matrix B:");
          for(i=0;i<p;i++)
          for(j=0;j<q;j++)
          scanf("%d",&b[i][j]);
          if((m!=q) && (n!=p))
                   printf("\nMatrix Multiplication is not possible");
          else
          {
                   for(i=0;i<m;i++)
                   {
                             c[i][j]=0;
                             for(j=0;j<p;j++)
                             {
                                      for(k=0;k<n;k++)
                                      {
                                      c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
                                      }
                             }
                   }
                   printf("\nMultiplication of 2 matrices are...\n");
                   for(i=0;i<m;i++)
                   {
                             for(j=0;j<n;j++)
                             {
                                      printf("%3d",c[i][j]);
                             }
                             printf("\n");
                   }
          }
          getch();
}
Output
Enter the size of matrix A: 3 3
Enter elements into matrix-A:
1 2 3 4 5 6 7 8 9
Enter the size of matrix B: 3 3
Enter elements into matrix-B:
1 2 3 4 5 6 7 8 9
Multiplication of 2 matrices are...
         30     36     42
         66     81     96
       102   126   150

//transpose matrix
#include<stdio.h>
#include<conio.h>
void main()
{
          int a[5][5],b[5][5],i,j,m,n,p,q;
          clrscr();
          printf("Enter the size of matrix A:");
          scanf("%d%d",&m,&n);
          printf("\nEnter the elements into matrix A:");
          for(i=0;i<m;i++)
          for(j=0;j<n;j++)
          scanf("%d",&a[i][j]);
          printf("\nOriginal Matrix is...\n");
          for(i=0;i<m;i++)
          {
                   for(j=0;j<n;j++)
                   {
                             printf("%3d",a[i][j]);
                   }
                   printf("\n");
          }
          printf("\nTranspose of matrices is...\n");
          for(i=0;i<m;i++)
          {
                   for(j=0;j<n;j++)
                   {
                             b[i][j]=a[j][i];
                             printf("%3d",b[i][j]);
                   }
                   printf("\n");
          }
          getch();
}
Output:
Enter the size of matrix A: 4 3
Enter the elements into matrix A: 1 2 3 4 5 6 7 8 9 10 11 12
Original Matrix is:
1           2     3
4      5     6
7      8     9
10  11   12
Transpose of matrices is:
     1      4     7   10
     2      5     8   11
     3      6     9   12
Multi-Dimensional Array:
Multi-dimensional arrays are widely used in scientific computing. A 3-D array is a combination of 2-D array.
Ex: int x[2][4][5]
It is considered as 3-D array in which there exist two 2-d arrays of size 4 rows and 5 columns.









































                                               

Initialization of 3-D array:

int a[2][4][5] : {{{1,2},{1,4}},{{3,4},{4,5}}};

1
2



1
4













3
4



4
5


















//reading and printing 3-d array:
#include<stdio.h>
void main()
{
int a[2][4][5],i,j,m,k,n,p;
clrscr();
printf("enter the no of planes,its rows and coloumns:");
scanf("%d%d%d",&p,&m,&n);
printf("\nenter the elements into array:"); /*reading the array*/
for(k=0;k<p;k++)
{
  for(i=0;i<m;i++)
  {
    for(j=0;j<n;j++)
    {
                                   printf("\nEnter next value");
                                   scanf("%d",&a[k][i][j]);
    }
  }
}
/* printing the data */
for(k=0;k<p;k++)
{
 for(i=0;i<m;i++)
 {
   for(j=0;j<n;j++)
   {
      printf("%d\t",a[k][i][j]);
   }
   printf("\n");
 }
}
getch();
}

Output:
enter the no of planes,its rows and coloumns:2 2 2
enter the elements into array:
Enter next value1
Enter next value2
Enter next value3
Enter next value4
Enter next value1
Enter next value2
Enter next value3
Enter next value4
1       2
3       4
1       2
3       4