MENU

Chapter 8 Arrays Solutions

Question - 11 : -
Write code for 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.
For example: If the content of the array is :

0 1 2 3 4
12 15 17 13 21
The Chenged Content will be:

0 1 2 3 4
21 12 15 17 13

Answer - 11 : -

void ChangeOver (int P[] , int N)
 {
 int i, j, temp;
 for (i=0; i
 {
 j=j+1;
 temp = P[i];
 P[i] = P [j ] ;
 P [j] = temp;
 }
 }

Question - 12 : -
Write a user-defined function DispNTen (int L[] [4], int R, int C) in C++ to find the display all the numbers, which are not divisible by 10. For example if the content of array is :

20 17 30
12 19 10
The output should be
17 12 19

Answer - 12 : -

void DispNTen (int L[] [4], int R,
 int C)
 {
 int i, j ;
 for (i = 0 ; i
 {for (j = 0; j
 {
 if (A[i] [j]%10= =0)
 cout <
 }
 }
 }

Question - 13 : -
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. 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

Answer - 13 : -

void Transfer (int A[], int B[], int size)
 {
 for (int i = 0; i
 {
 if(A[i]<0) B [i] =A [i] ; if (A [i] > 0)
 B[i+1]=A[i];
 else
 B[i/2]=A[i];
 }
 }

Question - 14 : -
Write a function SORTSCORE() in C++ to sort an array of structure IPL in descending order of score using selection sort.
Note : Assume the following definition of structure IPL:
struct IPL:

 {
 int Score;
 char Teamname[20];
 } ;

Answer - 14 : -

void SORTSCORE (IPL a[] , int n)
{ int largest;
IPL temp;
for ( int K = 0; K
{ largest = K;
for ( int j = K+1; j< n; j++) { if ( a[j].score > a[largest]. score)
{ largest = j;
}
}
temp = a[K];
a[K] = a[largest];
a [largest] = temp;
}
}

Question - 15 : -
Write a function in C++ TWOTOONE() which accepts two array X[ ], Y[ ] and their size n as argument. Both the arrays X[ ] and Y[ ] have the same number of elements. Transfer the content from two arrays X[ ], Y[ ] to array Z[ ]. The even places (0,2,4…) of array Z[ ] should get the contents from the array X[ ] and odd places (1,3,5…) of array Z[ ] should get the contents from the array Y[ ].
Example : If the X[ ] array contains 30,60,90 and the Y[ ] array contains 10,20,50. Then Z[ ] should contain 30,10,60,20,90,50.

Answer - 15 : -

 void TWOTOONE (int x[] , int n)
 {
 int z[n] ;
 for (int I =0;i
 {
 if(i%2 == 0)
 z [i] = x [i] ;
 else
 z [i] = y [i] ;
 }
 }

Question - 16 : -
Write code for a function void EvenOdd (int T[], int C) in C+ + , to add 1 in all the odd values and 2 in all the even values of the array T.
Example : If the original content of the array T is

T[0] T[l] T[2] T[3] T[4]
35 12 16 69 26
The modified content will be:

T[0] T[l] T[2] T[3] T[4]
36 14 18 70 28

Answer - 16 : -

 void EvenOdd (intT [] , int(C)
 {
 for (int i=0; i
 {
 if (T[i] %2==0)
 T [i] + = 2;
 else
 T[i] + = 1;
 }
 }

Question - 17 : -
Write a function SWAP2 CHANGE (int P[], int N) 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.
For example:
If the content of array P is
91,50,54,22,30,54
The content of array P should become
91,54,50,22,54,30

Answer - 17 : -

 void SWAP 2CHANGE (int P[], int N)
 {
 for (int i=0; i
 {
 if (P [i]% 10==0)
 {
 int temp=0;
 temp=P[i];
 P[i]=P [i+1] ;
 P[i+1]=temp;
 i++; //Go to next element
 }
 }
 }

Question - 18 : -
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.
For 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

Answer - 18 : -

 void SWAP2BEST (int ARR [] , int Size)
 {
 for (int n=0; n
 {
 if(ARR[n]%10 = =0)
 {
 int temp = 0;
 temp = ARR [n];
 ARR[n] = ARR[n+1];
 ARR [n+1] = temp;
 n++;
 }
 }
 }

Question - 19 : -
Write a function Alternate (int A[] [3], int N, int M) in C + + to display all alternate elements from two-dimensional array A(starting from A[0][0]).
For example:
If the array is containing :
23 54 76
37 19 28
62 13 19
The output will be
23 76 19 62 19

Answer - 19 : -

void Alternative (int A[] [3], intN, int M)
 {
 int T=0 ; I
 for (int J=0 ; J {
 if (T%2 = 0)
 COUt << A[I] [J]<<" " ;
 T++ ;
 }
 }
 OR
 void Alternative (int A[] [3] , intN, int M)
 {
 int *P=&A [0] [0] ;
 for (int I = 0; I
 {
 Cout<<*p<<" " ;
 { I+=2 ;
 }
 }
OR
Any other equivalent correct answer acceptable

Question - 20 : -
Write a function in C++ to print the sum of all the non-negative elements present on both the diagonal of a two dimensional array passed as the argument to the function.

Answer - 20 : -

void Diagonal (int A[] [20] , int N)
 {
 int K, Sdiag=0;
 for(int K=0;K 0)
 Sdiag+=A [K] [K] ;
 for(int K= 0;K 0 )
 Sdiag+=A[N-K-1] [K] ;
 cout<<"Sum of all the non-negative elements on both the diagonals= "<
 }

×