Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

An array Arr[35][15] is stored in the memory along the row with each element occupying 4 bytes. Find out the base address and address of an element Arr[20][5], if the location Arr[2][2] is stored at the address 3000. 

Answer»

A[35][15] => rows R=35, columns C=15

Let base address be B

Given element width W=4 bytes and A[2][2]=3000

In Row major,

 A[I][J]=B+W(C(I=lr)+(j-lc))

where lr=lowest row and lc=lowest column

A[2][2]=B+W(C(2-0)+(2-0))

3000=B+4(15(2)+2) 

3000=B+128

Base Address B=3000-128=2872

Using same formula

 A[20][5]=2872+4(15(20-0)+(5-0))

= 2872+1220

= 4092

2.

An array Arr[15][35] is stored in the memory along the column with each element occupying 8 bytes. Find out the base address and address of the element Arr[2][5], if the location Arr[5][10] is stored at the address 4000.

Answer»

Arr[15][35]

No. of Rows(i.e., R) = 15

No. of Cols(i.e., C) = 35

Element size(W) = 8 bytes 

Arr[I][J] = Arr[2][5] => I=2, J=5

Address of Arr[5][10] = 4000

Base Address (B) =? 

Lowest Row (i.e., lr) = 0

Lowest Col (i.e., lc) = 0

Formula to calculate address in Column Major arrangement is: 

Arr[P][Q] = B + W[(P - lr ) + R(Q - lc )] 

Arr[5][10] = B + 8((5 – 0) + 15(10 – 0)) 

4000 = B + 8(155) 

4000 = B + 1240 

=> B = 4000-1240= 2760

Parallely, Arr[I][J] = B + W[(I - lr ) + R(J - lc )] 

Arr[2][5] = 2760 + 8[(2 – 0) + 15(5 – 0)]

= 2760 + (8 x 77)

= 2760 + 616

= 3376

3.

What is the pre-condition for applying binary search algorithm?

Answer»

For applying binary search algorithm the array, to be scanned, must be sorted in any order (ascending or descending).

4.

Write an algorithm to search for 66 and 71 in the following array:3, 4, 7, 11, 18, 29, 45, 71, 87, 89, 93, 96, 99Make use of binary search technique. Also give the intermediate results while executing this algorithm. Convert this algorithm into a C++ program. 

Answer»

Algorithm:

1. Set beg=0,last=12

2. REPEAT steps 3 through 6 UNTIL beg>last //INT() is used to extract integer part

3. mid=INT((beg+last)/2)

4. if A[mid]==ITEM then

{

print "Search Successful" 

print ITEM,"fount at",mid 

break          /* go out of the loop*/

5. if A[mid]ITEM then

last=mid-1 /* END of repeat */ 

7. if beg!=last

print "Unsuccessful Search" 

8. END

Intermediate Results:

(i) Search for 66.

Step 1: beg=1; last=13; mid=INT(1+13)/2=7

Step 2: A[mid] i.e., A[7] is 45 45<66 then

beg=mid+1 i.e., beg=7+1=8

Step 3: mid=Int((beg+last)/2)=INT((8+13)/2)=10 

A[10] i.e., 89>66 then last = mid-1=10-1=9

Step 4: mid=((8+9)/2)=8 

A[8] is 71 71>66 than last = mid-1=8-1=7

Step 5: mid=((8+7)/2)=7 

A[7] is 45 45 < 66 then beg = mid+1=7+1=8 

Step 6: mid=((8+8)/2)=8 (beg=last=8)

 A[8] is 71 => 71!=66 

“Search Unsuccessful!!!”

(ii) Search for 71. 

Step 1: beg=1; last=13; mid=INT(1+13)/2=7

Step 2: A[mid] i.e., A[7] is

45 45<71 then

beg=mid+1 i.e., beg=7+1=8 

Step 3:

mid=Int((beg+last)/2)=INT((8+13)/2)=10 

A[10] i.e., 89>71 then last = mid-1=10-1=9

Step 4: mid=((8+9)/2)=8 

A[8] is 71 71=>71 

“Search Successful!!!”

Program: 

#include<iostream.h>

int Bsearch(int [],int); 

int main()

{ int A[]={3,4,7,11,18,29,45,71,87,89,93,96,99};

int index; 

index=Bsearch(A,71); 

if(index==-1)

cout<<"Element not found.."; 

else

 cout<<"Element found at

index:"<A[mid]) beg=mid+1; else last=mid-1;   } 

rerurn -1;

 }

int Bsearch(int A[],int item) 

{ int beg,last,mid; 

beg=0; last=13-1; 

while(beg<=last) 

{ mid=(beg+last)/2; 

if(item==A[mid]) return mid;

else if (item>A[mid]) beg=mid+1;

else last=mid-1;

}

rerurn -1;

}

5.

Suppose X, Y, Z are arrays of integers of size M, N and M+N respectively. The numbers in array X and Y appear in descending order.Write a user defined function in C++ to produce third array Z by merging arrays X and Y in descending order.

Answer»

#include <iostream.h>

void Merge(int X[],int M,int Y[],int N,int Z[]);

int main()

{ int X[50],Y[50],Z[50],MN=0,M,N;

cout<<"How many elements do U want to create first array with? ";

cin>>M;

cout<<"Enter First Array's elements [descending]..."; for(int i=0;i>X[i];

cout<<"How many elements do U want to create second array with? ";

cin>>N; MN=M+N;

cout<<"Enter Second Array's elements [descending]..."; 

for(int i=0;i>Y[i];

Merge(X,M,Y,N,Z);

cout<<"The merged array is....";

for(i=0;i<MN;i++)

cout<<Y[i]<<" ";

cout<<end1;

return 0;

}

void Merge(int X[],int M,int Y[],int N,int Z[])

{ int x,y,z;

for(x=-1,y=-1,z=-1;x>=0&&y>=0;)

{  if(X[x]<=Y[y]) Z[z--]=X[x--];

else Z[z--]=Y[y--];

}

if(x<0)

{  while(x>=0)

Z[z--]=X[x--];

}

else

{

while(y>=0)

Z[z--]=Y[y--];

}

}

6.

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 the numbers in array B appear in descending order. Write a user defined function in C++ to produce third array C by merging arrays A ad B in Ascending order. Use A, B, and C as arguments in the function.

Answer»

#include<iostream.h>

void Merge(int A[],int M,int B[],int N,int C[]);

int mai()

{ int A[50],B[50],C[50],MN=0,M,N;

cout<<"How many elements do U want to create first array with? ";

cin>>M;

cout<<"Enter First Array's elements [ascending]...";

for(int i=0;i<M;i++)

cin>>A[i];

cout<<"How many elements do U want to create second array with? ";

cin>>N;

MN=M+N;

cout<<"Enter Second Array's elements [descending]...";

for(int i=0;i<N;i++)

cin>>B[i];

Merge(A,M,B,N,C);

cout<<"The merged array is....";

for(i=0;i<MN;i++)

cout<<C[i]<<" ";

cout<<endl;

return 0;

}

void Merge(int A[],int M,int B[],int N,int C[])

 { int a,b,c;

for(a=0,b=-1,c=0;a=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--];

}

}

7.

Suppose A, B, C are arrays of integers of sizes m, n, m+n respectively. The numbers in arrays A and B appear in descending order. Give an algorithm to produce a third array C, containing all the data of array A and B in ascending order.

Answer»

Assuming that L = 0 and U = m-1,n-1 and (m+n)-1 respectively for A, B, and C

1.  ctrA=m-1; ctrB=n-1; ctrC=0;

2.  while ctrA>=0 and ctrB>=0 perform steps 3 through 10

3.  {  If A[ctrA]<=B[ctrB] then

4.  {  C[ctrC]=A[ctrA]

5.  ctrC=ctrC+1

6.  ctrA=ctrA-1 } 

7.  else

8.  { C[ctrC]=B[ctrB]

9.  ctrC=ctrC+1

10.  ctrB=ctrB-1  }

}

11.  if ctrA<0 then

12.  {  while ctrB>=0 perform steps 13 through 15

13.  C[ctrC]=B[ctrB]

14.  ctrC=ctrC+1

15.  ctrB=ctrB-1

}

}

16. if ctrB<0 then

17.  { while ctrA>=0 perform steps 18 through 20

18.  {  C[ctrC]=A[ctrA]

19.  ctrC=ctrC+1

20.  ctrA=ctrA-1

}

}

8.

Given two arrays of integers X and Y of sizes m and n respectively. Write a function named MERGE() which will produce a third array named Z, such that the following sequence is followed:(i) All odd numbers of X from left to right are copied into Z from left to right(ii) All even numbers of X from left to right are copied into Z from right to left(iii) All odd numbers of Y from left to right are copied into Z from left to right(iv) All even numbers of Y from left to right are copied into Z from right to left X, Y and Z are passed as argument to MERGE().e.g., X is {3, 2, 1, 7, 6, 3} and Y is {9, 3, 5, 6, 2, 8, 10} the resultant array Z is {3, 1, 7, 3, 9, 3, 5, 10, 8, 2, 6, 6, 2}

Answer»

void MERGE(int X[],int Y[],int n,int m)

{ int Z[20],i=0,j=0,k=0,l=m+n-1;

while(i<n&&k<20)

{ if(X[i]%2!=0)

{ Z[k]=X[i];

k++;

i++;

}

else

{ Z[l]=X[i];

l--;

i++;

}

}

while(j<m&&k<20)

{ if(Y[j]%2!=0)

{ Z[k]=Y[j];

k++;

j++;

}

else

{ Z[l]=Y[j];

l--;

j++;

}

}

cout<<"The elements of an array C is:";

for(i=0;i<n+m;i++)

cout<<"\n"<<Z[i];

}

9.

Write an algorithm to merge two arrays X[6], Y[5] stored in descending order. The resultant array should be in ascending order.

Answer»

Assuming that L=0 and U=6-1,5-1 and (6+5)-1 respectively for X, Y, and Z 

1. ctrX=6-1; ctrY=5-1; ctrZ=0; 

2. while ctrX>=0 and ctrY>=0 perform steps 3 through 10 

3. { If X[ctrX]<=Y[ctrY] then 

4. { Z[ctrZ]=X[ctrX] 

5. ctrZ=ctrZ+1 

6. ctrX=ctrX-1 } 

7. else

8. { Z[ctrZ]=Y[ctrY] 

9. ctrZ=ctrZ+1 

10. ctrY=ctrY-1 }

11. if ctrX<0 then 

12. { while ctrY>=0 perform steps 13 through 15

13. Z[ctrZ]=Y[ctrY] 

14. ctrZ=ctrZ+1 

15. ctrY=ctrY-1 

16. if ctrY<0 then 

17. { while ctrX>=0 perform steps 18 through 20

18. { Z[ctrZ]=X[ctrX]

19. ctrZ=ctrZ+1 

20. ctrX=ctrX-1 

}

}

10.

Write an algorithm to add corresponding elements of two matrices A[3 x 3] and B[3 x 3] 

Answer»

/* Read the two matrices */ 

1. for i=1 to 3 perform step 

2 through 4 2. { for j=1 to 3 perform step 3 through 4 

3. { Read A[i,j] 

4. Read B[i,j] 

}

/* Calculate the sum of the two and store it in third matrix C */ 

5. for i=1 to 3 perform step 6 through 8

{

6. for j=1 to 3 perform step

7 through 8 7. { C[i,j]=0

8. C[i,j]=A[i,j]+B[i,j]

}

}

11.

Write a user defined function in C++ to display the sum of row elements of two dimensional array A[5][6] containing integers.

Answer»

void RowSum(int A[5][6])

{ int SUMC[5];

for(int i=0;i<5;i++)

{ SUMC[i]=0;

for(int j=0;j<6;j++)

SUMC[i]+=A[i][j];

cout<<"Sum of row"<<i+1<<"="<< SUMC[i]<<end1;

}

}

12.

Write a function in C++ which accepts a 2D array of integers and its size as arguments and display the elements of middle row and the elements of middle column. [Assuming the 2D Array to be a square matrix with odd dimension i.e., 3 x 3, 5 x 5, 7 x 7 etc….] Example, if the array content is 3 5 47 6 92 1 8Output through the function should be:Middle Row: 7 6 9Middle Column: 5 6 1

Answer»

const int S=7; // or it may be 3 or 5

int DispMRowMCol(int Arr[S][S],int S)

{ int mid=S/2;

int i;

//Extracting middle row

cout<<"\n Middle Row:";

for(i=0;i<S;i++)

cout<<Arr[mid][i]<<" ";

//Extracting middle column

cout<<"\n Middle Column:";

for(i=0;i<S;i++)

cout<<Arr[i][mid]<<" ";

}

13.

Calculate the address of X[4,3] in a two-dimensional array X[1….5, 1…..4] stored in row=major order in the main memory. Assuming base address to be 1000 and that each requires 4 words of storage. 

Answer»

X[4][3]=B+W(C(I-1)+(J-1))

=1000+4(4(4-1)+(3-1))

=1000+4(4(3)+(2))

=1000+56

=1056

14.

Write a function in C++ which accepts a 2D array of integers and its size as arguments and display the elements which lie on diagonals.[Assuming the 2D Array to be a square matrix with odd dimension i.e., 3 x 3, 5 x 5, 7 x 7 etc….] Example, if the array content is5 4 3 6 7 81 2 9Output through the function should be:\Diagonal One: 5 7 9Diagonal Two: 3 7 1

Answer»

const int n=5;

void Diagonals(int A[n][n], int size)

{

int i,j;

cout<<"Diagonal One:";

for(i=0;i<n;i++)

cout<<A[i]ij]<<" ";

cout<<"\n Diagonal Two:"

for(i=0;i<n;i++)

cout<<A[i][n-(i+1)]<<" ";

}

15.

Consider the following key set: 42, 29, 74, 11, 65, 58, use insertion sort to sort the data in ascending order and indicate the sequences of steps required.

Answer»

Insertion sort:

Step-1 - ∞, 42, 29, 74, 11, 65, 58

Step-2 - ∞, 29, 42, 74, 11, 65, 58 

Step-2 - ∞, 29, 42, 11, 74, 65, 58

Step-4 - ∞, 29, 42, 11, 65, 74, 58

Step-5 - ∞, 29, 42, 11, 58, 65, 74

Step-6 - ∞, 11, 29, 42, 58, 65, 74

16.

Write a user-defined function in C++ to display those elements of a two dimensional array T[4][4] which are divisible by 100. Assume the content of the array is already present and the function prototype is as follows: void Showhundred(int T[4][4]);

Answer»

void Showhundred(int T[4][4])

{

for(int I = 0; I<4; I++)

{

for(int J = 0; J<4; J++)

{

if (T[I][J]%100 = = 0)

cout<<"Elemets which are divisible by 100 are:”

<<A[I][J]<<endl;

}

}

}

17.

Write a function SORTPOINTS() in C++ to sort an array of structure Game in descending order of Points using Bubble Sort.Note. Assume the following definition of structure Gamestruct Game { long PNo;   //Player Numberchar PName[20];float Points;};Sample Content of the array (before sorting)PNoPName Points103Ritika Kapur3001104John Philip2819101Razia Abbas3451105Tarun Kumar2971Sample Content of the array (after sorting)RollNoName Score101Razia Abbas3451103Ritika Kapur3001105Tarun Kumar2971104John Philip2819

Answer»

void SORTPOINTS(Game G[ ], int N)

{

Game Temp;

for (int I=0; I<N-1;I++)

for (int J=0;J<N-I-1;J++)

if (G[J].Points <G[J+1].Points)

{

Temp = G[J];

G[J] = G[J+1];

G[J+1] = Temp;

}

}

18.

Define a function SWAPCOL() in C++ to swap (interchange) the first column elements with the last column elements, for a two dimensional integer array passed as the argument of the function. Example: If the two dimensional array contents2149137758637212After swapping of the content of 1st column and last column, it should be: 9142737138652217

Answer»

void SWAPCOL(int A[ ][100], int M, int N)

{

int Temp, I;

for (I=0;I<M;I++)

{

Temp = A[I][0];

A[I][0] = A[I][N-1];

A[I][N-1] = Temp;

}

}

19.

Write a DSUM() function in C++ to find sum of Diagonal Elements from N x M Matrix. (Assuming that the N is a odd numbers)

Answer»

int DSUM(int A[],int N)

{ int i,dsum1=0,dsum2=0;

for(i=0;i<N;i++)

{ dsum1+=A[i][i];

dsum2+=A[N-(i+1)][i];

}

return(dsum1+dsum2-A[N/2][N/2]);

//because middle element is added twice

}

20.

Write an algorithm to subtract a matrix A[4 x 4] from a matrix X[4 x 4]

Answer»

/* Read the two matrices */ 

1. for i=1 to 4 perform step 2 through 4 

2. { for j=1 to 4 perform step 3 through 4 

3. { Read A[i,j] 

4. Read B[i,j] 

/* Calculate the sum of the two and store it in third matrix C */

5. for i=1 to 4 perform step 6 through 8

{

6. for j=1 to 4 perform step 7 through 8 

7. { C[i,j]=0

8. C[i,j]=A[i,j]-B[i,j]

}

 }

21.

Write a user-defined function in C++ to find and display the sum of both the diagonal elements of a two dimensional array MATRIX[6][6] containing integers.

Answer»

float diagonalSum(float MATRIX[6][6], int r, int c)

{

int i,j;

float sum=0;

//We are calculating sum of diagonal elements considering both diagonals

//We are adding intersecting element on two diagonal twice

for(i=0;i<r;i++)

{

for(j=0;j<c;j++)

{

if(i==j) //elements on first diagonal

sum+= MATRIX [i][j];

if((i+j)==(r-1)) // elements on off-diagonal

sum+= MATRIX [i][j];

}

}

return sum;

}

22.

Write an algorithm to print all those elements of a matrix X[4 x 4] that are not diagonal elements

Answer»

Students I am giving you the program for printing Non Diagonal elements of a matrix X[4x4], try to convert this code into algorithm.

#include<conio.h>

#include<iostream.h>

void accept(int a[4][4],int size)

{

cout<<"Diagonal One:";

for (int i=0;i<size;i++)

for(int j=0;j<size;j++)

if (i != j && i != size-j-1)

cout<<a[i][j];

}

void main()

{

int a[4][4]={{5,4,3,4},{6,7,9,1},{8,0,3,7},{2,4,5,9}

};

clrscr();

accept(a,4); 

getch();

}

23.

Each element of two-dimensional array (with 5 rows and 4 columns) is stored in one memory location. If A(1,1) is at location 2000, what is the address of A(4,4) ? The arrangement is row-major. Use a suitable formula for the calculation.

Answer»

A[5][4] => rows R=5, columns C=4

Let base address be B

Given element width W=1 bytes and A[1][1]=2000

In Row major,

A[I][J]=B+W(C(I=lr)+(j-lc))

where lr=lowest row and lc=lowest column 

A[1][1]=B+W(C(1-0)+(1-0))

2000=B+1(4(1)+1)

2000=B+5

Base Address B=2000-5=1995

Using same formula

 A[4][4]=1995+1(4(4-0)+(4-0)) 

=1995+20

=2015

24.

In array A[n], after deletion of ay element, no element was shifted, thus, the free space is scattered across the array. You have been given the task to solve this problem. Write an algorithm to combine all the elements at the rear end of the array so that all the free spaces are available at the beginning of the array.

Answer»

1. ctr=pos

2. Repeat steps 3 and 4 until ctr<=1

3.  A[ctr]=A[ctr-1]

4.  ctr=ctr-1

/* End of Repeat*/

5. Display the new list of element 

6. End

25.

What are data structures? What are their types and sub-types? Explain each of the subtypes with examples.

Answer»

The data structures are named group of data of some data types.

The data structures can be classified into following two types: 

1. Simple Data Structure: These data structure are normally built from primitive data types like integers, reals, characters, boolean. Simple data structure can be classified into following two categories:

(a) Array: Arrays refer to a named list of a finite number n of similar data elements. For example, int ARR[10]; Above array ARR have 10 elements, each elements will be referenced as Arr[0], ARR[1]………….ARR[9].

(b) Structure: Structure refers to a named collection of variables of different data types. For example, a structure named as STUD contais (Rno, Name, Mark), then individual fields will be referenced as STUD.fieldname such as, STUD.Rno, STUD.Name etc. 

2. Compound Data Structure: Simple data structure can be combine in various waus to form more complex structures called compound data structures which are classified into following two categories:

(a) Linear data structure: These data structures are a single level data structures representing linear relationship among data. Following are the types of linear data structure:

(i) Stacks: Stack is a LIFO (Last In First Out) list. For example, stack of plates on counter, as that plates are inserted or removed only from the top of the stack.

(ii) Queue: Queue is a FIFO (First In First Out) list. For example, line of people waiting for their turn to vote.

(iii) Linked List: Linked lists are special lists of some data elements liked to one another. For example, peoples seating in a cinema hall where each seat is connected to other seat.

(b) Non-Linear data structure: These data structures are multilevel data structure representing hierarchical relationship among data. For example, relationship of child, parent and grandparent.

26.

An array MAT[30][10] is stored in the memory column wise with each element occupying 8 bytes of memory. Find out the base address and the address of element MAT[20][5], if the location of MAT[5][7] is stored at the address 1000.

Answer»

Base Address B

No of rows m=30

Element size W=8

Lowest Row and column indices lr, lc=0 

Address of Ith, jth element of array in column major order is: 

Address of MAT[I][J] = B + W(m(J - lc) + (I - lr))

MAT[5][7] = 1000

1000 = B + 8(30(7-0)+(5-0)) 

1000 = B + 8(30(7)+(5)) 

1000 = B + 8(210 + 5) 

1000 = B + 8(215) 1000 = B + 1720

B = 1000 – 1720

B = -720

Base address is -720

Now address of MAT[20][5] is computed as:

MAT[20][5] = -720 + 8(30(5 – 0) + (20 – 0))

= -720 + 8(30(5) + (20))

= -720 + 8(150 + 20)

= -720 + 8(170)

= -720 + 1360

= 640

27.

An array T[50][20] is stored in the memory along the column with each element occupying 4 bytes. Find out the base address and address of the element T [30][15], if the element T[25][10] is stored at the memory location 9800.

Answer»

T[50][20]

No. of Rows(i.e., R) = 50

No. of Cols(i.e., C) = 20

Element size(W) = 4 bytes

T[I][J] = T[30][15] => I=30, J=15

Address of T[25][10] = 9800

Base Address (B) =?

Lowest Row (i.e., lr) = 0

Lowest Col (i.e., lc) = 0

Formula to calculate address in Column Major arrangement is:

T[P][Q] = B + W[(P - lr) + R(Q - lc )] 

T[25][10] = B + 4((25 – 0) + 50(10 – 0))

9800 = B + 4(525) (∵ T[25][10] = 9800 given)

9800 = B + 2100

=> B = 9800 – 2100 

= 7700 Parallely, T[I][J] = B + W[(I - lr ) + R(J - lc )] 

T[30][15] = 7700 + 4[(30 – 0) + 50(15 – 0)] 

= 7700 + (4 x 780) 

= 7700 + 3120

= 10820

28.

Define a function SWAPARR() in C++ to swap (interchange) the first row elements with the last row elements, for a two dimensional integer array passed as the argument of the function.Example: If the two dimensional array contents5632124925819758After swapping of the content of 1st column and last column, it should be:9758124925815632

Answer»

void SWAPARR (int A[100][], int M, int N)

{

int Temp, I;

for (I=0;I<M;I++)

{

Temp = A[0][I];

A[0][I] = A[N-1][I];

A[N-1][I] = Temp;

}

}

29.

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 and integer 1 to show the presence of the number in the array. The function should have three parameters: (1) an array P (2) the number DATA to be searched and (3) the number of elements N.

Answer»

int bsearch(float P[10],int DATA,int N)

{ int beg=0,last=N-1,mid;

while(beg<=last)

{ mid=(beg+last)/2;

if(P[mid]==DATA) return 1; //element is present in array 

else if(DATA>P[mid]) beg=mid+1;

else last=mid-1;

}

return 0;  //element is absent in array

}

30.

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

Answer»

void ElementSwap(int A[],int size)

{ int lim,tmp;

if(size%2!=0)  //if array has odd no. of element

lim = size-1;

else

lim = size;

for(int i=0;i<lim;i+=2)

{  tmp = A[i];

A[i] = A[i+1];

A[i+1]=tmp;

}

}

31.

Write a user defined function in C++ to display the sum of column elements of two dimensional array R[7][7] containing integers.

Answer»

void COLSUM(int R[7][7])

{int SUMC;

for (int j=0;j<7;j++)

{

SUMC=0;

for(int i=0;i<7;i++)

SUMC=SUMC + R[i][j];

Cout<< "Sum of Column "<<j<<" = "<<SUMC ;

}

}

32.

Write a function REASSIGN() in C++, which accepts an array of integer and its size as parameters and divide allthose array elements by 5 which are divisible by 5 and multiply other array element by 2. Sample Input Data of the arrayA[0]A[1]A[2]A[3]A[4]2012156032Content of the array after calling REASSIGN() functionA[0]A[1]A[2]A[3]A[4]42431264

Answer»

void REASSIGN (int Arr[ ], int Size)

{

for (int i=0;i<Size;i++)

if (Arr[i]%5==0)

Arr[i]/=5;

else

Arr[i]*=2;

}

33.

Write a function in C++, which accepts an integer array and its 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 nine elements initially contains the elements as 3, 4, 5, 16, 9 then the function should rearrange the array as 9, 8, 15, 32, 27

Answer»

void RearrangeArray(int A[],int size)

{

for(int i=0;i<size;i++)

{ if(A[i]%2==0)

A[i]*=2;

else

A[i]*=3;

}

}

34.

 Write a function in C++ to print the product of each row of a two dimensional integer array passed as the argument of the function.Explain: if the two dimensional array contains204010405030603020402030Then the output should appear as:Product of Diagonal 1 = (1 x 5 x 2 x 4)=40Product of Diagonal 2 = (3 x 6 x 3 x 2)=108

Answer»

void RowProduct(int A[4][3],int R,int C)

{ int Prod[R];

for(int i=0;i<R;i++)

{ Prod[i]=1;

for(int j=0;j<c;j++)

Prod[i]*=A[i][j];

cout<<"Product of row"<<i+1<<"="<<Prod[i]<<endl;

}

}

35.

Write a function in C++ to print the product of each column of a two dimensional integer array passed as the argument of the function.Explain: if the two dimensional array containsThen the output should appear as:124356432215Product of Column 1 = 24Product of Column 2 = 30Product of Column 3 = 240

Answer»

void ColProd(int A[4][3],int r,int c)

{ int Prod[C],i,j;

for(j=0;j<c;j++)

{ Prod[j]=1;

for(i=0;i<r;i++)

Prod[j]*=A[i][j];

cout<<"Product of Column" <<j+1<<"="<<Prod[j]<<end1;

}

}