0% found this document useful (0 votes)
67 views8 pages

Array Functions - 1D

The document provides solutions to 10 questions related to array functions in C++. The questions cover topics like: 1) Shifting elements of an array by one position and wrapping the last element to the first position. 2) Swapping elements that are multiples of 10 with the next element. 3) Transferring elements from one array to two arrays based on even/odd positions. 4) Dividing elements divisible by 5 by 5, and multiplying other elements by 2. 5) Sorting a structure array based on one of the structure fields. The questions are followed by well-commented code solutions explaining the logic and approach for each problem in 3 sentences or less.

Uploaded by

FARAH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views8 pages

Array Functions - 1D

The document provides solutions to 10 questions related to array functions in C++. The questions cover topics like: 1) Shifting elements of an array by one position and wrapping the last element to the first position. 2) Swapping elements that are multiples of 10 with the next element. 3) Transferring elements from one array to two arrays based on even/odd positions. 4) Dividing elements divisible by 5 by 5, and multiplying other elements by 2. 5) Sorting a structure array based on one of the structure fields. The questions are followed by well-commented code solutions explaining the logic and approach for each problem in 3 sentences or less.

Uploaded by

FARAH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

ARRAY FUNCTIONS (1D ARRAY)

EACH QUESTIONS CARRY 2 OR 3 MARKS

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:

void Get2From1( int ALL[],int N)


{ int Odd[ ], Even[ ], j=0, k=0;
for(int i=0;i<N;i++)
{
if (i%2==0)
{ Even[j]=ALL[i];
j++;
}
else
{
Odd[k]=ALL[i];
k++;
}
}
}
Q.4 Write a function REASSIGN( ) in C++, which accepts an array of integers and its size as
arguments and divide all those array elements by 5 which are divisible by 5 and multiply other array
elements by 2.

Ans:-

void REASSIGN (int A[ ], int n)


{
for(int i=0;i<n;i++)
{
if(A[i]%5= =0)
A[i]= A[i]/5;
else
A[i]= A[i]*2;
}
}
Q.5 Write a function SORTPOINTS( ) in C++ to sort an array of structure Game in descending
order of Points using bubble sort:
struct Game
{ long Pno; // Player no.
char Pname[20];
long Points;
};
Ans:
void SORTPOINTS(Game G[ ], int n)
{ Game temp;
for(int i=0;i<n;++i)
{
for(int j=0;j<(n-1) ;j++)
{
if(G[j].Points<G[j+1].Points)
{ temp=G[j];
G[j]=G[j+1];
G[j+1]=temp;
}
}
}
}

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:

void SwapArray(int A[ ], int N)


{ int i, j, temp;
if(N%2==0)
j=N;
else
j= N-1;
for(i=0; i<j; i+=2)
{ temp=A[i];
A[i]=A[i+1];
A[i+1]=temp;
}
}

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:

void manipulate (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]*3;
cout<<a[i]<<”,”;
}
}

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--];
}
}

( ½ Mark for function header with desired parameter s)


( ½ Mark initialising counters)
(1 Mark for correct formation of loop)
(1 Mark for correct comparison of elements)
(1 Mark for transferring remaining elements in resultant array)

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:

void insertsort( int a[],int n)


{
int p,ptr;
//Assuming a[0]=int_min i.e. smallest integer
for(p=1;p<=n;p++)
{
temp=a[p];
ptr=p-1;
while(temp<a[ptr])
{
a[ptr+1]=a[ptr]; // Move Element Forward
ptr--;
}
a[ptr+1]=temp; // Insert Element in Proper Place
}
Q.21 Write function oddEven(int S [ ], int N) in C++, to add 5 in all the odd values and 10 in all the even
values of the array S.
Example : if the original content of the array S is 50, 11, 19, 24, 28
The modified content will be: 60, 16, 24, 34, 38
Ans:

void oddEven (int S[ ],int N)


{
for (int i=0;i<N;i++)
{
if (a[i]%2= =0)
a[i]=a[i]+10;
else
a[i]=a[i]+5;
cout<<a[i]<<”,”;
}
}
Q.22 Write a function TRANSFER(int A[], int B[], int Size) in C++ to copy the elements of array A into
array B in such a way that all the negative elements of A appear in the beginning of B, followed by all
the positive elements, followed by all the zeroes maintaining their respective orders in array A. (3)
For example:
If the contents of array A are:
7, -23, 3, 0, -8,-3,4, 0
The contents of array B should be
-23 , -8, -3, 7, 3, 4, 0, 0
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();
}

void transfer(int a[],int b[],int size)


{
int k=0;
for(int i=0;i<size;i++)
if(a[i]<0)
{
b[k]=a[i];
k++;
}
for(int i=0;i<size;i++)
if(a[i]>0)
{
b[k]=a[i];
k++;
}
for(int i=0;i<size;i++)
if(a[i]==0)
{
b[k]=a[i];
k++;
}

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:

You might also like