NCERT Solutions for Class 12 Computer Science (C++) – Arrays
NCERT Solutions for Class 12 Computer Science (C++) having 14th Chapter whose Chapter wise Solution given below.
void main(){ int X [4] = {100, 75, 10, 125};int Go = random(2) + 2;cout<<X [i]<<"$$";for (int i = Go; i<4; i ++)}
Short Answer Type Questions-II[3 marks each]
Answer:
void grace_score(int score[].int size){for(int i=0;i<size;i++){score [i]=score[i]+5;if (score [i]<40)cout< }}
Question 2.
Question 3.
#include<iostream.h>void switchover (int A[],int N, int split){for(int K=0; K<N; K++)if(K<split)void display(int A [],int N)A [K] + = K;else{A[K] *=K; }cout<<A[K]<<endl;for (in K=0; K<N; K++)(K%2==0)? Cout<<A[K]<<"%" :}switchover(H, 6, 3);void main(){ int H [ ] = {30, 40, 50, 20, 10, 5};display(H, 6);}
Answer:
A[i] [j] = B+W x [No. of rows x (I-Lr) + (J-Lc)]A[20] [25] = B + 2x [30x (20-0) + (25-0)]A[7] [10] = 48750+ 2x[30x(7-0)+(10-0)]50000 = B+2x[30x(20-0)+(25-0)]B = 48750= 49190
Answer:
void insertsort (int a[],int n){int p,ptr;//Assuming a[0]=int_min i.e. smallest integerfor(p = 1; p<=n;p++){ temp=a[p];Forwardptr=p-1; while (temp<a[ptr] )a [ptr+1]=a[ptr] ; //Move Element{ ptr--;}}a[ptr+1]=temp; //Insert Element in Proper Place
Answer:
void NewMAT(int A[] [] , int r,int c){for (int i = 0; i<r; i+ + )A[i][j]/=6for (j=0; j=60)&&(A[1] [j)<=600))
OR
A[i] [j] =A[i] [j] /6;}
Question 8.
| A[0] | A[l] | A[2] | A[3] | A[4] | A[5] | A[6] | A[7] | A[8] | A[9] |
| 55 | 43 | 20 | 16 | 39 | 90 | 83 | 40 | 48 | 25 |
After executing the function, the array content should be changed as follows:
| A[0] | A[l] | A[2] | A[3] | A[4] | A[5] | A[6] | A[7] | A[8] | A[9] |
| 5 | 0 | 5 | 0 | 0 | 5 | 0 | 5 | 0 | 5 |
Answer:
void Alter (int A [] , int N){for (int i=0; i<N; i++)elseif (A[i] %5==0)A [i] = 5;}A [i] = 0;
change all the multiples of 10 in the array to 10 and rest of the element as 1.
For example, if an arrary of 10 integers is as follows :
|
P[0] |
P[l] |
P[2] |
P[3] |
P[4] |
P[5] |
P[6] |
P[7] |
P[8] |
P[9] |
|
100 |
43 |
20 |
56 |
32 |
91 |
80 |
40 |
45 |
21 |
After executing the
function, the array content should be changed as follows :
|
P[0] |
P[l] |
P[2] |
P[3] |
P[4] |
P[5] |
P[6] |
P[7] |
P[8] |
P[9] |
|
10 |
1 |
10 |
1 |
1 |
1 |
10 |
10 |
1 |
1 |
Answer:
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;
}
the odd values and 10 in all the even values of the array S.
|
50 |
11 |
19 |
24 |
28 |
The modified content
will be :
|
60 |
16 |
24 |
34 |
38 |
Answer:
void oddEven (int s []
, int N)
{
for (int i=0; i<N; i++)
{
if(s[i]%2!=0)
s[i]=s[i]+5;
else
s[i]=s [i]+10;
}
}
which takes integer arrays, each containing 6 elements as parameters. The
function should exchange all odd places (1st, 3rd and 5th) of the two arrays,
for example
|
15 |
10 |
12 |
21 |
52 |
76 |
And if the array B
contains
|
23 |
41 |
67 |
83 |
13 |
53 |
Then the function
should make the contents of the array A as
|
15 |
41 |
12 |
83 |
52 |
53 |
And the contents of
array B as
|
23 |
10 |
67 |
21 |
13 |
76 |
Answer:
void Transfer (int A
[6] , int B[6] )
{
int temp;
for (int i=;i<6; i+ = 2)
{
temp=A [i]
A [i] =B [i]
B [i] +temp;
}
}
and display all the numbers, which are divisible by 10. For example if the
content of array is :
|
12 |
20 |
13 |
|
2 |
10 |
30 |
Answer:
void DisTen (int A[]
[4] , int N, int M)
{
int i, j;
for (i=0; i<M; i++)
{
for (j = 0 ; j <N; j++)
{
if (A[i][j]%10==0)
cout << A [i] [ j] ;
}
}
}
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.
|
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:
void ChangeOver (int
P[] , int N)
{
int i, j, temp;
for (i=0; i<N; i = i+1)
{
j=j+1;
temp = P[i];
P[i] = P [j ] ;
P [j] = temp;
}
}
re-positions 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.
|
0 |
1 |
2 |
3 |
4 |
|
22 |
25 |
70 |
32 |
12 |
The Chenged Content
will be:
|
0 |
1 |
2 |
3 |
4 |
|
25 |
70 |
32 |
12 |
22 |
Answer:
void Convert (int T[],
int Num)
{
int i, j, temp;
for (i=Num; i > 10; i-=l)
{
j = i – 1;
temp = P [ i ] ;
P [i] = P[jl ;
P[j] = temp;
}
}
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 |
Answer:
void DispNTen (int L[]
[4], int R,
int C)
{
int i, j ;
for (i = 0 ; i<C; i + +)
{for (j = 0; j<R; j+ + )
{
if (A[i] [j]%10= =0)
cout <<A[i] [j]<<” “;
}
}
}
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 :
Answer:
void Transfer (int
A[], int B[], int size)
{
for (int i = 0; i<size; i++)
{
if(A[i]<0) B [i] =A [i] ; if (A [i] > 0)
B[i+1]=A[i];
else
B[i/2]=A[i];
}
}
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.
19.
descending order of score using selection sort.
{
int Score;
char Teamname[20];
} ;
Answer:
SORTSCORE (IPL a[] , int n)
20.
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[ ].
10,20,50. Then Z[ ] should contain 30,10,60,20,90,50.
Answer:
void TWOTOONE (int x[] , int n)
{
int z[n] ;
for (int I =0;i<n;i++)
{
if(i%2 == 0)
z [i] = x [i] ;
else
z [i] = y [i] ;
}
}
21.
all the odd values and 2 in all the even values of the array T.
|
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:
void EvenOdd (intT [] , int(C)
{
for (int i=0; i<C; i++)
{
if (T[i] %2==0)
T [i] + = 2;
else
T[i] + = 1;
}
}
22.
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.
Answer:
void SWAP 2CHANGE (int P[], int N)
{
for (int i=0; i<N; 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
}
}
}
23.
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.
Answer:
void SWAP2BEST (int ARR [] , int Size)
{
for (int n=0; n<size; n++)
{
if(ARR[n]%10 = =0)
{
int temp = 0;
temp = ARR [n];
ARR[n] = ARR[n+1];
ARR [n+1] = temp;
n++;
}
}
}
24.
should modify each element of the array Amt having N elements, as per the
following rules :
|
Existing |
Amt |
|
If |
Add |
|
If |
Add |
|
If> |
Add |
Answer:
void AddTax (Float Amt[] , int N)
{
for (int i=0; i<N; i++)
if(Amt[i]<50,000) Amt[i]+=0-25*Amt[i]; else
if (Amt[i]>=50000&&Amt[i] <100000)
Amt[i]+=0.2*Amt[i]; else
if(Amt[i]>=1,00000)
Amt[i]+=0.15*Amt[i];
}
TOPIC-2
Two-dimensional Arrays
Short Answer Type
Questions-I[2 marks each]
Question
1.
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]).
Answer:
void
Alternative (int A[] [3], intN, int M)
{
int T=0 ; I<N; 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<N*M ; I+=2)
{
Cout<<*p<<” ” ;
{ I+=2 ;
}
}
2.
present on both the diagonal of a two dimensional array passed as the argument
to the function.
Answer:
void
Diagonal (int A[] [20] , int N)
{
int K, Sdiag=0;
for(int K=0;K<N;K++) if ( A[K] [K] > 0)
Sdiag+=A [K] [K] ;
for(int K= 0;K<N;K++) if ( A[N-K-1][K] >
0 )
Sdiag+=A[N-K-1] [K] ;
cout<<“Sum of all the non-negative
elements on both the diagonals= “<<Sdiag;
}
3.
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:
voidswap_row
(intARR [] [3] , int R,int() )
{
for(int i=0 J=0: J<C;J++)
{
int temp=ARR[i][j];
ARR[i][j]=ARR[R-1][j];
ARR[R-1][j]=temp;
}
}
4.
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:
void
SumLast 3 (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]-3)% 10==0))
Sum=Sum+A[i][j];
}
cout<<sum;
}
5.
find and display the
|
22 |
16 |
12 |
|
19 |
5 |
2 |
The
output should be 36
Answer:
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]-2)%10 = = 0))
Sum=Sum + A[i] [j] ;
}
cout<<sum;
}
6.
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
Answer:
void
diag (int x[] [] , int m, int n)
{
for (int i=m; i>0 ; i— —)
{
for(int j =0; j<n ; j++
{
if(i==j)
cout<<” “<<n[i] [j] ;
}
}
}
7.
and returns the sum of all numbers present in the first row of the array, for
example, if the array contains
|
1 |
13 |
10 |
9 |
|
29 |
17 |
2 |
21 |
|
14 |
3 |
12 |
31 |
|
15 |
16 |
25 |
52 |
Then
the function should return 33.
Answer:
int
SumSingle (int A[4] [4])
{
int Sum=0;
for(int i=0 ; i for (int j=0 ; j Sum=Sum+ A[i] [j]
return Sum;
}
8.
alternate elements from two-dimensional array H (starting from H [0] [0]).
Answer:
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<<H[i] [j]<< ” ” ;
}
}
Short Answer Type
Questions-II[3 marks each]
1.
requiring 4 bytes of storage. If the base address of array P is 1400, find out
the location of P[8][5].
2.
location of A[2][5] if each element occupies 4 bytes and array is stored
columnwise.
3.
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.
4.
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.
5.
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.
6.
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.
7.
requiring 8 bytes of storage. If the base address of array T is 14000, find out
the location of T[10][7].
8.
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.
9.
content of a two dimensional array, with each column content in reverse order.
array is as follows :
|
15 |
12 |
56 |
45 |
51 |
|
13 |
91 |
92 |
87 |
63 |
|
11 |
23 |
61 |
46 |
81 |
function should display output as:
Answer:
void
REVCOL (int P[ ] [5], int N, int M)
{
for (int I = N – 1; I>=0; I- -)
{
for (int J = 0; J<M; J++)
Cout<<P[l] [J];
cout<<end1;
}
}
OR
void REVCOL (int P [] [5], int N, int M)
{
for (int I = 0; I<N/2; I++)
{
for (int J = 0; J<M; J++)
{
int T = P [I] [J] ;
P [1] [J] = P [N – I – 1] [J] ;
P[N-I-1] [J] = T;
}
}
for (I = 0; I<N; I++)
{
for (int J=0; J<M; J++)
cout<<P [I] [J] ;
cout<<end1;
}
}
10.
dimensional array, with each row content in reverse order.
|
15 |
12 |
56 |
45 |
51 |
|
13 |
91 |
92 |
87 |
63 |
|
11 |
23 |
61 |
46 |
81 |
function should display output as follows:
Answer:
void
REVROW (int P[] [5] , int N, int M)
{
for (int I =0;I<N;I++) {for (int J=M-1;
J>=0; J- -)
cout<<P[I] [J];
cout<<end1;
}
}
OR
void REVROW (int P [] [5] , int N, int M)
{
for (int I=0; I<N; I++)
for (int J=0; J<M/2; J++)
{
int T = P [I] [J] ;
P [I] [j] = P[I] [M – J – 1] ;
P [I] [M – J – 1] = T;
}
}
for (I = 0; I<N; I++)
{
for (int J = 0; J<M; J++)
Cout<<P[I] [J] ;
cout<<end1;
}
}
11.
row with each of its element occupying 4 bytes, find the address of the element
S[5][15], if the element S[8][10] is stored at the memory location 62,000.
Answer:
(S [I] [J] )
12.
display the elements of first, third and fifth column from a two dimenstional
array P having R number of rows and C number of columns.
|
515 |
102 |
113 |
801 |
145 |
|
303 |
141 |
129 |
202 |
121 |
|
285 |
189 |
100 |
760 |
112 |
function should display the following as output:
Answer:
void
SHOWMID (intP[] [5],intR,int C)
{
for(int J=0; J<C; J++)
Cout<<P[R/2][J]<<” “;
cout<<end1;
for (int I=0;I<R;I++)
cout<<P[I] [C/2]<<” ” ;
}
Long Answer Type
Questions[4 marks each]
1.
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.
Answer:
[20] [50]
2.
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.
|
115 |
112 |
116 |
101 |
125 |
|
103 |
101 |
121 |
102 |
101 |
|
185 |
109 |
109 |
160 |
172 |
function should display the following as output:
Answer:
void
SHOWMID (int P[] [5] , int R, int C)
{
for(int J=0;J<C;J++)
cout<<P[R/2] [J] <<” ” ;
cout<<end1;
for(int I=0;I<R;I++)
cout<<P[I][C/2]<<” “;
}
3.
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.
Answer:
[50]
