MENU

Chapter 8 Arrays Solutions

Question - 21 : -
Write a user-defined function swap_row(int ARR[][3],int R,int C) in C++ to swap the first row values with the last row values : For example:

10 20 30
40 50 60
70 80 90
Then after function call,the content of the array should be:

70 80 90
40 50 60
10 20 30

Answer - 21 : -

voidswap_row (intARR [] [3] , int R,int() )
 {
 for(int i=0 J=0: J
 {
 int temp=ARR[i][j];
 ARR[i][j]=ARR[R-1][j];
 ARR[R-1][j]=temp;
 }
 }

Question - 22 : -
Write a user-defined function SumLast 3 (int A [] [4], int N, int M) in C + + to find and display the sum of all the values, which are ending with 3 (i.e., units place is 3). Fdr example if the content of array is :

33 13 92
99 3 12
The output should be 49

Answer - 22 : -

void SumLast 3 (int A[] [4], int N, int M)
 {
 int Sum=0;
 for(int i=0; i
 for(int j = 0; j
 {
 If (((A[i] [j]-3)% 10==0))
 Sum=Sum+A[i][j];
 }
 cout<
 }

Question - 23 : -
Write a user-defined function AddEnd2(int A[] [4], int N, int M) in C+ + to find and display the
sum of all the values, which are ending with 2 (i.e., units place is 2).
For example, if the content of arrray is :

22 16 12
19 5 2
The output should be 36

Answer - 23 : -

void AddEnd2(int A[] [4], int N, int M)
 {
 int Sum=0;
 for(int i=0; i
 for(int j = 0; j
 if(((A[i] [j]-2)%10 = = 0))
 Sum=Sum + A[i] [j] ;
 }
 cout<
 }

Question - 24 : -
Write a function SKIPEACH(int H[] [3], int C, int R) in C+ + to display all alternate elements from two-dimensional array H (starting from H [0] [0]).
For example:
If the array is containing:
12 45 67
33 90 76
21 43 59
The output will be
12 67 90 21 59

Answer - 24 : -

void SKIPEACH (int H[ ] [3], int C, int R)
 {
 for (int i = 0; i C; i + + )
 { for (int j = 0, j < R; j++)
 {
 if ( (i + j)%2 = =0)
 cout<
 }
 }

Question - 25 : -
Given an array A[10][12] whose base address is 10000. Calculate the memory location of A[2][5] if each element occupies 4 bytes and array is stored columnwise.

Answer - 25 : -

w=4
b=10000
m=10
n[i][i] = [b+ w(i-l1) + m(j-l2)
n[2][5] = [10000+ 4(2-0) + 10(5-0)]
n[2][5] = [10000 +8 +50 ]
n[2][5] = 10058

Question - 26 : -
An array T[-1..35][-2..15] is stored in the memory along the row with each element occupying 4 bytes. Find out the base address and address of element T[20][5], if an element T[2][2] is stored at the memory location 3000. Find the total number of elements stored in T and number of bytes allocated to T.

Answer - 26 : -

Take T[2][2] as the base address [BA],
sr=starting row, sc=starting column;
Along the row
A[i][j] = BA + size[nc*(i-sr) + (j-sc)]
T[20][5] = 3000 + 4 [ 18 * (20-2) + (5-2)]
= 3000 + 4[(18 * 18) + (3)]
= 3000 + 4[324 + 3]
= 3000 + 4(327)
= 3000 + 1308
= 4308
nc = no. of columns sr=2 sc=2
Total number of elements stored in T=37*18=666
Total number of bytes allocated to T=666*4=2664

Question - 27 : -
An array A[20] [30] is stored along the row in the memory with each element requiring 4 bytes of storage. If the base address of array A is 32000, find out the location of A[15][10]. Also, find the total number of elements present in this array.

Answer - 27 : -

Array A[20] [30]
Base Address = 32000
Size of element(W)=4 bytes
A[I][J]=A[15][10] hence I=15 and J=10
Total Row (R)=20
Total Column (C)=30
A [I] IJ]=B + W[C(I-0)+(J-0)]
A[15][10]=32000 +4[30(15-0)+(10-0)]
= 32000+4[30(15)+10]
= 32000+4[450+10]
= 32000+4[460]
= 32000+1840
= 33840

Question - 28 : -
An array T[25][20] is stored along the row in the memory with each element requiring 2 bytes of storage. If the base address of array T is 42000, find out the location of T[10][15]. Also, find the total number of elements present in this array.

Answer - 28 : -

Array A[25] [20]
Base Address = 42000
Size of element(W)= 2 bytes
A[I][J]=A[10][15] hence I=10 and J=15
Total Row (R) = 25
Total Column (C)= 20
A [I] [J] =B+ W[C(I-0)+(J-0)]
A[10] [15] =42000 +2[20(10-0)+(15-0)]
= 42000+2(20(10+15)]
= 42000+2[200+15]
= 42000+2(215]
= 42000+430]
= 42430

Question - 29 : -
Each element of the array A[8][6] is stored using 4 bytes of memory. If the element A[2][4] is stored at location 936, find the address of A[5][1]. Assume that the array is stored in Row Major.

Answer - 29 : -

nr=8 nc=6 size = 4 bytes
A[2][4] = 936 A[5][1] = ?
Formula for row-major
A[i][j] = BA + size * ((i – sr) * nc + (j – sc))
A[5][1] = A[2][4] + 4 * ((5 – 2) * 6 + (1 – 4))
= 936 + 4* ((3) *6-3)
= 936 + 4 * 15
= 936 + 60
A[5][l] = 996

×