The Simplest Form of An Array Is One-Dimensional Array. The Syntax To Define An Array Is As Follows. Type Arr-Name (Size) E.G. Int S
The Simplest Form of An Array Is One-Dimensional Array. The Syntax To Define An Array Is As Follows. Type Arr-Name (Size) E.G. Int S
1. Write a program for the average of ten different students total marks.
2. Write a program to input 5 different values by using pointer without
using an array.
3. Write a program to input any 5 values using pointer and modify each
element by multiplying 10 at each address of pointer.
Implementation of 1D Array in Memory
In memory, 1d arrays are implemented by allocating a sequence of
addressed locations so as to accommodate all its elements. The
starting address of the very first element of the array is called
base address of the array.
Formula for Address calculation of A[I,J] of order mxn [In Row Major form]:-
B+W[n(I-Lr)+(J-Lc)] where n are no. of cols.
In A[Lr : Ur , Lc : Uc] B is Base Address, W Element size in Bytes
Lr(Lower Bound of Row) Lr is Lowest Row Index and Ur is Highest row index [m=Ur-Lr+1]
Ur(Upper Bound of Col.) Lc is Lowest Col. Index and Uc is Highest Col. Index .[n=Uc-Lc+1]
EX:- A[-3..5,-2..7]
Problems in Row Major Implementation
For Numericals in 2D Array you always need to have no. of rows and cols. i.e m and
n.You will be given Array in two forms as:-
i) A[4..7,-1..3]
Here you have given elements of 1st row 1st Element(4) and Last row 1st
Element(7).Similarly, you have 1st col. 1st Element(-1) and Last col. 1st Element(3).
ii) A[10x5] Here you have given No. of rows as 10 and No. of Col.s as 5 directly.
Q1:-A 2D Array A[4..7,-1..3] requires 2 words of storage for each elt..If array is stored
in Row Major form,Calculate the address of A[6,2].Given Base address as 100.
Ans:-Here in B+W[n(I-Lr)+(J-Lc)],
B=100, W=2,I=6.J=2
Lr=4,Ur=7,So m(No. of Rows=Ur-Lr+1=7-4+1=4) i.e m=4
Lc=-1,Uc=3,So n(No. of Cols=Uc-Lc+1=3-(-1)+1 i.e n=5
So, Address of A[6,2]= 100+2[5(6-4)+(2-(-1))]=100+2[10+3]=100+26=126.
Q2:-A 2D Array X[10X5] in which each element takes 2 bytes to store.If X[1,1] begins
at address 150.Find location of X[3,4].Implementation is Row-Major.
Ans:- 176.
2)Column Major Implementation:-It stores the elements Column wise i.e it
stores first Col. then second Col. then third & so on.For Ex:-
Formula for Address calculation of A[I,J] of order mxn [In Column Major form]:-
B+W[(I-Lr)+m(J-Lc)] where m are no. of rows.
In A[Lr : Ur , Lc : Uc] B is Base Address, W Element size in Bytes
Lr is Lowest Row Index and Ur is Highest row index [m=Ur-Lr+1]
Lc is Lowest Col. Index and Uc is Highest Col. Index .[n=Uc-Lc+1]
Problems in Column Major Implementation:-
Q1:-In Array A[-20..20,10..35] requires 1 Byte of storage.If array is stored in Col. Major
Implementation with starting address as 500,determine the location of A[0,30].
Ans:-Here B=500, W=1, Lr=-20,Lc=10,I=0,J=30
m=Ur-Lr+1=20-(-20)+1=41.
Address of A[0,30]= B+W[(I-Lr)+m(J-Lc)]= 500+1[(0-(-20)+41(30-10)]=500+[20+(41x20)]=1340.
More Problems:-
Q1:-In Array A[-15..20,20..45], each element requires 1 byte of storage.If the array is
stored in Column Major form having beginning address as 1000, determine the
location of A[0,40].
Ans:-1735.
Selection Sort:
The basic idea of a selection sort is to repeatedly select the smallest key in
the remaining unsorted array.(Firstly 1st position is checked and fixed with
the shortest Elt. and then 2nd position is checked & so on).
Selection Sort
#include<iostream.h> void main()
#include<conio.h> {
void SelSort(int AR[], int size) clrscr();
{ int AR[50],ITEM,N,index;
int temp,pos; cout<<"Enterarray elements"<<endl;
for (int i=0; i<size;i++) for (int i=0; i<10; i++)
{ {
for (int j=i+1; j<size; j++) cin>>AR[i];
{ }
if (AR[i]>AR[j]) SelSort(AR,10);
{ cout<<endl<<"The sorted array is as
follows"<<endl;
temp=AR[i];
for (i=0; i<10; i++)
AR[i]=AR[j];
{
AR[j]=temp;
cout<<AR[i]<<" ";
}
}
}
getch();
}
}
}
Bubble Sort
The basic idea of a selection sort is to compare two adjoining values
and exchange them if they are not in proper order. The example
below can describe it.
void InsSort (int AR[], int size) int AR[ 50 ], ITEM, N, index;
{ cout<<“How many elements ..?”<<endl;
int temp, j; cin>>N;
AR [0] = INT_MIN ;
for (int i = 1; i < size; i++) cout<<“Enter array elements”<<endl;
{
temp = AR [i]; for (int i=0; i<N; i++)
j = i – 1; {
while (temp < AR [ j ]) cin>>AR [ i ];
{ }
AR [ j + 1 ] = AR [ j ];
j--; InsSort (AR, N);
}
AR [ j + 1 ] = temp; cout<<“The sorted array is as follows\n”;
cout<<“Array after pass – “<<i<<“-is”;
for (i=0; i<N; i++)
for (int k =1; k <= size; k++)
{
{
cout<<AR [ i ]<<“ “<<endl;
cout<<AR [ k ]<<“ “<<endl;
}
}
}
getch();
}
}
// AR [ j + 1 ] = AR [ j ]; This line is used
To change the current scanning elt to next
if the previous elt. Is placed to the right
Position.
Merging of Arrays:
Merging means combining elements of two arrays to form a new array.
Simplest way of merging two arrays is that first copy all the elements of
one array into new array and then copy all the elements of other array
into new array. If you want the resultant array to be sorted, you can
sort the resultant array. Another popular technique to have a sorted
resultant array is sorting while merging – Merge Sort.
Merge Sort.
Example of Merge Sort
If
M=4 and A[i]=11,22,33,44 (Already Sorted)
Note:- If A[i] & B[j] are not sorted the sort them first using
any of the sorting method described earlier as
Selection,Bubble or Insertion.
MERGE SORT
void merge(int A[],int M,int B[],int N,int C[]) void main()
{ {
int i=0, j=0, z=0; clrscr();
while((i<M) && (j<N)) int A [50], B [50], C [50], MN =0,M,N;
{ cout<<“Limit of First Array?”<<endl;
if(A[i]<B[j]) cin>>M;
{ cout<<“Enter array elements”<<endl;
C[z]=A[i]; for (int i=0; i<M; i++)
i++;z++; {
} cin>>A[ i ];
else if(A[i]>B[j]) }
{ cout<<“Limit of Second Array?”<<endl;
C[z]=B[j]; cin>>N;
j++; z++; cout<<“Enter array elements”<<endl;
} for (int i=0; i<N; i++)
} {
if(i>=M) //i.e if A[i] ends 1st while cmprng cin>>B[ i ];
{ }
for(int k=0;k<N-j;k++) MN = M + N;
C[z+k]=B[j+k]; merge ( A, M, B, N, C)
} cout<<“Array after merging”<<endl;
else //i.e if j>=N or B[j] ends 1st for (int i=0; i<MN; i++)
{ {
for(int k=0;k<M-i;k++) cout<<C [ i ];
C[z+k]=A[i+k]; }
getch();
} }
}
Example of Merge Sort
If M=4 and A[i]=11,22,33,44 (Already Sorted)
N=3 and B[j]=6,16,26 (Already Sorted)
While(i<4 && j<3)
{ if(11<6)
1)11<6 c[z]=6 i=0,j=1,z=1
2)11<16 c[z]=6,11 i=1,j=1,z=2
3)22<16 c[z]=6,11,16 i=1,j=2,z=3
4)22<26 c[z]=6,11,16,22 i=2,j=2,z=4
5)33<26 c[z]=6,11,16,22,26 i=2,j=3,z=4 Now j<N cond. Fails, so
if(i>=M) i.e 2>=4 false so ctrl goes to else part i.e j>=N (3>=3)
For(int k=0;k<M-i;k++) i.e for(int k=0;k<2;k++) i.e loop for rest of elts of A[i]
C[z+k]=A[i+k] i.e now assign remaining elts of A[i] as it is to C[z].
Q2:- Write a Function in C++ which accepts an integer array(1D) and its size as an argument
and exchange the values of first half side elements with the second half side elements of the
array.
For Ex:- if A[i]=2,4,1,6,7,9,23,10 the B[j]=7,9,23,10,2,4,1,6.
Hint:- Use mid=size/2. And for(i=0;i<mid;i++)
Q3:-Write a function in C++ to print the sum of all the values which are either divisible by 2 or
are divisible by 3 present in a 2D Array passed as the Argument to the function.
Hint :-Use if A[i][j]%2==0 and A[i][j]%3==0.
Q4:-WAF in C++ which accepts an integer array(2D) and its size as an argument and
Displays the elements which lie on diagonals.
Hint:- Use cout<<A[i][i] and cout<<cout<<A[i][n-(i+1)]
Q5:-WAF in C++ to print the product of each col. Of a 2D Array passed as the argument of the
fn.
Q6. An Array A[15][20] is stored in along the row(i.e Row Major form) with each element
Occupying 4 Bytes.Find out the Base address and the address of the element A[3][2], if the
Location of A[5][2] is stored at the address 1500.
Hint:-Initially take Lr=Lc=0.
Ans:-1092, 1340.
Q7. Find out the total number of elements and the total size of following arrays.{ Assuming
that each element consumes 4 bytes }
– Z[7]
– Y[-3…7]
– X[5] [4]
– W[-2..2][5..8]
Ans: a. Element: 7,Size : 28 bytes
b. Element: U-L +1 = 7-{-3}+1 =11, Size: 11 x 4 = 44 bytes.
c. Element: 5x 4 =20; Size : 20x 4 = 80 bytes
d. Rows = 2-{-2}+1 = 5, Cols. = 8 – 5 + 1 = 4, Elements :5 x 4 =20; Size=80 bytes
Q1:-In Array A[-15..20,20..45], each element requires 1 byte of storage.If the array is stored in
Column Major form having beginning address as 1000, determine the location of A[0,40].
Q3. An Array A[15][35] is stored in along the column(i.e Column Major form).Find out the Base
address and the address of the element A[2][5], if the Location of A[5][10] is stored at the
address 4000.
Hint:-Initially take Lr=Lc=0.
Q4:-An Array Arr[40][10] is stored in memory along column with each element Occupying
4 Bytes.Find out the address of Arr[3][6], if the location Arr[30[10] is stored at 9000.
Hint:- Initially take Ir=Ic=0 Ans:-7280.
Q5:- An Array Arr[20][15] is stored in memory along column with each element Occupying
8 Bytes.Find out the Base address and Address of Arr[2][3], if the element Arr[4][5] is
stored at Address 1000.(Ans:-168, 663).