Array in C++


Array in C++

Array in C++


Array

Array is a collection of similar type of data that shares a common name.
In C++, all arrays consist of contiguous memory location.

To find the size of array-
Array (size/length) =U-L+1                        
where U=upper value   ,L=lower value            

Ex.
If the array element numbered as -7,-6,-5…….. 0, 1, 2………. 15, then find the length.
Array size=15-(-7) +1
                  =15+7+1
                  =23


Address calculation

Address of element with subscript I=  Base address +ES (I-L)
Where ES is size of an array element L is lower bound of array.



Q. what will be the address of 5th element in a floating -point array implemented in C++? The array is specified as Amount [16]. The base address of array is 1058.

Sol. Address of amount [4] =base add +ES(I-L)
                                           L=0
                                           ES=4
                                           Base address=1058
                                                      =1058+ 4(4-0)
                                                       =1058+16
                                                        =1074



Basic operations on One-dimensional Array


Searching


°       Linear search

In a linear search each element of array is compared with the given item to be searched for, one by one.


Algorithm
/*Initialize counter by assigning lower bound value of an array.*/
Step1   Set ctr=L
Step2 Repeat stem. 3 through 4 until ctr>U
Step3 If Ar(ctr)==ITEM then
  { print “search successful”
   print ctr,”is the location of”, ITEM
   break
    }
Step4 ctr=ctr+1
Step5 If ctr>U then
           print “search unsuccessful!”
Step6 END.



°        Binary Search

This is very popular searching, you minimum possible comparisons. For this searching array should be in ascending or descending order.

For Array AR[L,U] is stored in ascending order
1.      Set beg=L , last=U
2.    Repeat step 3 through 6 UNTIL beg>last
3.    mid=INT(beg+last)/2              //int is used to extract the                  
                                       used to extract the integer
                                                             part
4.    if AR[mid]==ITEM then
   {
      print “search successful”
      print ITEM “found at”, mid
      break
    }
5.     If AR[mid]<ITEM then
     beg=mid+1
6.    IF AR[mid]>ITEM
    last =mid-1
7.     If beg=last
    print “unsuccessful search”
8.    END


 Sorting 

°        Selection
°        Bubble
°        Insertion


     2D-Array


Implementation of 2D Array in memory

Array in C++



Address calculation


Row majored

Array in C++


Address of [I,J] element=B+W[c(I-Lr)+(J-Lc)]

Where B=base address ,W=element size, Lr=first row number, Lc=first column number


Ex. A 2-D array defined as A[4…7,-1……3] required 2 words of storage for each element. If array is stored in row major, calculate the address of A[6,2],base address is 100.


Sol. Address [I, J] =B+W[c(I-Lr)+(J-Lc)]
                      B=100, W=2
                       Lr=4, Lc=-1
                      c= 3+1+1=5 
                       A[6,2]=100+2[5(6-4)+(2-(-1))]
                                   =100+2[5x2 +3]
                                   =100 +26
                                   =126




Column majored

Array in C++

Address of[I,J] element=B+W[(I-Lr)+r(J-Lc)]       
Where r=no of rows


Operation in 2-D array

#include<iostream.h>
#include<process.h>
void main()
{
 int a[10][10], b[10][10], c[10][10];
 int i, j, m, n, p, q;
 cout<<”Input the rows and columns of matrix a”;
 cin>>m>>n;
 cout<<”Input the rows and columns of matrix b”;
 cin>>p>>q;
 if(m==p)&&(n==q)
   cout<<”Matrix can be added\n”;
 else
 {
   cout<<”Matrix can’t be added\n”;
   exit(0);
 }
cout<<”\n Input matrix A”;
for(i=0;i<m; i++)
{
  for(j=0;j<n;j++)\
  cin>>a[i][j];
}
cout<<”\n Enter matrix B”;
for(i=0;i<p;i++)
{
  for(j=0;j<q;j++)
   cin>>b[i][j];
}
for (i=0;i<m;i++)
{
  for (j=0;j<n;j++)
     c[i][j]=a[i][j]+b[i][j];
}
  cout<<”\n The sum is”;
  for(i=0;i<m;i++)
 {
  cout<<”\n”;
  for(j=0;j<n;j++)
       cout<<” “<<c[i][j];
 }
}





This post is about Array in C++. In the next post we will discuss the topic Stack . 








Post a Comment

0 Comments