Array Functions - 1D
Array Functions - 1D
Q.1 Write a function void ChangeOver(int P[ ], int N) in C++, which re-positions all the elements of the
array by shifting each of them to the next position and by shifting the last element to the first position.
Example:
If the content of array P is
12, 15, 17, 13, 21
The content of array P should become
21, 12, 15, 17, 13
Ans:
void ChangeOver(int P[ ],int N)
{ int temp=P[N-1];
for(int i=N-2; i>=0; i--)
P[i+1]=P[i];
P[0]=temp;
}
Q.2 Write a function SWAP2BEST(int ARR[], int Size) in C++ to modify the content of the array in such a
way that the elements, which are multiples of 10 swap with the value present in the very next position
in the array.
Example:
If the content of array ARR is
90, 56, 45, 20, 34, 54
The content of array ARR should become
56, 90, 45, 34, 20, 54
Ans:
void SWAP2BEST(int ARR[ ],int Size)
{ int i, temp;
for(i=0; i<Size;)
{ if(ARR[i]%10==0)
{ temp=ARR[i+1];
ARR[i+1]=ARR[i];
ARR[i]=temp;
i+=2;
}
else
i++;
}
}
Q.3 Write a Get2From1( ) function in C++ to transfer the content from one array ALL[ ] to two
different arrays Odd[ ] and Even[ ]. The Odd[ ] array should contain the values from odd
positions (1,3,5, …..) of ALL[ ] and Even[ ] array should contain the values from even positions
(0,2,4,…) of ALL[ ].
Example
If the ALL[ ] array contains
12, 34, 56, 67, 89, 90.
The Odd[ ] array should contain
34, 67, 90
And the Even[ ] array should contain
12, 56, 89
Ans:
Ans:-
Q.6 Write a function in C++, which accepts an integer array and its size as parameters and
rearranges the array in reverse.
Example:If an array of nine elements initially contains the elements as 4, 2, 5, 1, 6, 7, 8, 12, 10
Then the function should rearrange the array as 10, 12, 8, 7, 6, 1, 5, 2, 4
Ans:
void reversearray(int A[ ], int size)
{ int i, j, temp;
for(i=0, j=size-1; i<=j; i++, j--)
{ temp=A[i];
A[i]=A[j];
A[j]=temp;
}
}
Q.7 Write a function in C++, which accepts an integer array and its size as arguments and swap the
elements of every even location with its following odd location.
Example: If an array of nine elements initially contains the elements as 2, 4, 1, 6, 5, 7, 9, 23, 10
then the function should rearrange the array as 4, 2, 6, 1, 7, 5, 23, 9, 10
Ans:
Q.8 Write function in C++ which accepts an integer array and size as arguments and replaces elements
having odd values with thrice its value and elements having even values with twice its value.
Example : if an array of five elements initially contains elements as 3, 4, 25, 16, 9
The function should rearrange the content of the array as 9, 8, 75, 32, 27
Ans:
Q.9 Write a function in C++ which accepts an integer array and its size as arguments and replaces
elements having even values with its half and elements having odd values with twice its value .
Example : If an array of five elements initially contains the elements as 3, 4, 5, 16, 9
then the function should rearrange content of the array as 6, 2, 10, 8, 18
Ans:
void changearray(int a[ ],int size)
{
for (int i=0;i<size;i++)
{
if (a[i]%2= =0)
a[i]=a[i]/2;
else
a[i]=a[i]*2;
cout<<a[i]<<’,’;
}
}
Q.10 Write a function in C++ which accepts an integer array and its size as arguments and exchanges
the values of first half side elements with the second half side elements of the array.
Example : If an array of 8 elements initial content as 2, 4, 1, 6, 7, 9, 23, 10
The function should rearrange array as 7, 9, 23, 10, 2, 4, 1, 6
Ans:
void change(int a[ ],int size)
{ int i,j,temp;
for(i=0,j=size/2; j<size; i++,j++)
{ temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
Q.11 Define the function SwapArray(int[ ], int),that would expect a 1D integer array NUMBERS and its
size N. the function should rearrange the array in such a way that the values of that locations of the
array are exchanged. (Assume the size of the array to be even).
Example :
If the array initially contains {2, 5, 9, 14, 17, 8, 19, 16}
Then after rearrangement the array should contain {5, 2, 14, 9, 8, 17, 16, 19}
Ans:
void SwapArray(int ARR[ ],int Size)
{ int i, temp;
for(i=0; i<Size;i++)
{ temp=ARR[i+1];
ARR[i+1]=ARR[i];
ARR[i]=temp;
}
}
Q.12 Assume a array E containing elements of structure Employee is required to be arranged in
descending order of Salary. Write a C++ function to arrange same with the help of bubble sort,
the array and its size is required to be passed as parameters to the function.
Definition of structure Employee is as follows:
struct Employee
{ int Eno;
char name[25];
float Salary;
};
Ans:
void bubble(Employee E[ ],int n)
{
int i,j;
Employee temp;
for(i=0;i<n;++i)
for(j=0; j<(n-1) ;j++)
if(E[j].salary<E[j+1].salary)
{ temp=E[j];
E[j]=E[j+1];
E[j+1]= temp;
}
cout<<"The details of the employee in descending order of salary ";
for(i=0;i<n;i++)
cout<<E[i].Eno<<'\t'<<E[i].name<<’\t<<E[i].Salary<<endl;
}
Q.13 Suppose A, B, C are arrays of integers of size M, N and M+N respectively. The numbers in array A
appear in ascending order while numbers in array B in descending order. Write user defined function
in C++ to produce third array C by merging array A by B in ascending order.
Use A, B and C as arguments in the function.
Ans:
void Merge(int A[ ],int M,int B[ ],int N,int C[ ])
{
int a=0 ,b= N-1,c=0;
while (a<M && b>=0)
{
if(A[a]<=B[b])
C[c++]=A[a++];
else
C[c++]=B[b--];
}
if(a<M)
{ while(a<M)
C[c++]=A[a++];
}
else
{ while(b>=0)
C[c++]=B[b--];
}
}
Q.14 Suppose a 1D array AR containing integers is arranged in ascending order. Write a user defined
function in C++ to search for one integer from AR with the help of binary search method, to show
presence of the number in the array. The function should have three parameters:
(1) an array AR (2) the number to be searched and (3) the number of elements N in the array.
Ans:
void BinSearch(int AR[ ], int item, int N)
{ int beg=0, last= N-1,mid, flag=0;
while(beg<=last)
{ mid=(beg + last)/2;
if (item= = AR[mid])
{ flag=1;
break;
}
else if (item<AR[mid])
last=mid-1;
else
beg=mid+1;
}
if( flag = = 0)
cout<<”\nThe Search Element “<<item<<” is not available”;
else
cout<<”\nThe Search Element “<<item<<” is available”;
}
Q.15 Suppose an array P containing float is arranged in ascending order. Write a user defined function
in C++ to search for one float from P with the help of binary search method. The function should
return an integer 0 to show absence of the number in the array. The function should have the
parameters as (1) an array P (2) the number DATA to be searched (3) number of elements N.
Ans:
int BinSearch(float P[ ],float DATA, int N)
{ int beg=0, last= N-1, mid;
while(beg<=last)
{ mid=(beg + last)/2;
if (DATA= = P[mid])
{ return 1;
}
else if (DATA<P[mid])
last=mid-1;
else
beg=mid+1;
}
return 0;
}
Q.16 Write a function in C++ to combine the contents of two equi-sized arrays A and B by computing
their corresponding elements with the formula 2*A[i]+3*B[i]; where value i varies from 0 to N-1 and
transfer the resultant content in the third same sized array.
Ans:
void AddNSave(int A[],int B[],int C[],int N)
{
for (int i=0;i<N;i++)
C[i]=2*A[i]+3*B[i];
}
Q.17
Ans:
Q.19
Q.20 Write a function to sort any array of n elements using insertion sort . Array should be passed
as argument to the function.
Ans:
#include<iostream.h>
#include<conio.h>
void transfer(int a[],int b[],int size);
void main()
{
int a[]= {7, -23, 3, 0, -8,-3,4, 0},b[8];
transfer(a,b,8);
for(int i=0;i<8;i++) cout<<b[i]<<'\t';
getch();
}
Q.23 Write the definition of function named Array_Swap() that will accept an integer array & its size as
arguments and the function will interchange/swap elements in such a way that the first element is
swapped with the last element, second element is swapped with the second last element and so on,
only if anyone or both the elements are odd.
E.g. if initially array of seven elements is:
5, 16, 4, 7, 19, 8, 2
After execution of the above function, the contents of the array will be:
2,16, 19, 7, 4, 8, 5
Ans: