MENU

Chapter 8 Arrays Solutions

Question - 1 : -
Observe the following C++ code and find out, which out of the given options (i) to (iv) are the expected correct output. Also assign the maximum and minimum value that can be assigned to the variable тАШGoтАЩ.

void main()
┬а{ int X [4] = {100, 75, 10, 125};
┬аint Go = random(2) + 2;
┬аfor (int i = Go; i<4; i ++)
┬аcout<
┬а}
(i) 100$$75
(ii) 75$$10$$125$$
(iii) 75$$10$$
(iv) 10$$125$

Answer - 1 : -

(iv) is correct option
Max. Value = 3
Min. Value = 2

Question - 2 : -
Write the difintion of a function grace_score (int score [], int size) in C ++,which should check all the elements of the array and give an increase of 5 to those scores which are less than 40.
Example: if an array of seven integers is as follows:
45,35,85,80,33,27,90
After executing the function, the array content should be changed as follows :
45,40,85,80,38,32,90

Answer - 2 : -

void grace_score(int score[].int size)
┬а{
┬аfor(int i=0;i
┬а{
┬аif (score [i]<40)
┬аscore [i]=score[i]+5;
┬аcout< }
┬а}

Question - 3 : -
Find the output of the following C+ + program:
#include void repch(char s[]) { for(int i=0;s[i]!=тАЩ\0тА▓;i++) { if(((i%2)!=0) &&(s[i]!=s[i + 1])) { S[i]=тАЩ@тАЩ; cout<<тАЬHelloтАЭ; } else if (s[i]==s [i + 1]) { s[i+1]=тАЭ ! тАЭ ; i++ ; } } } void main() { char str[]=тАЭSUCCESSтАЭ; cout<<тАЬOriginal StringтАЭ<

Answer - 3 : -

Original String SUCCESS
Changed String S@C!ES!

Question - 4 : -
Find the output of the following:

┬а#include
┬аvoid switchover (int A[],int N, int split)
┬а{
┬аfor(int K=0; K
┬аif(K
┬аA [K] + = K;
┬аelse
┬аA[K] *=K; }
┬аvoid display(int A [],int N)
┬а{
┬аfor (in K=0; K
┬а(K%2==0)? Cout<
┬аcout<
┬а}
┬аvoid main()
┬а{ int H [ ] = {30, 40, 50, 20, 10, 5};
┬аswitchover(H, 6, 3);
┬аdisplay(H, 6);
┬а}

Answer - 4 : -

30%41
52% 60
40% 25

Question - 5 : -
An integer array A [30] [40] is stored along the column in the memory. If the element A[20][25] is stored at 50000, find out the location of A[25] [30].

Answer - 5 : -

A[i] [j] = B+W x [No. of rows x (I-Lr) + (J-Lc)]
┬аA[20] [25] = B + 2x [30x (20-0) + (25-0)]
┬а50000 = B+2x[30x(20-0)+(25-0)]
┬аB = 48750
┬аA[7] [10] = 48750+ 2x[30x(7-0)+(10-0)]
┬а= 49190

Question - 6 : -
An array P[30][20] is stored along the column in the memory with each element requiring 2 bytes of storage. If the base address of the array P is 26500, find out the location of P[20][10].

Answer - 6 : -

Total number of rows = 30
Total size = 2 bytes
Base Address = 26500
LOC(P[I][J]) = Base Address+((I-LBR)+(J- LBC)*R)W
Assuming Lower Bound of Row(LBR)=0
Lower Bound of Column(LBC)=0
Total number of Rows(R)=30
Size of each element(W)=2
LOC(P[20][10]) = 26500 + ((20-0)+(10-0)*30)*2
LOC(P[20][10]) = 26500 + 640
LOC(P[20][10]) = 27140

Question - 7 : -
Write a function to sort any array of n elements using insertion sort. Array should be passed as argument to the function.

Answer - 7 : -

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+1]=a[ptr] ; //Move Element
┬аForward
┬аptr--;
┬а}
┬аa[ptr+1]=temp; //Insert Element in Proper Place
┬а}

Question - 8 : -
Write a function NewMAT(int A[][],int r, int c) in C++, which accepts a 2d array of integer and its size as parameters divide all those array elements by 6 which are not in the range 60 to 600(both values inclusive) in the 2d Array.

Answer - 8 : -

void NewMAT(int A[] [] , int r,int c)
┬а{
┬аfor (int i = 0; i
┬аfor (j=0; j=60)&&(A[1] [j)<=600))
┬аA[i][j]/=6
OR

A[i] [j] =A[i] [j] /6;
┬а}

Question - 9 : -
A two dimensional array P[20] [50] is stored in the memory along the row with each of its element occupying 4 bytes, find the address of the element P[10] [30], if the element P[5] [5] is stored at the memory location 15000.

Answer - 9 : -

Loc (P [I] [J] along the row =BaseAddress+W [(I-LBR)*C+ (J-LBC)]
(where C is the number of columns, LBR = LBC =0)
LOC (P [5] [5]) = BaseAddress + W* [I*C + J]
15000 = BaseAddress + 4 * [5 * 50 + 5]
= BaseAddress + 4 * [250 + 5]
= BaseAddress + 4*255 = BaseAddress + 1020
BaseAddress = 15000 -1020 = 13980
LOC (P [10] [30]) = 13980 + 4* [10 * 50 + 30]
= 13980 + 4 * 530
= 13980 + 2120
= 16100
OR LOC (P [10] [30])
= Loc (P[5] [5]) + W[ (I-LBR) * C+ (J-LBC)]
= 15000 + 4 [(10 тАУ 5) * 50 + (30 тАУ 5)]
= 15000 + 4 [5 * 50 + 25]
= 15000 + 4 *275
= 15000 + 1100
= 16100
OR
(where C is the number of columns and LBR = LBC = 1)
LOC (P [5] [5])
15000 = BaseAddress + W [(I-1) *C + (J-1)]
= BaseAddress + 4 [4*50 + 4]
= BaseAddress + 4 [200 + 4]
= BaseAddress + 4 * 204
= BaseAddress + 816
BaseAddress = 15000 тАУ 816 = 14184
LOC (P [10] [30])
= 14184 + 4 [(10 -1) * 50 + (30 -1)]
= 14184 + 4 [9*50 + 29]
= 14184 + 4*479
= 14184 + 1916
= 16100

Question - 10 : -
Write a user-defined function DisTen (int A[] [4], int N, int M) in C++ to find and display all the numbers, which are divisible by 10. For example if the content of array is :

12 20 13
2 10 30
The output should be
20 10 30

Answer - 10 : -

void DisTen (int A[] [4] , int N, int M)
┬а{
┬аint i, j;
┬аfor (i=0; i
┬а{
┬аfor (j = 0 ; j
┬а{
┬аif (A[i][j]%10==0)
┬аcout << A [i] [ j] ;
┬а}
┬а}
┬а}

×