Question 1: Write the definition of a function AddUp(int Arr[ ], int N) in C++, in which all even positions (i.e. 0,2,4 ) of the array should be added with the content of the element in the next position and odd positions (i.e. 1,3,5, ) elements should be incremented by 10. All India 2017
NOTE
- The function should only alter the content in the same array.
- The function should not copy the altered content in another array.
- The function should not display the altered content of the array.
- Assuming, the Number of elements in the array are Even.
Аnswer:
void AddUp(int Arr[], int N) { for(int i=0;i<N;i++) { if(i%2==0) Arr[i] += Arr[i+1]; else Arr[i] += 10; } } Also Check: CBSE Class 12 Computer Science Syllabus
Question 2: Write the definition of a function FixPay(float Pay[ ], int N) in C++, which should modify each element of the array Pay having N elements, as per the following rules:
Аnswer:
void FixPay(float Pay[], int N) { for(int i=0;i<=N—1;i++) { if(Pay[i]<100000) Pay[i] = Pay[i]+(Pay[i]*25)/100; else if(Pay[i]>=100000 && Pay[i]<200000) Pay[i] = Pay[i]+(Pay[i]*20)/100; else Pay[i] = Pay[i]+(Pay[i]*15)/100; } }
Also Check: CBSE Class 12 Physical Education Syllabus
Question 3: Write the definition of a function FixSalary(float Salary[ ], int N) in C++, which should modify each element of the array Salary having N elements, as per the following rules:
Аnswer:
void FixSalary(float Salary[], int N) { for(int i=0;i<=N—1;i++) { if(Salary[i]<100000) Salary[i]=Salary[i]+(Salary[i]*35)/100; else if(Salary[i]>=100000 && Salary[i]<200000) Salary[i]=Salary[i]+(Salary[i]*30)/100; else Salary[i]=Salary[i]+(Salary[i]*20)/100; } }
Also Check: CBSE Class 12 Physics Syllabus
Question 4: Write the definition of a function Alter (int A[ ], int N) in C++, which should change all the multiples of 5 in the array to 5 and rest of the elements as 0. e.g. if an array of 10 integers is as follows:
Аnswer:
void Alter(int A[], int N) { for(int i=0;i<10;i++) { if(A[i]%5==0) A[i]=5; else A[i]=0; } }
Question 5: Write the definition of a function Changeant P[ ], int N) in C++, which should change all the multiples of 10 in the array to 10 and rest of the elements as 1. All India 2015
Аnswer:
void Change(int P[], int N) { for (int i=0;i<N;i++) { if (P[i]%10 == 0) P[i] = 10; else P[i] = 1; } }
Also Check: CBSE Class 12 Math Syllabus
Question 6: Write the definition of a function Modify(int A[ ], int N) in C++, which should reposition the content after swapping each adjacent pair of numbers in it.
[NOTE Assuming the size of array is multiple of 4]
Аnswer:
void Modify(int A[], int N) { for(int i=0;i<N;i++) { int temp = A[i]; A[i] = A[i+2]; A[i+2] = temp; if(i%4>=1) i = i+2; } }
Also Check: CBSE Class 12 English Syllabus
Question 7: Write code for a function void 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.
Аnswer:
void oddEven(int S[],int N) { for(int i=0;i<N;i++) { if(S[i]%2 == 0) S[i] += 10; else S[i]+=5; } }
Also Check: CBSE Class 12 Biology Syllabus
Question 8: 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.
Аnswer:
void EvenOdd(int T[],int C) { for(int i=0;i<C;i++) { if(T[i]%2 == 0) T[i] += 2; else T[i] += 1; } }
Question 9: 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. All India 2014
Аnswer:
void TW0T00NE(int X[], int Y[], int n) { int Z[40], i,j=0,k=0; for(i=0;i<(n+n);i++) { if(i%2 == 0) { Z[i] = X[j]; j++; } else { Z[i] = Y[k]; k++; } } }
Question 10: Write code for a function void Convert (int T[ ], int Num) in C++, which repositions all the elements of the array by shifting each of them one to one position before and by shifting the first element to the last position.
Аnswer:
void Convert(int T[], int Num) { int temp = T[0]; for(int i=0;i<(Num-1);i++) { T[i]=T[i+l]; } T[i]= temp; }
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.
Аnswer:
void ChangeOver(int P[],int N) { int temp; for(int i=0;i<(N-1);i++) { temp = P[N-1]; P[N-1] = P[i]; P[i] = temp; } }
Question 12: Write the definition for a function void Transfer (int A[6], int B[6]) in C++, which takes two integer arrays, each containing 6 elements as parameters. The function should exchange all odd places (i.e. 3rd and 5th) of the two arrays.
Аnswer:
void Transfer(int A[6],int B[6]) { int i,temp; for(i=1;i<=5;i=i+2) { temp = A[i]; A[i] = B[i]; B[i] = temp; } }
Question 13: Write a function SWAP2CHANGE(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.
e.g. 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
Аnswer:
void SWAP2CHANGE(int p[],int N) { int C,i; for(i=0;i<=N-2;i++) { if(p[i]%10 == 0) { C = p[i]; p[i] = p[i+1]; p[i+1] = C; i++: } } }
Question 14: 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.
e.g. If the contents of array ARR are
90, 56, 45, 20, 34, 54
The contents of array should become
56, 90, 45, 34, 20, 54 All India 2012
Аnswer:
void SWAP2BEST(int ARR[], int Size) { int i,C; for(i=0;i<=Size-2;i++) { if(ARR[i]%10==0) { C=ARR[i]; ARR[i] = ARR[i+1]; ARR[i+1] = C; i++; } } }
Question 15: Write a Get2From1( ) function in C++ to transfer the content from one array ALL[ ] to two arrays Odd[ ] and Even[ ]. The Even[ ] array should contain the values from places(0, 2, 4, …) of array ALL[ ] and Odd[ ] array should contain the values from odd position like (1, 3, 5, …).
e.g. The ALL[ ] array should contain 30,10, 60, 50, 90, 80
If the Even[ ] array contains 30, 60, 90
and the Odd[ ] array contains
10, 50, 80 All India 2011
Аnswer:
void Get2From1(int ALL[], int N) { int p,q,i,j=0,k=0; if(N%2 == 0) { p = N/2; q = N/2; } else { p = N/2; q = ((N/2)+l); } int *0dd = new int[p]; int *Even = new int[q]; for(i=0;i<(N);i++) { if(i%2 == 0) Even[j++]=ALL[i]; else Odd[k++]=ALL[i]; } }
Question 16: Write a function CHANGE( ) in C++, which accepts an array of integer and its size as parameters and divide all those array elements by 7, which are divisible by 7 and multiply other array elements by 3.
Аnswer:
void CHANGE(int A[],int N) { for(int i=0;i<N;i++) if(A[i]%7 == 0) A[i]=A[i]/7; else } A[i] = A[i]*3; } }
Question 17: Write a function REASSIGN( ) in C++, which accepts an array of integer and its size as parameters and divide all those array elements by 5, which are divisible by 5 and multiply other array elements by 2.
Аnswer:
void REASSIGN(int A[],int N) { for(int i=0;i<N;i++) { 1f(A[i]%5 == 0) A[i] = A[i]/5; else A[i] = A[i]*2; } }
Question 18: Write a function SORTPOINTS( ) in C++ to sort an array of structure Game in descending order points using bubble sort.
NOTE Assume the following definition of structure Game.
struct Game { long PNo; //Player Number char PName[20]; long Points; }
Аnswer:
void SORTPOINTS(struct Game G[], int n) { int i, j; struct Game t; for(i=1;i<n;i++) { for(j=0;j<=n-i-1;j++) { if(G[j+1].Points>G[j].Points) { t = G[j]; G[j] = G[j+1]: G[j+1] = t; } } } }
Question 19: Write a function SORTSCORE( ) in C++to sort an array of structure. Examinee in descending order of Score using bubble sort.
NOTE Assume the following definition of structure Examinee.
struct Examinee { long Roll No; char Name[20]; float Score; };
Аnswer:
void SORTSCORE(struct Examinee ex[],int n) { int i,j; struct Examinee t; for(i=1;i<n;i++) { for(j=0;j<=n-i-1;j++) { if(ex[j+1].Score>ex[j].Score) { t = ex[j]; ex[j] = ex[j+1]; ex[j+1] = t; } } } }
Topic – 2
Two-Dimensional Array
2/3 Marks Questions
Question 1: Write a definition for a function SUMMIDCOLfint MATRIX[ ][10], int N, int M) in C++, which finds the sum of the middle column’s elements of the MATRIX (Assuming N represents number of rows and M represents number of columns, which is an odd integer). All India 2017
Example: If the content of array MATRIX having N as 5 and M as 3 is as follows:
The function should calculate the sum and display the following:
Sum of Middle Column : 15
Аnswer:
void SUMMIDCOL(int MATRIX[][10],int N,int M) { int j, SUM=0; j=M/2; for(int i=0;i<N;i++) SUM += MATRIX[i][j]; cout<<"SUM of Middle Column:"<<SUM; }
Question 2: ARR[15][20] is a two-dimensional array, which is stored in the memory along the row with each of its elements occupying 4 bytes. Find the address of the element ARR[5][15], if the element ARR[10][5] is stored at the memory location 35000. All India 2017
Аnswer:
B = 35000, W = 4 bytes, R=15, C=20, Lr=10, Lc=5, I=5, J=15 For row-wise allocation, Address of ARR[I][J] = B+W[C(I-Lr)+(J-Lc)] ARR[5][15] = 35000+4[20(5-10)+(15-5)] = 35000+4[20(-5)+10] = 35000+4[-100+10] = 35000+4[-90] = 35000-360 = 34640
Question 3: Write definition for a function DISPMID (int A[ ] [5], int R, int C) in C++ to display the elements of middle row and middle column from two dimensional array A having R number of rows and C number of columns.
Аnswer:
void DISPMID(int A[][5], int R, int C) { int i; i=R/2; for(int j=0;j<C;j++) cout<<A[i][j]<<'\t'; count<<endl; i=C/2; for(j=0;j<R;j++) cout<<A[j][i]<<'\t'; }
Question 4: R[10][50] is a two dimensional array, which is stored in the memory along the row with each of its element occupying 8 bytes, find the address of the element R[5][15], if the element R[8] [10] is stored at the memory location 45,000. All India 2016
Аnswer:
B = 45000, R = 10, C = 50. W = 8 bytes, Lr = 8, Lc = 10, I = 5, J = 15 For row-wise allocation, R[I][J] = B + W[C(I-Lr)+(J-Lc)] R[5][15] = 45000 + 8[50(5-8)+(15-10)] = 45000 + 8[50x(-3)+5] = 45000 + 8[-150+5] = 45000 - 1160 = 43840
Question 5: Write definition for a function SHOWMID(int P[ ][5], int R, int C) in C++ to display the elements of middle row and middle column from a two dimensional array P having R number of rows and C number of columns, e.g. if the content of array is as follows:
The function should display the following as output:
103 101 121 102 101
116 121 109 Delhi 2016
Аnswer:
void SHOWMID(int P[][5], int R, int C) { int i,j; i=R/2; for(j=0;j<C;j++) cout<<P[i][j]<<'\t'; Cout<<endl; i=C/2; for(j-0;j<R;j++) cout<<P[j][i]<<'\t'; }
Question 6: T[20] [50] is a two dimensional array, which is stored in the memory along the row with each of its element occupying 4 bytes, find the address of the element T[15][5], if the element T[10][8] is stored at the memory location 52000. Delhi 2016
Аnswer:
B = 52000, R = 20, Ir= 10, C = 50 Ic = 8, W = 4bytes, I = 15, J = 5 For row-wise allocation, T[I][J] = B+W[C(I-Lr)+(J-Lc)] T[15][5] = 52000 + 4[50(15-10)+(5-8)] = 52000 + 4[50x5+(-3)] = 52000 + 4[250-3] = 52000 + 4 x 247 = 52000 + 988 = 52988
Question 7: Write definition for a function SHOWMID(int P[ ][5], int R, int C) in C++ to display the elements of first, third and fifth column ffom a two dimensional array P having R number of rows and C number of columns.
For example, if the content of array is as follows:
Аnswer:
void SHOWMID(int P[][5], int R.int C) { int i,j; for(j=0;j<C;j+=2) { cout<<endl; for(i=0;i<R;i++) { cout<<P[1][j]<" "; } } }
Question 8: Write a function REVROW (int P[ ][5], int N, int M) in C++ to display the content of a two dimensio) al array, with each row content in reverse order. All India 2015
Аnswer:
void REVR0W(int P[][5], int N, int M) { for(int i=0;i<N;i++) { int x=M-1; for(int j=0;j<M;j++) { if(J<x) { int t = P[i][j]; P[i][j] = P[i][x]; P[i][x]= t; x--; } } } for(i=0;i<N;i++) { for(int j=0;j<M;j++) cout<<P[i][j]<<""; cout<<endl; } }
Question 9: A two dimensional array ARR[50] [20] is stored in the memory along the row with each of its elements occupying 4 bytes. Find the address of the element ARR[30][10], if the element ARR[10][5] is stored at the memory location 15000.
All India 2015
Аnswer:
B = 15000, R = 50, C = 20,W = 4 bytes, Lr = 10. Lc = 5, I = 30, J = 10 For row-wise allocation, ARR[I][J] = B + W[C(I-Lr)+(J-Lc)] = 15000 + 4[20(30-10)+(10-5)] = 15000 + 4[20x20+5] = 15000 + 4[405] = 15000 + 1620 = 16620
Question 10: Write a function REVCOL (int P[ ][5], int N, int M) in C++ to display the content of a two dimensional array, with each column content in reverse order. Delhi 2015
NOTE Array may contain any number of rows.
Аnswer:
void REVCO(int P[][5], int N, int M) { for(int i=0;i<M;i++) { int X=N—1; for(int j=0;j<N;j++) { if(j<X) { int t = P[j][i]; P[j][i] = P[X][i]; P[X][i] = t; X--; } } } for(i=0;i<N;i++) } for(int j=0;j<M;j++) cout<<p[i][j]<<""; cout<<endl; } }
Question 11:
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.
Аnswer:
B=15000 R=20 C=50 W=4 bytes Lr=5, Lc=5, I=10, J=30 For row-wise allocation, P[I][J] = B + W[C(I-Lr)+(J-Lc)] P[10][30] = 15000 + 4[50(10-5)+(30-5)] = 15000 + 4[50(5)+25] = 15000 + 4[250 + 25] = 15000 + 4 x 275 = 15000 + 1100 = 16100
Question 12:
Write a function ADDDIAG (int A[ ][5], int N, int M) in C++ to display sum of the content, which at the diagonals of a two dimensional array.
[NOTE: Assume the array to be of even dimension such as 2×2,4×4, 6×6 etc.]
Аnswer:
void ADDDIAG(int A[][5],int N, int M) { int sum = 0; for(int i=0;i<N;i++) { for(int j=0;j<M;j++) { if(i == j) sum = sum + A[i][j]; else if(j == M-i-1) sum = sum + A[i][j]; } } cout<<sum; }
Question 13:
Write a user-defined function SumLast3 (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. unit place is 3).
Аnswer:
void SumLast3(int A[][4],int N.int M) { int sum=0; for(int r=0;r<N;r++) { for(int c=0;c<M;c++) { int rem = A[r][c]%10; if(rem == 3) sum += A[r][c]; } } cout<<sum; }
Question 14:
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. unit place is 2).
Аnswer:
void AddEnd2(int A[][4],int N.int M) { int sum = 0; for(int i=0;i<N;i++) { for(int j=0;j<M;j++) { if(A[i][j]%10 == 2) sum += A[i][j]; } } cout<<sum; }
Question 15:
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. Delhi 2014
Аnswer:
B = 42000 U = 2 bytes, C = 20, R = 25, Lr =0, Lc = 0, I = 10, J = 15 For row-wise allocation, Address of T[I][J] = B + W[C(I-Lr)+(J-Lc)] T[10][15] = 42000 + 2[20(10-0)+(15-0)] = 42000 + 2[20 x 10 + 15] = 42000 + 2[200 + 15] = 42000+215x2 = 42000+430 = 42430 Total number of elements present in this array = R x C = 25x20 = 500
Question 16:
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. All India 2014
Аnswer:
B = 32000, W = 4 bytes, R = 20, C = 30, Lr = 0 Lc = 0, I = 15, J = 10 For row-wise allocation, Address of A[I][J] = B + W[C(I-Lr)+(J-Lc)] A[15][10] = 32000 + 4[30(15-0)+(10-0)] = 32000+C(15x301+10] x 4 = 32000 + [460 x 4] = 32000+1840 = 33840 Total number of elements present in this array = R x C = 20 x 30 = 600
Question 17:
Write a function in C++ which accepts a 2D array of integers and its size arguments and displays the elements which lie on minor diagonal. [Top right to bottom left diagonal]
[Assuming the 2D array to be square matrix with odd dimension i.e. 3×3, 5×5, 7×7, etc….] All India (C) 2014
For example,
Аnswer:
void MinorDiagonal(int A[][10], int M, int N) { for(int i=0;i<M;i++) { for(int j=0;j<N;j++) { if(j==N—i—1) { cout<<A[i][j]<<endl; } } } }
Question 18:
Write a user-defined function DispTen(int A[ ][4], int N, int M) in C++ to find and display all the numbers, which are divisible by 10.
Аnswer:
void DispTen(int A[][4], int N, int M) { int i,j; for(i=0;i<N;i++) for(j=0;j<M;j++) if(A[i][j]%10 == 0) cout<<A[i][j]<<" "; }
Question 19:
Write a user-defined function DispNTen(int L[ ][4], int R, int C) in C++ to find and display all the numbers, which are not divisible by 10.
Аnswer:
void DispNTen(int L[][4],int R,int C) { int i,j; for(i=0;i<R;i++) for(j=0;j<C;j++) if(L[i][j]%10 != 0) cout<<L[i][j]<<" "; }
Question 20:
Write a user-defined function int SumSingle(int A[4] [4]) in C++, which finds and returns the sum of all numbers present in the first row of the array.
Then, the function should return 33.
Аnswer:
int SumSingle(int A[4][4]) { int Sum = 0; for(int j=0;j<=3;j++) { Sum = Sum + A[0][j]; } return Sum; }
Question 21:
An array P[15][10] is stored along the column in the memory with each element requiring 4 bytes of storage. If the base address of array P is 14000, find out the location of P[8][5]. Delhi 2013
Аnswer:
B = 14000, W = 4 bytes, R = 15, C = 10, Lr=0, Lc =0 I = 8, J = 5 For column-wise allocation, Address of P[I][J] = B + W[(I-Lr)+(J-Lc)R] P[8][5] = 14000 + 4[18-0)+(5-0)15] = 14000 + 4[8 + (5)15] = 14000 + 4[8 + 75] = 14000 + 4[83] = 14000 + 332 = 14332
Question 22:
An array T[15][10] is stored along the row in the memory with each element requiring 8 bytes of storage. If the base address of array T is 14000, find out the location of T[10][7]. All India 2013
Аnswer:
B = 14000, W = 8 bytes, R = 15, C = 10, Lr = 0, Lc = 0, I = 10, J = 7 For row-wise allocation, Address of T[I][J] = B + W[C(I-Lr)+(J-Lc)] T[10][7] = 14000 + 8[10(10-0)+(7-0)] = 14000+8[10(10) + 7] = 14000+8[100 + 7] = 14000+8[107] = 14000+856 = 14856
Question 23:
An array S[10] [15] is stored in the memory with each element requiring 2 bytes of storage. If the base address of array S is 25000, determine the location of S[5][10] if the array is S stored along the column. Delhi (C) 2013
Аnswer:
B = 25000, W = 2 bytes, R = 10, C = 15, Lr = 0, Lc=0, I = 5, J = 10 For column-wise allocation. Address of S[I][J] = B + W[(I-Lr)+R(J-Lc)] S[5][10] = 25000 + 2[(5-0)+1000-0)] = 25000 + 2[(5)+(10)10] = 25000 + 2[5+100] = 25000 + 2[105] = 25000 + 210 S[5][10] = 25210
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]).
Аnswer:
void SKIPEACH(int H[][3],int C,int R) { int i,j; for(i=0;i<=R-1;i++) { if(i%2==0) { j=0; } else { j=l: } whi1e(j<=C-1) { cout<<H[i][j]<<" "; j = j+2; } } }
Question 25:
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]).
Аnswer:
void ALTERNATE(int A[][3],int N.int M) { int i,j; for(i=0;i<=N-1;i++) { if(i%2==0) { j=0; } else { j=l; } while(j<=M-1) { cout<<A[i][j]<<" "; j+=2; } } }
Question 26:
An array S[10][30] is stored in the memory along the column with each of the element occupying 2 bytes. Find out the memory location of S[5][10], if the element S[2][15] is stored at the location 8200. Delhi 2012
Аnswer:
R = 10, C = 30, B = 8200, M = 2 bytes, I = 5, J = 10, Lr = 2, Lc= 15 For column-wise allocation, Address of S[I][J]=B + W[(I-Lr)+R(J-Lc)] S[5][10] = 8200+2(5-2)+10(10-15)] = 8200 + 2[3+10x(-5)] = 8200 + 2[3-50] = 8200 + 2x(-47) = 8200 - 94 = 8106
Question 27:
An array T[20] [10] is stored in the memory along the column with each of the element occupying 2 bytes. Find out the memory location of T[10][5], if the element T[2] [9] is stored at the location 7600. All India 2012
Аnswer:
R = 20, C = 10, B = 7600, M = 2 bytes, I = 10, J = 5, Lr = 2, Lc =9 For column-wise allocation, Address of T[I][J] = B + W(I-Lr)+R(J-Lc)] T[10][5] = 7600 + 2(10-2)+20(5-9)] = 7600 + 2[8 + 20 x (-4)] = 7600 + 2[8-80] = 7600 + 2(-72) = 7600 - 144 = 7456
Question 28:
Write a COLSUM function in C++ to find sum of each column of a N*M matrix. Delhi 2011
Аnswer:
void C0LSUM(int A[4][3],int r,int c) { int Sum[10],i,j; for(j=0;j<c;j++) { Sum[j] = 0; for(i=0;i<r;i++) Sum[j] += A[i][j]; cout<<"Sum of columns"<<j+1<<"="<<Sum[j]<<endl; } }
Question 29:
Write a DSUM function in C++ to find sum of diagonal element of a N*N matrix. All India 2011
Аnswer:
void DSUM(int A[][3],int n) { int i,j,sum=0; cout<<"\nSum of Diagonal One"; for(i=0;i<n;i++) { sum += A[i][i]; } cout<<sum<<" "; sum=0; cout<<"\nSum of Diagonal Two"; for(i=0;i<n;i++) { sum += A[i][n-(i+1)]; } cout<<sum<<" "; }
Question 30:
An array P[20][50] is stored in the memory along the column with each of the element occupying 4 bytes. Find out the memory location for the element P[15][10], if P[0][0] is stored at 5200. Delhi 2011
Аnswer:
B = 5200, M = 4 bytes, R = 20, C = 50. I = 15, J = 10, Lr = 0, Lc = 0 For column-wise allocation. Address of P[I][J] = B + W[(I-Lr)+R(J-Lc)] P[15][10] = 5200 + 4[(15-0)+20(10-0)] = 5200 + 4[15 + 20 x 10] = 5200+4(15+200) = 5200+4(215) = 5200+860 = 6060
Question 31:
An array G[50][20] is stored in the memory along the row with each of the element occupying 8 bytes. Find out the memory location for the element G[10][15], if G[0][0] is stored at 4200. All India 2011
Аnswer:
B = 4200, W = 8 bytes, R = 50, C = 20, I = 10, J = 15, Lr = 0, Lc =0 For row-wise allocation, Address of G[I][J] =B + W[C(I-Lr)+(J-Lc)] G[10][15] = 4200+8[20(10-0)+(15-0)] = 4200+8[20(10)+15] = 4200+8(215) = 4200+1720 = 5920
Question 32:
Write a function int SKIPSUM(int A[ ][3], int N, int M) in C++ to find and return the sum of elements from all alternate elements of a two-dimensional array starting from A[0][0]. Delhi 2010
Аnswer:
int SKIPSUM(int A[][3],int N.int M) int s=0,C=1; for(int i=0;i<N;i++) for(int j=0;j<M;j++) { if(C%2 != 0) s = s+A[i][j]; C++; } return s; }
Question 33:
Write a function int ALTERSUM(int B[ ][5], int N, int M) in C++ to find and return the sum of elements from all alternate elements of a two-dimensional array starting from B[0][0]. All India 2010
Аnswer:
int ALTERSUM(int B[][5],int N,int M) { int s=0,C=1; for(int i=0;i<N;i++) for(int j=0;j<M;j++) { if(C%2 != 0) s = s+B[i][j]; C++; } return s; }
Question 34:
An array P[50] [60] is stored in the memory along the column with each of the element occupying 2 bytes. Find out the memory location for the element P[10][20], if the base address of the array is 6800. Delhi 2010
Аnswer:
B = 6800, M = 2 bytes, R = 50, C = 60, I = 10, J = 20, Lr = 0, Lc =0 For column-wise allocation, Address of P[I][J] = B + W[I-Lr)+R(J-Lc)] P[10][20] = 6800 + 2[(10-0)+50(20-0)] = 6800+2(10+20*50) = 6800+2(10+1000) = 6800+2020 = 8820
Question 35:
An array T[90][100] is stored in the memory along the column with each of the element occupying 4 bytes. Find out the memory location for the element T[10][40], if the base address of the array is 7200. All India 2010
Аnswer:
B = 7200, W = 4 bytes, R = 90, C = 100, I = 10, J = 40, Lr = 0, Lc = 0 For column-wise allocation, Address of T[I][J] = B + W[(I-Ir)+R(J-Lc)] T[10][40] = 7200 + 4L(10-0)+90(40-0)] = 7200+4(10+40*90) = 7200+4(10+3600) = 7200+14440 = 21640
Question 36:
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.
Аnswer:
void SWAPCOL(int A[][5], int M, int N) int i,temp; for(i=0;i<M;i++) { temp = A[i][O]; A[i][0] = A[i][N-1]; A[i][N-l] = temp; } }
Question 37:
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.
Аnswer:
void SWAPARR(int a[][5],int r,int c) int i,t; for(i=0;i<c;i++) { t=a[0][i]; a[0][i]=a[r-l][i]; a[r-1][i]=t; } }
Question 38:
An array S [40] [30] is stored in the memory along the column with each of the elements occupying 4 bytes. Find out the base address and address of element S[20][15], if an element S[15][10] is stored at the memory location 7200. Delhi 2009
Аnswer:
R=40 C=30 W=4 bytes For colum-wise allocation, Address of S[I][J] = B + N(I-Ir)+R(J-Lc)] Address of S[15][10] = B+((10-0)*40+(15-0))*4 7200 = B+(400+15)*4 7200 = B+(415*4) 7200 = B+1660 B = 7200-1660 = 5540 Now, address of S[20][15] = B+W [(J-0)*R+(I-0)]*4 = 5540+[(15-0)*40+(20-0)]*4 = 5540+(15*40+20)*4 = 5540+(600+20)*4 = 5540+620*4 = 5540+2480 = 8020
Question 39:
An array T[50] [20] is stored in the memory along the column with each of the elements occupying 4 bytes. Find out the base address and address of element T[30][15], if an element T[25][10] is stored at the memory location 9800. All India 2009
Аnswer:
R = 50, C = 20, W = 4 bytes For column-wise allocation, Address of T[I][J] = B + W[(I-Lr)+R(J-Lc)] T[25][10] = B + 4[(25-0)+50(10-0)] T[25][10]= B+4[25+50*10] 9800 = B+2100 B = 9800-2100 B = 7700 Hence, base address = 7700 Now, for address of element T[30][15] T[30][15] = 7700+4[30+50x15] = 7700+4*780 = 7700+3120 = 10820