<div class=”entry-content”><div id=”toc_container” class=”no_bullets contracted” style=”width: auto; display: table;”><p class=”toc_title”>Contents <span class=”toc_toggle”>[<a href=”#”>show</a>]</span></p><ul class=”toc_list” style=”display: none;”><li><a href=”#NCERT_Solutions_for_Class_12_Computer_Science_C_8211_Arrays”><span class=”toc_number toc_depth_1″>1</span> NCERT Solutions for Class 12 Computer Science (C++) – Arrays</a><ul><li><a href=”#TOPIC-1″><span class=”toc_number toc_depth_2″>1.1</span> TOPIC-1</a></li><li><a href=”#Data_Structures_and_One-dimensional_Arrays”><span class=”toc_number toc_depth_2″>1.2</span> Data Structures and One-dimensional Arrays</a></li><li><a href=”#Short_Answer_Type_Questions-I2_Marks_Each”><span class=”toc_number toc_depth_2″>1.3</span> Short Answer Type Questions-I[2 Marks Each]</a></li><li><a href=”#Short_Answer_Type_Questions-II3_marks_each”><span class=”toc_number toc_depth_2″>1.4</span> Short Answer Type Questions-II[3 marks each]</a></li><li><a href=”#TOPIC-2″><span class=”toc_number toc_depth_2″>1.5</span> TOPIC-2</a></li><li><a href=”#Two-dimensional_Arrays”><span class=”toc_number toc_depth_2″>1.6</span> Two-dimensional Arrays</a></li><li><a href=”#Short_Answer_Type_Questions-I2_marks_each”><span class=”toc_number toc_depth_2″>1.7</span> Short Answer Type Questions-I[2 marks each]</a></li><li><a href=”#Short_Answer_Type_Questions-II3_marks_each-2″><span class=”toc_number toc_depth_2″>1.8</span> Short Answer Type Questions-II[3 marks each]</a></li><li><a href=”#Long_Answer_Type_Questions4_marks_each”><span class=”toc_number toc_depth_2″>1.9</span> Long Answer Type Questions[4 marks each]</a></li></ul></li></ul></div>
<h2><span id=”NCERT_Solutions_for_Class_12_Computer_Science_C_8211_Arrays”>NCERT Solutions for Class 12 Computer Science (C++) – Arrays</span></h2>
<h3 style=”text-align: center;”><span id=”TOPIC-1″><u>TOPIC-1</u></span></h3>
<h3 style=”text-align: center;”><span id=”Data_Structures_and_One-dimensional_Arrays”>Data Structures and One-dimensional Arrays</span></h3>
<h3 style=”text-align: center;”><span id=”Short_Answer_Type_Questions-I2_Marks_Each”>Short Answer Type Questions-I<span class=”MSGENFONTSTYLENAMETEMPLATEROLELEVELMSGENFONTSTYLENAMEBYROLEHEADING4MSGENFONTSTYLEMODIFERSIZE11″><span style=”font-size: 11.0pt;”>[2 Marks Each]</span></span></span></h3>
<p> </p><div class=”code-block code-block-1″ style=”margin: 8px 0; clear: both;”>
<script async=”” src=”//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js” type=”text/javascript”></script>
<ins class=”adsbygoogle adsbygoogle-ablated-ad-slot adsbygoogle-noablate” style=”display: block;” data-ad-client=”ca-pub-7398766921532682″ data-ad-slot=”8094000682″ data-ad-format=”auto” data-adsbygoogle-status=”done” google_element_uid=”1″ data-ad-status=”filled”><div id=”aswift_1_host” tabindex=”0″ title=”Advertisement” aria-label=”Advertisement” style=”border: none; height: 280px; width: 750px; margin: 0px; padding: 0px; position: relative; visibility: visible; background-color: transparent; display: inline-block;”><iframe id=”aswift_1″ name=”aswift_1″ style=”left:0;position:absolute;top:0;border:0;width:750px;height:280px;” sandbox=”allow-forms allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-top-navigation-by-user-activation” width=”750″ height=”280″ frameborder=”0″ marginwidth=”0″ marginheight=”0″ vspace=”0″ hspace=”0″ allowtransparency=”true” scrolling=”no” src=”https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-7398766921532682&output=html&h=280&slotname=8094000682&adk=2916952469&adf=1164200656&pi=t.ma~as.8094000682&w=750&lmt=1670848520&format=750×280&url=https%3A%2F%2Fwww.cbsetuts.com%2Fncert-solutions-class-12-computer-science-c-arrays%2F&wgl=1&adsid=ChEIgPvEnQYQ7oKUkIGo7bvHARI9AMmKl4m4hEr3BUQB6I0NE8jFBnJQaFUsAiNfL0bbYDtbkUfxolZ4Gbh-iiWepQd0pNmSmRQvZ41DG163eQ&uach=WyJXaW5kb3dzIiwiMTAuMC4wIiwieDg2IiwiIiwiMTA4LjAuNTM1OS4xMjUiLFtdLGZhbHNlLG51bGwsIjY0IixbWyJOb3Q_QV9CcmFuZCIsIjguMC4wLjAiXSxbIkNocm9taXVtIiwiMTA4LjAuNTM1OS4xMjUiXSxbIkdvb2dsZSBDaHJvbWUiLCIxMDguMC41MzU5LjEyNSJdXSxmYWxzZV0.&dt=1672584909930&bpp=2&bdt=2611&idt=-M&shv=r20221207&mjsv=m202212050101&ptt=9&saldr=aa&abxe=1&cookie=ID%3D70877dca39d7cee1-22c4db3411d900f8%3AT%3D1672348851%3ART%3D1672348851%3AS%3DALNI_MYJDyG_bu32YHEUMIGJIo2bVt3_WQ&gpic=UID%3D00000b9a5c88ec58%3AT%3D1672348851%3ART%3D1672575613%3AS%3DALNI_Mb87E1-6Z_pG79EP2w2fRKpjdy0KA&prev_fmts=0x0&nras=1&correlator=1195691797536&frm=20&pv=1&ga_vid=540223204.1672348852&ga_sid=1672584909&ga_hid=1623091329&ga_fc=1&u_tz=330&u_his=12&u_h=768&u_w=1366&u_ah=728&u_aw=1366&u_cd=24&u_sd=1&dmc=4&adx=105&ady=668&biw=1349&bih=657&scr_x=0&scr_y=100&eid=44759875%2C44759926%2C44759837%2C31071168%2C44780792&oid=2&pvsid=584854560617544&tmod=1209859931&uas=0&nvt=1&ref=https%3A%2F%2Fwww.learncbse.in%2F&eae=0&fc=1920&brdim=0%2C0%2C0%2C0%2C1366%2C0%2C1366%2C728%2C1366%2C657&vis=1&rsz=%7C%7CeE%7C&abl=CS&pfx=0&fu=0&bc=31&ifi=2&uci=a!2&fsb=1&xpc=mnN3fYKXPI&p=https%3A//www.cbsetuts.com&dtd=28″ data-google-container-id=”a!2″ data-google-query-id=”CJO2mavQpvwCFbRBDwId4gEFkQ” data-load-complete=”true”></iframe></div></ins>
<script type=”text/javascript”>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<p>Question 1.<br>
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’.</p>
<pre>void main()
{ int X [4] = {100, 75, 10, 125};
int Go = random(2) + 2;
for (int i = Go; i<4; i ++)
cout<<X [i]<<”$$”;
}</pre>
<p>(i) 100<span class=”MathJax_Preview” style=””></span><div class=”MathJax_Display” style=”text-align: center;”><span class=”MathJax” id=”MathJax-Element-1-Frame” tabindex=”0″ style=”text-align: center;”><nobr><span class=”math” id=”MathJax-Span-1″ style=”width: 4.255em; display: inline-block;”><span style=”display: inline-block; position: relative; width: 3.486em; height: 0px; font-size: 122%;”><span style=”position: absolute; clip: rect(1.335em, 1003.43em, 2.666em, -999.997em); top: -2.252em; left: 0em;”><span class=”mrow” id=”MathJax-Span-2″><span class=”mn” id=”MathJax-Span-3″ style=”font-family: MathJax_Main;”>75</span><span class=”mo” id=”MathJax-Span-4″ style=”font-family: MathJax_Main;”>(</span><span class=”mi” id=”MathJax-Span-5″ style=”font-family: MathJax_Math-italic;”>i</span><span class=”mi” id=”MathJax-Span-6″ style=”font-family: MathJax_Math-italic;”>i</span><span class=”mo” id=”MathJax-Span-7″ style=”font-family: MathJax_Main;”>)</span><span class=”mn” id=”MathJax-Span-8″ style=”font-family: MathJax_Main;”>75</span></span><span style=”display: inline-block; width: 0px; height: 2.257em;”></span></span></span><span style=”display: inline-block; overflow: hidden; vertical-align: -0.372em; border-left: 0px solid; width: 0px; height: 1.378em;”></span></span></nobr></span></div><script type=”math/tex; mode=display” id=”MathJax-Element-1″>75
(ii) 75</script>10<span class=”MathJax_Preview” style=””></span><div class=”MathJax_Display” style=”text-align: center;”><span class=”MathJax” id=”MathJax-Element-2-Frame” tabindex=”0″ style=”text-align: center;”><nobr><span class=”math” id=”MathJax-Span-9″ style=”width: 1.847em; display: inline-block;”><span style=”display: inline-block; position: relative; width: 1.488em; height: 0px; font-size: 122%;”><span style=”position: absolute; clip: rect(1.437em, 1001.44em, 2.41em, -999.997em); top: -2.252em; left: 0em;”><span class=”mrow” id=”MathJax-Span-10″><span class=”mn” id=”MathJax-Span-11″ style=”font-family: MathJax_Main;”>125</span></span><span style=”display: inline-block; width: 0px; height: 2.257em;”></span></span></span><span style=”display: inline-block; overflow: hidden; vertical-align: -0.059em; border-left: 0px solid; width: 0px; height: 0.941em;”></span></span></nobr></span></div><script type=”math/tex; mode=display” id=”MathJax-Element-2″>125</script><br>
(iii) 75<span class=”MathJax_Preview” style=””></span><div class=”MathJax_Display” style=”text-align: center;”><span class=”MathJax” id=”MathJax-Element-3-Frame” tabindex=”0″ style=”text-align: center;”><nobr><span class=”math” id=”MathJax-Span-12″ style=”width: 1.283em; display: inline-block;”><span style=”display: inline-block; position: relative; width: 1.027em; height: 0px; font-size: 122%;”><span style=”position: absolute; clip: rect(1.437em, 1000.98em, 2.41em, -999.997em); top: -2.252em; left: 0em;”><span class=”mrow” id=”MathJax-Span-13″><span class=”mn” id=”MathJax-Span-14″ style=”font-family: MathJax_Main;”>10</span></span><span style=”display: inline-block; width: 0px; height: 2.257em;”></span></span></span><span style=”display: inline-block; overflow: hidden; vertical-align: -0.059em; border-left: 0px solid; width: 0px; height: 0.941em;”></span></span></nobr></span></div><script type=”math/tex; mode=display” id=”MathJax-Element-3″>10</script><br>
(iv) 10$$125$</p>
<p>Answer:<br>
(iv) is correct option<br>
Max. Value = 3<br>
Min. Value = 2</p>
<h3 style=”text-align: center;”><span id=”Short_Answer_Type_Questions-II3_marks_each”>Short Answer Type Questions-II[3 marks each]</span></h3>
<p>Question 1.<br>
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.<br>
Example: if an array of seven integers is as follows:<br>
45,35,85,80,33,27,90<br>
After executing the function, the array content should be changed as follows :<br>
45,40,85,80,38,32,90</p>
<p>Answer:</p>
<pre>void grace_score(int score[].int size)
{
for(int i=0;i<size;i++)
{
if (score [i]<40)
score [i]=score[i]+5;
cout< }
}</pre>
<p>Question 2.</p>
<p>Find the output of the following C+ + program:<br>
#include<iostream.h> 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”<<str; repch(str); cout<<“Changed String”<<str; }</p>
<p>Answer:<br>
Original String SUCCESS<br>
Changed String S@C!ES!</p>
<p>Question 3.<br>
Find the output of the following:</p>
<pre> #include<iostream.h>
void switchover (int A[],int N, int split)
{
for(int K=0; K<N; K++)
if(K<split)
A [K] + = K;
else
A[K] *=K; }
void display(int A [],int N)
{
for (in K=0; K<N; K++)
(K%2==0)? Cout<<A[K]<<”%” :
cout<<A[K]<<endl;
}
void main()
{ int H [ ] = {30, 40, 50, 20, 10, 5};
switchover(H, 6, 3);
display(H, 6);
}</pre>
<p>Answer:<br>
30%41<br>
52% 60<br>
40% 25</p>
<p>Question 4.<br>
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].</p>
<p>Answer:</p>
<pre>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</pre>
<p>Question 5.<br>
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].</p>
<p>Answer:<br>
Total number of rows = 30<br>
Total size = 2 bytes<br>
Base Address = 26500<br>
LOC(P[I][J]) = Base Address+((I-LBR)+(J- LBC)*R)W<br>
Assuming Lower Bound of Row(LBR)=0<br>
Lower Bound of Column(LBC)=0<br>
Total number of Rows(R)=30<br>
Size of each element(W)=2<br>
LOC(P[20][10]) = 26500 + ((20-0)+(10-0)*30)*2<br>
LOC(P[20][10]) = 26500 + 640<br>
LOC(P[20][10]) = 27140</p><div class=”google-auto-placed ap_container” style=”width: 100%; height: auto; clear: both; text-align: center;”><ins data-ad-format=”auto” class=”adsbygoogle adsbygoogle-noablate” data-ad-client=”ca-pub-7398766921532682″ data-adsbygoogle-status=”done” style=”display: block; margin: auto; background-color: transparent; height: 280px;”><div id=”aswift_4_host” style=”border: none; height: 280px; width: 750px; margin: 0px; padding: 0px; position: relative; visibility: visible; background-color: transparent; display: inline-block;”></div></ins></div>
<p>Question 6.<br>
Write a function to sort any array of n elements using insertion sort. Array should be passed as argument to the function.</p>
<p>Answer:</p>
<pre>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] )
{
a [ptr+1]=a[ptr] ; //Move Element
Forward
ptr–;
}
a[ptr+1]=temp; //Insert Element in Proper Place
}</pre>
<p>Question 7.<br>
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.</p>
<p>Answer:</p>
<pre>void NewMAT(int A[] [] , int r,int c)
{
for (int i = 0; i<r; i+ + )
for (j=0; j=60)&&(A[1] [j)<=600))
A[i][j]/=6</pre>
<p>OR</p>
<pre>A[i] [j] =A[i] [j] /6;
}</pre>
<p>Question 8.<br>
Write the definition of a function Alter (intA[], int N) in C++, which should change all the multiples of 5 in the array to 5 and rest of the elements as 0. For example, if an array of 10 integers is as follows :</p>
<table border=”2″>
<tbody>
<tr>
<td style=”text-align: center;” width=”33″>A[0]</td>
<td style=”text-align: center;” width=”34″>A[l]</td>
<td style=”text-align: center;” width=”34″>A[2]</td>
<td style=”text-align: center;” width=”33″>A[3]</td>
<td style=”text-align: center;” width=”34″>A[4]</td>
<td style=”text-align: center;” width=”34″>A[5]</td>
<td style=”text-align: center;” width=”34″>A[6]</td>
<td style=”text-align: center;” width=”33″>A[7]</td>
<td style=”text-align: center;” width=”33″>A[8]</td>
<td style=”text-align: center;” width=”35″>A[9]</td>
</tr>
<tr>
<td style=”text-align: center;” width=”33″>55</td>
<td style=”text-align: center;” width=”34″>43</td>
<td style=”text-align: center;” width=”34″>20</td>
<td style=”text-align: center;” width=”33″>16</td>
<td style=”text-align: center;” width=”34″>39</td>
<td style=”text-align: center;” width=”34″>90</td>
<td style=”text-align: center;” width=”34″>83</td>
<td style=”text-align: center;” width=”33″>40</td>
<td style=”text-align: center;” width=”33″>48</td>
<td style=”text-align: center;” width=”35″>25</td>
</tr>
</tbody>
</table>
<p>After executing the function, the array content should be changed as follows:</p>
<table border=”2″>
<tbody>
<tr>
<td style=”text-align: center;” width=”33″>A[0]</td>
<td style=”text-align: center;” width=”34″>A[l]</td>
<td style=”text-align: center;” width=”34″>A[2]</td>
<td style=”text-align: center;” width=”33″>A[3]</td>
<td style=”text-align: center;” width=”34″>A[4]</td>
<td style=”text-align: center;” width=”34″>A[5]</td>
<td style=”text-align: center;” width=”33″>A[6]</td>
<td style=”text-align: center;” width=”34″>A[7]</td>
<td style=”text-align: center;” width=”33″>A[8]</td>
<td style=”text-align: center;” width=”35″>A[9]</td>
</tr>
<tr>
<td style=”text-align: center;” width=”33″>5</td>
<td style=”text-align: center;” width=”34″>0</td>
<td style=”text-align: center;” width=”34″>5</td>
<td style=”text-align: center;” width=”33″>0</td>
<td style=”text-align: center;” width=”34″>0</td>
<td style=”text-align: center;” width=”34″>5</td>
<td style=”text-align: center;” width=”33″>0</td>
<td style=”text-align: center;” width=”34″>5</td>
<td style=”text-align: center;” width=”33″>0</td>
<td style=”text-align: center;” width=”35″>5</td>
</tr>
</tbody>
</table>
<p>Answer:</p>
<pre>void Alter (int A [] , int N)
{
for (int i=0; i<N; i++)
if (A[i] %5==0)
A [i] = 5;
else
A [i] = 0;
}</pre>
<p>OR<br>
Any other correct equivalent function definition</p>
<p>Question 9.<br>
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.</p><div class=”google-auto-placed ap_container” style=”width: 100%; height: auto; clear: both; text-align: center;”><ins data-ad-format=”auto” class=”adsbygoogle adsbygoogle-noablate” data-ad-client=”ca-pub-7398766921532682″ data-adsbygoogle-status=”done” style=”display: block; margin: auto; background-color: transparent; height: 280px;”><div id=”aswift_5_host” style=”border: none; height: 280px; width: 750px; margin: 0px; padding: 0px; position: relative; visibility: visible; background-color: transparent; display: inline-block;”></div></ins></div>
<p>Answer:<br>
Loc (P [I] [J] along the row =BaseAddress+W [(I-LBR)*C+ (J-LBC)]<br>
(where C is the number of columns, LBR = LBC =0)<br>
LOC (P [5] [5]) = BaseAddress + W* [I*C + J]<br>
15000 = BaseAddress + 4 * [5 * 50 + 5]<br>
= BaseAddress + 4 * [250 + 5]<br>
= BaseAddress + 4*255 = BaseAddress + 1020<br>
BaseAddress = 15000 -1020 = 13980<br>
LOC (P [10] [30]) = 13980 + 4* [10 * 50 + 30]<br>
= 13980 + 4 * 530<br>
= 13980 + 2120<br>
= 16100<br>
OR LOC (P [10] [30])<br>
= Loc (P[5] [5]) + W[ (I-LBR) * C+ (J-LBC)]<br>
= 15000 + 4 [(10 – 5) * 50 + (30 – 5)]<br>
= 15000 + 4 [5 * 50 + 25]<br>
= 15000 + 4 *275<br>
= 15000 + 1100<br>
= 16100<br>
OR<br>
(where C is the number of columns and LBR = LBC = 1)<br>
LOC (P [5] [5])<br>
15000 = BaseAddress + W [(I-1) *C + (J-1)]<br>
= BaseAddress + 4 [4*50 + 4]<br>
= BaseAddress + 4 [200 + 4]<br>
= BaseAddress + 4 * 204<br>
= BaseAddress + 816<br>
BaseAddress = 15000 – 816 = 14184<br>
LOC (P [10] [30])<br>
= 14184 + 4 [(10 -1) * 50 + (30 -1)]<br>
= 14184 + 4 [9*50 + 29]<br>
= 14184 + 4*479<br>
= 14184 + 1916<br>
= 16100</p><div class=”google-auto-placed ap_container” style=”width: 100%; height: auto; clear: both; text-align: center;”><ins data-ad-format=”auto” class=”adsbygoogle adsbygoogle-noablate” data-ad-client=”ca-pub-7398766921532682″ data-adsbygoogle-status=”done” style=”display: block; margin: auto; background-color: transparent; height: 280px;”><div id=”aswift_6_host” style=”border: none; height: 280px; width: 750px; margin: 0px; padding: 0px; position: relative; visibility: visible; background-color: transparent; display: inline-block;”></div></ins></div>
<p>Question 10.<br>
Write the definition of function Change (int P[], int N) in C+ +, which should 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>
<table style=”height: 61px;” border=”2″ width=”421″>
<tbody>
<tr>
<td style=”text-align: center;” width=”33″>P[0]</td>
<td style=”text-align: center;” width=”34″>P[l]</td>
<td style=”text-align: center;” width=”34″>P[2]</td>
<td style=”text-align: center;” width=”33″>P[3]</td>
<td style=”text-align: center;” width=”34″>P[4]</td>
<td style=”text-align: center;” width=”34″>P[5]</td>
<td style=”text-align: center;” width=”33″>P[6]</td>
<td style=”text-align: center;” width=”34″>P[7]</td>
<td style=”text-align: center;” width=”33″>P[8]</td>
<td style=”text-align: center;” width=”35″>P[9]</td>
</tr>
<tr>
<td style=”text-align: center;” width=”33″>100</td>
<td style=”text-align: center;” width=”34″>43</td>
<td style=”text-align: center;” width=”34″>20</td>
<td style=”text-align: center;” width=”33″>56</td>
<td style=”text-align: center;” width=”34″>32</td>
<td style=”text-align: center;” width=”34″>91</td>
<td style=”text-align: center;” width=”33″>80</td>
<td style=”text-align: center;” width=”34″>40</td>
<td style=”text-align: center;” width=”33″>45</td>
<td style=”text-align: center;” width=”35″>21</td>
</tr>
</tbody>
</table>
<p>After executing the function, the array content should be changed as follows :</p>
<table style=”height: 62px;” border=”2″ width=”447″>
<tbody>
<tr>
<td style=”text-align: center;” width=”33″>P[0]</td>
<td style=”text-align: center;” width=”34″>P[l]</td>
<td style=”text-align: center;” width=”33″>P[2]</td>
<td style=”text-align: center;” width=”33″>P[3]</td>
<td style=”text-align: center;” width=”34″>P[4]</td>
<td style=”text-align: center;” width=”34″>P[5]</td>
<td style=”text-align: center;” width=”33″>P[6]</td>
<td style=”text-align: center;” width=”34″>P[7]</td>
<td style=”text-align: center;” width=”33″>P[8]</td>
<td style=”text-align: center;” width=”35″>P[9]</td>
</tr>
<tr>
<td style=”text-align: center;” width=”33″>10</td>
<td style=”text-align: center;” width=”34″>1</td>
<td style=”text-align: center;” width=”33″>10</td>
<td style=”text-align: center;” width=”33″>1</td>
<td style=”text-align: center;” width=”34″>1</td>
<td style=”text-align: center;” width=”34″>1</td>
<td style=”text-align: center;” width=”33″>10</td>
<td style=”text-align: center;” width=”34″>10</td>
<td style=”text-align: center;” width=”33″>1</td>
<td style=”text-align: center;” width=”35″>1</td>
</tr>
</tbody>
</table>
<p>Answer:</p>
<pre>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;
}</pre>
<p>OR<br>
Any other correct equivalent function definition</p>
<p>Question 11.<br>
Write code for a function 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.<br>
Example : If the original content of the array S is</p>
<table border=”2″>
<tbody>
<tr>
<td style=”text-align: center;” width=”48″>50</td>
<td style=”text-align: center;” width=”48″>11</td>
<td style=”text-align: center;” width=”48″>19</td>
<td style=”text-align: center;” width=”48″>24</td>
<td style=”text-align: center;” width=”49″>28</td>
</tr>
</tbody>
</table>
<p>The modified content will be :</p>
<table border=”2″>
<tbody>
<tr>
<td style=”text-align: center;” width=”48″>60</td>
<td style=”text-align: center;” width=”48″>16</td>
<td style=”text-align: center;” width=”48″>24</td>
<td style=”text-align: center;” width=”48″>34</td>
<td style=”text-align: center;” width=”49″>38</td>
</tr>
</tbody>
</table>
<p>Answer:</p>
<pre>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;
}
}</pre>
<p>Question 12.<br>
Write the definition for a function void Transfer (int A[6], int B[6]) in C++, 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<br>
If the array A contains</p>
<table border=”2″>
<tbody>
<tr>
<td style=”text-align: center;” width=”39″>15</td>
<td style=”text-align: center;” width=”38″>10</td>
<td style=”text-align: center;” width=”38″>12</td>
<td style=”text-align: center;” width=”38″>21</td>
<td style=”text-align: center;” width=”38″>52</td>
<td style=”text-align: center;” width=”39″>76</td>
</tr>
</tbody>
</table>
<p>And if the array B contains</p>
<table border=”2″>
<tbody>
<tr>
<td style=”text-align: center;” width=”39″>23</td>
<td style=”text-align: center;” width=”38″>41</td>
<td style=”text-align: center;” width=”38″>67</td>
<td style=”text-align: center;” width=”38″>83</td>
<td style=”text-align: center;” width=”38″>13</td>
<td style=”text-align: center;” width=”39″>53</td>
</tr>
</tbody>
</table>
<p>Then the function should make the contents of the array A as</p>
<table border=”2″>
<tbody>
<tr>
<td style=”text-align: center;” width=”39″>15</td>
<td style=”text-align: center;” width=”38″>41</td>
<td style=”text-align: center;” width=”38″>12</td>
<td style=”text-align: center;” width=”38″>83</td>
<td style=”text-align: center;” width=”38″>52</td>
<td style=”text-align: center;” width=”39″>53</td>
</tr>
</tbody>
</table>
<p>And the contents of array B as</p>
<table border=”2″>
<tbody>
<tr>
<td style=”text-align: center;” width=”39″>23</td>
<td style=”text-align: center;” width=”38″>10</td>
<td style=”text-align: center;” width=”38″>67</td>
<td style=”text-align: center;” width=”38″>21</td>
<td style=”text-align: center;” width=”38″>13</td>
<td style=”text-align: center;” width=”39″>76</td>
</tr>
</tbody>
</table>
<p>Answer:</p>
<pre>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;
}
}</pre>
<p>Question 13.<br>
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 :</p>
<table border=”2″>
<tbody>
<tr>
<td style=”text-align: center;” width=”49″>12</td>
<td style=”text-align: center;” width=”48″>20</td>
<td style=”text-align: center;” width=”49″>13</td>
</tr>
<tr>
<td style=”text-align: center;” width=”49″>2</td>
<td style=”text-align: center;” width=”48″>10</td>
<td style=”text-align: center;” width=”49″>30</td>
</tr>
</tbody>
</table>
<p>The output should be<br>
20 10 30</p>
<p>Answer:</p>
<pre>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] ;
}
}
}</pre>
<p>Question 14.<br>
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.<br>
For example: If the content of the array is :</p>
<table style=”height: 63px;” border=”2″ width=”261″>
<tbody>
<tr>
<td style=”text-align: center;” width=”48″>0</td>
<td style=”text-align: center;” width=”54″>1</td>
<td style=”text-align: center;” width=”48″>2</td>
<td style=”text-align: center;” width=”42″>3</td>
<td style=”text-align: center;” width=”48″>4</td>
</tr>
<tr>
<td style=”text-align: center;” width=”48″>12</td>
<td style=”text-align: center;” width=”54″>15</td>
<td style=”text-align: center;” width=”48″>17</td>
<td style=”text-align: center;” width=”42″>13</td>
<td style=”text-align: center;” width=”48″>21</td>
</tr>
</tbody>
</table>
<p>The Chenged Content will be:</p>
<table border=”2″>
<tbody>
<tr>
<td style=”text-align: center;” width=”47″>0</td>
<td style=”text-align: center;” width=”37″>1</td>
<td style=”text-align: center;” width=”42″>2</td>
<td style=”text-align: center;” width=”42″>3</td>
<td style=”text-align: center;” width=”54″>4</td>
</tr>
<tr>
<td style=”text-align: center;” width=”47″>21</td>
<td style=”text-align: center;” width=”37″>12</td>
<td style=”text-align: center;” width=”42″>15</td>
<td style=”text-align: center;” width=”42″>17</td>
<td style=”text-align: center;” width=”54″>13</td>
</tr>
</tbody>
</table>
<p>Answer:</p>
<pre>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;
}
}</pre>
<p>Question 15.<br>
Write code for a function void Convert (int T[], int Num) in C++, which 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.<br>
For example : If the content of the array is :</p>
<table border=”2″>
<tbody>
<tr>
<td style=”text-align: center;” width=”48″>0</td>
<td style=”text-align: center;” width=”54″>1</td>
<td style=”text-align: center;” width=”48″>2</td>
<td style=”text-align: center;” width=”42″>3</td>
<td style=”text-align: center;” width=”48″>4</td>
</tr>
<tr>
<td style=”text-align: center;” width=”48″>22</td>
<td style=”text-align: center;” width=”54″>25</td>
<td style=”text-align: center;” width=”48″>70</td>
<td style=”text-align: center;” width=”42″>32</td>
<td style=”text-align: center;” width=”48″>12</td>
</tr>
</tbody>
</table>
<p>The Chenged Content will be:</p>
<table border=”2″>
<tbody>
<tr>
<td style=”text-align: center;” width=”47″>0</td>
<td style=”text-align: center;” width=”37″>1</td>
<td style=”text-align: center;” width=”42″>2</td>
<td style=”text-align: center;” width=”42″>3</td>
<td style=”text-align: center;” width=”54″>4</td>
</tr>
<tr>
<td style=”text-align: center;” width=”47″>25</td>
<td style=”text-align: center;” width=”37″>70</td>
<td style=”text-align: center;” width=”42″>32</td>
<td style=”text-align: center;” width=”42″>12</td>
<td style=”text-align: center;” width=”54″>22</td>
</tr>
</tbody>
</table>
<p>Answer:</p>
<pre>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;
}
}</pre>
<p>Question 16.<br>
Write a user-defined function DispNTen (int L[] [4], int R, int C) in C++ to find the display all the numbers, which are not divisible by 10. For example if the content of array is :</p>
<table border=”2″>
<tbody>
<tr>
<td style=”text-align: center;” width=”49″>20</td>
<td style=”text-align: center;” width=”48″>17</td>
<td style=”text-align: center;” width=”49″>30</td>
</tr>
<tr>
<td style=”text-align: center;” width=”49″>12</td>
<td style=”text-align: center;” width=”48″>19</td>
<td style=”text-align: center;” width=”49″>10</td>
</tr>
</tbody>
</table>
<p>The output should be<br>
17 12 19</p>
<p>Answer:</p>
<pre>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]<<” “;
}
}
}</pre>
<p>Question 17.<br>
Write a function Transfer (int A[], int B[], int Size) in C++ to copy the 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 :<br>
If the contents of array A are :<br>
7, -23,3,0, -8, -3,4,0<br>
The contents of array B should be<br>
-23, -8, -3,7,3,4,0</p>
<p>Answer:</p>
<pre>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];
}
}</pre>
<p>Question 18.<br>
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.</p>
<p>Answer:<br>
Loc (ARR[I] [J] along the row<br>
= BaseAddress + W [(I – LBR) *C + (J – LBC)]<br>
(where C is the number of columns, LBR = LBC = 0 LOC (ARR [10] [5])<br>
= BaseAddress + W [I *C + J]<br>
15000 = BaseAddress + 4 [10 * 20 + 5]<br>
= BaseAddress + 4 [200 + 5]<br>
= BaseAddress + 4 x 205<br>
= BaseAddress + 820<br>
BaseAddress = 15000 – 820<br>
= 14180<br>
LOC (ARR [30] [10]) = 14180 + 4 [30 * 20 + 10]<br>
= 14180 + 4 * 610<br>
= 14180 + 2440<br>
= 16620<br>
OR<br>
LOC (ARR [30] [10]) = LOC (ARR [10] [5] + W [(I – LBR) * C + (J – LBC)]<br>
= 15000 + 4 [(30-10) *20 + (10 – 5)]<br>
= 15000 + 4 [20*20 + 5]<br>
= 15000 + 4 *405<br>
= 15000 + 1620<br>
= 16620<br>
OR<br>
Where C is the number of columns and LBR=LBC=1<br>
LOC (ARR[10] [5])<br>
15000 = BaseAddress + W [(I -1) * C + (J-l)]<br>
= BaseAddress + 4 [9*20 + 4]<br>
= BaseAddress + 4[180 + 4]<br>
= BaseAddress + 4 * 184<br>
= BaseAddress + 736<br>
BaseAddress = 15000 – 736<br>
= 14264<br>
LOC (ARR [30] [10])<br>
= 14264 + 4 [(30 -1) * 20 + (10 -1)]<br>
= 14264 + 4[29 *20 + 9]<br>
= 14264 + 4 [580 + 9]<br>
= 14264 + 4*589<br>
= 14264 + 2356<br>
= 16620</p><div class=”google-auto-placed ap_container” style=”width: 100%; height: auto; clear: both; text-align: center;”><ins data-ad-format=”auto” class=”adsbygoogle adsbygoogle-noablate” data-ad-client=”ca-pub-7398766921532682″ data-adsbygoogle-status=”done” style=”display: block; margin: auto; background-color: transparent; height: 280px;”><div id=”aswift_7_host” style=”border: none; height: 280px; width: 750px; margin: 0px; padding: 0px; position: relative; visibility: visible; background-color: transparent; display: inline-block;”></div></ins></div>
<p>Question 19.<br>
Write a function SORTSCORE() in C++ to sort an array of structure IPL in descending order of score using selection sort.<br>
Note : Assume the following definition of structure IPL:<br>
struct IPL:</p>
<pre> {
int Score;
char Teamname[20];
} ;</pre>
<p>Answer:</p>
<p>void SORTSCORE (IPL a[] , int n)<br>
{ int largest;<br>
IPL temp;<br>
for ( int K = 0; K <n-1; K++)<br>
{ largest = K;<br>
for ( int j = K+1; j< n; j++) { if ( a[j].score > a[largest]. score)<br>
{ largest = j;<br>
}<br>
}<br>
temp = a[K];<br>
a[K] = a[largest];<br>
a [largest] = temp;<br>
}<br>
}</p>
<p>Question 20.<br>
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[ ].<br>
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.</p>
<p>Answer:</p>
<pre> 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] ;
}
}</pre>
<p>Question 21.<br>
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.<br>
Example : If the original content of the array T is</p>
<table border=”2″>
<tbody>
<tr>
<td style=”text-align: center;” width=”48″>T[0]</td>
<td style=”text-align: center;” width=”48″>T[l]</td>
<td style=”text-align: center;” width=”48″>T[2]</td>
<td style=”text-align: center;” width=”48″>T[3]</td>
<td style=”text-align: center;” width=”49″>T[4]</td>
</tr>
<tr>
<td style=”text-align: center;” width=”48″>35</td>
<td style=”text-align: center;” width=”48″>12</td>
<td style=”text-align: center;” width=”48″>16</td>
<td style=”text-align: center;” width=”48″>69</td>
<td style=”text-align: center;” width=”49″>26</td>
</tr>
</tbody>
</table>
<p>The modified content will be:</p>
<table border=”2″>
<tbody>
<tr>
<td style=”text-align: center;” width=”48″>T[0]</td>
<td style=”text-align: center;” width=”48″>T[l]</td>
<td style=”text-align: center;” width=”48″>T[2]</td>
<td style=”text-align: center;” width=”48″>T[3]</td>
<td style=”text-align: center;” width=”49″>T[4]</td>
</tr>
<tr>
<td style=”text-align: center;” width=”48″>36</td>
<td style=”text-align: center;” width=”48″>14</td>
<td style=”text-align: center;” width=”48″>18</td>
<td style=”text-align: center;” width=”48″>70</td>
<td style=”text-align: center;” width=”49″>28</td>
</tr>
</tbody>
</table>
<p>Answer:</p>
<pre> void EvenOdd (intT [] , int(C)
{
for (int i=0; i<C; i++)
{
if (T[i] %2==0)
T [i] + = 2;
else
T[i] + = 1;
}
}</pre>
<p>Question 22.<br>
Write a function SWAP2 CHANGE (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.<br>
For example:<br>
If the content of array P is<br>
91,50,54,22,30,54<br>
The content of array P should become<br>
91,54,50,22,54,30</p>
<p>Answer:</p>
<pre> 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
}
}
}</pre>
<p>Question 23.<br>
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.<br>
For example:<br>
If the content of array ARR is<br>
90,56,45,20,34,54<br>
The content of array ARR should become<br>
56,90,45,34,20,54</p>
<p>Answer:</p>
<pre> 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++;
}
}
}</pre>
<p>Question 24.<br>
Write the definition of a function AddTax(float Amt[], int N) in C + + , which should modify each element of the array Amt having N elements, as per the following rules :</p>
<table style=”height: 212px;” border=”2″ width=”367″>
<tbody>
<tr>
<td style=”text-align: center;” width=”130″>Existing Value of Amt</td>
<td style=”text-align: center;” width=”130″>Amt to be changed to</td>
</tr>
<tr>
<td style=”text-align: center;” width=”130″>If less than 50,000</td>
<td style=”text-align: center;” width=”130″>Add 25% in the existing value</td>
</tr>
<tr>
<td style=”text-align: center;” width=”130″>If > =50,000 and <1,00,000</td>
<td style=”text-align: center;” width=”130″>Add 20% in the existing value</td>
</tr>
<tr>
<td style=”text-align: center;” width=”130″>If> =1,00,000</td>
<td style=”text-align: center;” width=”130″>Add 15% in the existing value</td>
</tr>
</tbody>
</table>
<p>Answer:</p>
<pre> 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];
}
</pre>
<h3 style=”text-align: center;”><span id=”TOPIC-2″><u>TOPIC-2</u></span></h3>
<h3 style=”text-align: center;”><span id=”Two-dimensional_Arrays”>Two-dimensional Arrays</span></h3>
<h3 style=”text-align: center;”><span id=”Short_Answer_Type_Questions-I2_marks_each”>Short Answer Type Questions-I[2 marks each]</span></h3>
<p>Question 1.</p>
<p>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]).<br>
For example:<br>
If the array is containing :<br>
23 54 76<br>
37 19 28<br>
62 13 19<br>
The output will be<br>
23 76 19 62 19</p>
<p>Answer:</p>
<pre>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 ;
}
}</pre>
<p>OR<br>
Any other equivalent correct answer acceptable</p>
<p>Question 2.<br>
Write a function in C++ to print the sum of all the non-negative elements present on both the diagonal of a two dimensional array passed as the argument to the function.</p>
<p>Answer:</p>
<pre>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;
}</pre>
<p>Question 3.<br>
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:</p>
<table style=”height: 92px;” border=”2″ width=”232″>
<tbody>
<tr>
<td style=”text-align: center;” width=”78″>10</td>
<td style=”text-align: center;” width=”78″>20</td>
<td style=”text-align: center;” width=”72″>30</td>
</tr>
<tr>
<td style=”text-align: center;” width=”78″>40</td>
<td style=”text-align: center;” width=”78″>50</td>
<td style=”text-align: center;” width=”72″>60</td>
</tr>
<tr>
<td style=”text-align: center;” width=”78″>70</td>
<td style=”text-align: center;” width=”78″>80</td>
<td style=”text-align: center;” width=”72″>90</td>
</tr>
</tbody>
</table>
<p>Then after function call,the content of the array should be:</p>
<table border=”2″>
<tbody>
<tr>
<td style=”text-align: center;” width=”72″>70</td>
<td style=”text-align: center;” width=”66″>80</td>
<td style=”text-align: center;” width=”72″>90</td>
</tr>
<tr>
<td style=”text-align: center;” width=”72″>40</td>
<td style=”text-align: center;” width=”66″>50</td>
<td style=”text-align: center;” width=”72″>60</td>
</tr>
<tr>
<td style=”text-align: center;” width=”72″>10</td>
<td style=”text-align: center;” width=”66″>20</td>
<td style=”text-align: center;” width=”72″>30</td>
</tr>
</tbody>
</table>
<p>Answer:</p>
<pre>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;
}
}</pre>
<p>Question 4.<br>
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 :</p>
<table border=”2″>
<tbody>
<tr>
<td style=”text-align: center;” width=”49″>33</td>
<td style=”text-align: center;” width=”48″>13</td>
<td style=”text-align: center;” width=”49″>92</td>
</tr>
<tr>
<td style=”text-align: center;” width=”49″>99</td>
<td style=”text-align: center;” width=”48″>3</td>
<td style=”text-align: center;” width=”49″>12</td>
</tr>
</tbody>
</table>
<p>The output should be 49</p>
<p>Answer:</p>
<pre>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;
}</pre>
<p>Question 5.<br>
Write a user-defined function AddEnd2(int A[] [4], int N, int M) in C+ + to find and display the<br>
sum of all the values, which are ending with 2 (i.e., units place is 2).<br>
For example, if the content of arrray is :</p>
<table border=”2″>
<tbody>
<tr>
<td style=”text-align: center;” width=”48″>22</td>
<td style=”text-align: center;” width=”48″>16</td>
<td style=”text-align: center;” width=”49″>12</td>
</tr>
<tr>
<td style=”text-align: center;” width=”48″>19</td>
<td style=”text-align: center;” width=”48″>5</td>
<td style=”text-align: center;” width=”49″>2</td>
</tr>
</tbody>
</table>
<p>The output should be 36</p>
<p>Answer:</p>
<pre>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;
}</pre>
<p>Question 6.<br>
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<br>
i.e. 3.3 , 5.5 , 7.7, etc …]<br>
For example:<br>
If the 2D array is<br>
6 7 8<br>
1 3 6<br>
7 9 3<br>
The following should be displayed :<br>
8<br>
3<br>
7</p>
<p>Answer:</p>
<pre>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] ;
}
}
}</pre>
<p>Question 7.<br>
Write a user-defined function int Sum Single(int A[4][4])in C++, which finds and returns the sum of all numbers present in the first row of the array, for example, if the array contains</p>
<table border=”2″>
<tbody>
<tr>
<td style=”text-align: center;” width=”49″>1</td>
<td style=”text-align: center;” width=”48″>13</td>
<td style=”text-align: center;” width=”48″>10</td>
<td style=”text-align: center;” width=”49″>9</td>
</tr>
<tr>
<td style=”text-align: center;” width=”49″>29</td>
<td style=”text-align: center;” width=”48″>17</td>
<td style=”text-align: center;” width=”48″>2</td>
<td style=”text-align: center;” width=”49″>21</td>
</tr>
<tr>
<td style=”text-align: center;” width=”49″>14</td>
<td style=”text-align: center;” width=”48″>3</td>
<td style=”text-align: center;” width=”48″>12</td>
<td style=”text-align: center;” width=”49″>31</td>
</tr>
<tr>
<td style=”text-align: center;” width=”49″>15</td>
<td style=”text-align: center;” width=”48″>16</td>
<td style=”text-align: center;” width=”48″>25</td>
<td style=”text-align: center;” width=”49″>52</td>
</tr>
</tbody>
</table>
<p>Then the function should return 33.</p>
<p>Answer:</p>
<pre>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;
}</pre>
<p>Question 8.<br>
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]).<br>
For example:<br>
If the array is containing:<br>
12 45 67<br>
33 90 76<br>
21 43 59<br>
The output will be<br>
12 67 90 21 59</p>
<p>Answer:</p>
<pre>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]<< ” ” ;
}
}
</pre>
<h3 style=”text-align: center;”><span id=”Short_Answer_Type_Questions-II3_marks_each-2″>Short Answer Type Questions-II<span class=”MSGENFONTSTYLENAMETEMPLATEROLENUMBERMSGENFONTSTYLENAMEBYROLETEXT36Exact”>[3 marks each]</span></span></h3>
<p>Question 1.<br>
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 1400, find out the location of P[8][5].</p>
<p>Answer:<br>
Loc (P[8][5]) = B+W((I-Lr)+(J-Lc)M)<br>
= 1400+4((8-0)+(5-0)15)<br>
= 1732.</p><div class=”google-auto-placed ap_container” style=”width: 100%; height: auto; clear: both; text-align: center;”><ins data-ad-format=”auto” class=”adsbygoogle adsbygoogle-noablate” data-ad-client=”ca-pub-7398766921532682″ data-adsbygoogle-status=”done” style=”display: block; margin: auto; background-color: transparent; height: 280px;”><div id=”aswift_8_host” style=”border: none; height: 280px; width: 750px; margin: 0px; padding: 0px; position: relative; visibility: visible; background-color: transparent; display: inline-block;”></div></ins></div>
<p>Question 2.<br>
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.</p>
<p>Answer:<br>
w=4<br>
b=10000<br>
m=10<br>
n[i][i] = [b+ w(i-l1) + m(j-l2)<br>
n[2][5] = [10000+ 4(2-0) + 10(5-0)]<br>
n[2][5] = [10000 +8 +50 ]<br>
n[2][5] = 10058</p>
<p>Question 3.<br>
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.</p>
<p>Answer:<br>
Take T[2][2] as the base address [BA],<br>
sr=starting row, sc=starting column;<br>
Along the row<br>
A[i][j] = BA + size[nc*(i-sr) + (j-sc)]<br>
T[20][5] = 3000 + 4 [ 18 * (20-2) + (5-2)]<br>
= 3000 + 4[(18 * 18) + (3)]<br>
= 3000 + 4[324 + 3]<br>
= 3000 + 4(327)<br>
= 3000 + 1308<br>
= 4308<br>
nc = no. of columns sr=2 sc=2<br>
Total number of elements stored in T=37*18=666<br>
Total number of bytes allocated to T=666*4=2664</p>
<p>Question 4.<br>
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.</p>
<p>Answer:<br>
Array A[20] [30]<br>
Base Address = 32000<br>
Size of element(W)=4 bytes<br>
A[I][J]=A[15][10] hence I=15 and J=10<br>
Total Row (R)=20<br>
Total Column (C)=30<br>
A [I] IJ]=B + W[C(I-0)+(J-0)]<br>
A[15][10]=32000 +4[30(15-0)+(10-0)]<br>
= 32000+4[30(15)+10]<br>
= 32000+4[450+10]<br>
= 32000+4[460]<br>
= 32000+1840<br>
= 33840</p>
<p>Question 5.<br>
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.</p>
<p>Answer:<br>
Array A[25] [20]<br>
Base Address = 42000<br>
Size of element(W)= 2 bytes<br>
A[I][J]=A[10][15] hence I=10 and J=15<br>
Total Row (R) = 25<br>
Total Column (C)= 20<br>
A [I] [J] =B+ W[C(I-0)+(J-0)]<br>
A[10] [15] =42000 +2[20(10-0)+(15-0)]<br>
= 42000+2(20(10+15)]<br>
= 42000+2[200+15]<br>
= 42000+2(215]<br>
= 42000+430]<br>
= 42430</p>
<p>Question 6.<br>
An array S[10][15] is stored in the memory with each elements 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.</p>
<p>Answer:<br>
S[10][15] //Base address; 25000<br>
W=2 bytes<br>
S[5][10] =?<br>
m(row)=0 i=5<br>
n(column)=15 j=10<br>
Array is stored in the memory along the column:<br>
P[i][j] = Base address + u[i+mxj]<br>
s[5][10] = 25000+2[5+10×10]<br>
= 25000+2(5+100] [1]<br>
= 25000+2[105]<br>
= 25000+210<br>
= 25210</p>
<p>Question 7.<br>
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].</p>
<p>Answer:<br>
Base Address = 14000 = B<br>
T [I][J] = B + W [C[I-Ir] + (J-Jc)]<br>
= 14000 + 8 [10(10-0)+(7-0)]<br>
= 14000 + 8 x 107<br>
= 14000 + 856<br>
= 14856</p>
<p>Question 8.<br>
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.</p>
<p>Answer:<br>
nr=8 nc=6 size = 4 bytes<br>
A[2][4] = 936 A[5][1] = ?<br>
Formula for row-major<br>
A[i][j] = BA + size * ((i – sr) * nc + (j – sc))<br>
A[5][1] = A[2][4] + 4 * ((5 – 2) * 6 + (1 – 4))<br>
= 936 + 4* ((3) *6-3)<br>
= 936 + 4 * 15<br>
= 936 + 60<br>
A[5][l] = 996</p>
<p>Question 9.<br>
Write a function REVCOL (in P[] [5], int N, int int M) in C++ to display the content of a two dimensional array, with each column content in reverse order.<br>
Note : Array may contain any number of rows. For example, if the content of array is as follows :</p>
<table style=”height: 92px;” border=”2″ width=”241″>
<tbody>
<tr>
<td style=”text-align: center;” width=”30″>15</td>
<td style=”text-align: center;” width=”26″>12</td>
<td style=”text-align: center;” width=”26″>56</td>
<td style=”text-align: center;” width=”25″>45</td>
<td style=”text-align: center;” width=”25″>51</td>
</tr>
<tr>
<td style=”text-align: center;” width=”30″>13</td>
<td style=”text-align: center;” width=”26″>91</td>
<td style=”text-align: center;” width=”26″>92</td>
<td style=”text-align: center;” width=”25″>87</td>
<td style=”text-align: center;” width=”25″>63</td>
</tr>
<tr>
<td style=”text-align: center;” width=”30″>11</td>
<td style=”text-align: center;” width=”26″>23</td>
<td style=”text-align: center;” width=”26″>61</td>
<td style=”text-align: center;” width=”25″>46</td>
<td style=”text-align: center;” width=”25″>81</td>
</tr>
</tbody>
</table>
<p>The function should display output as:<br>
11 23 61 46 81<br>
13 91 92 87 63<br>
15 12 56 45 51</p>
<p>Answer:</p>
<pre>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;
}
}</pre>
<p>Question 10.<br>
Write a function REVROW (int N, int M) in C + + to display the content of a two dimensional array, with each row content in reverse order.<br>
For example, if the content of array is as follows:</p>
<table style=”height: 90px;” border=”2″ width=”254″>
<tbody>
<tr>
<td style=”text-align: center;” width=”30″>15</td>
<td style=”text-align: center;” width=”26″>12</td>
<td style=”text-align: center;” width=”26″>56</td>
<td style=”text-align: center;” width=”25″>45</td>
<td style=”text-align: center;” width=”25″>51</td>
</tr>
<tr>
<td style=”text-align: center;” width=”30″>13</td>
<td style=”text-align: center;” width=”26″>91</td>
<td style=”text-align: center;” width=”26″>92</td>
<td style=”text-align: center;” width=”25″>87</td>
<td style=”text-align: center;” width=”25″>63</td>
</tr>
<tr>
<td style=”text-align: center;” width=”30″>11</td>
<td style=”text-align: center;” width=”26″>23</td>
<td style=”text-align: center;” width=”26″>61</td>
<td style=”text-align: center;” width=”25″>46</td>
<td style=”text-align: center;” width=”25″>81</td>
</tr>
</tbody>
</table>
<p>The function should display output as follows:<br>
51 45 56 12 15<br>
63 87 92 91 13<br>
81 46 61 23 81</p>
<p>Answer:</p>
<pre>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;
}
}</pre>
<p>Question 11.<br>
S[50][20] 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 S[5][15], if the element S[8][10] is stored at the memory location 62,000.</p>
<p>Answer:</p>
<p>LOC (S [I] [J] )<br>
= Reference Address+W [ (I-Lr) *C+ (J-Lc) ]<br>
LOC (S [5] [15] )<br>
= LOC(5 [8] [10]+4[(15-10)*50+(5-8)]<br>
= 62000 + 4 [5*50-3]<br>
= 62000 + 988<br>
= 62988</p>
<p>Question 12.<br>
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 from a two dimenstional array P having R number of rows and C number of columns.<br>
For example, if the content of array is as follows :</p>
<table border=”2″>
<tbody>
<tr>
<td style=”text-align: center;” width=”52″>515</td>
<td style=”text-align: center;” width=”52″>102</td>
<td style=”text-align: center;” width=”52″>113</td>
<td style=”text-align: center;” width=”52″>801</td>
<td style=”text-align: center;” width=”52″>145</td>
</tr>
<tr>
<td style=”text-align: center;” width=”52″>303</td>
<td style=”text-align: center;” width=”52″>141</td>
<td style=”text-align: center;” width=”52″>129</td>
<td style=”text-align: center;” width=”52″>202</td>
<td style=”text-align: center;” width=”52″>121</td>
</tr>
<tr>
<td style=”text-align: center;” width=”52″>285</td>
<td style=”text-align: center;” width=”52″>189</td>
<td style=”text-align: center;” width=”52″>100</td>
<td style=”text-align: center;” width=”52″>760</td>
<td style=”text-align: center;” width=”52″>112</td>
</tr>
</tbody>
</table>
<p>The function should display the following as output:<br>
515 303 285<br>
113 129 100<br>
145 121 112</p>
<p>Answer:</p>
<pre>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]<<” ” ;
}</pre>
<h3 style=”text-align: center;”><span id=”Long_Answer_Type_Questions4_marks_each”>Long Answer Type Questions[4 marks each]</span></h3>
<p>Question 1.<br>
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.</p>
<p>Answer:</p>
<p>T [20] [50]<br>
Along the row<br>
A [i] [j] = BA + size [((i – sr)*nc + j- sc)]<br>
T [10] [8] = 52000<br>
T [15] [5] =52000 + 4* ((15 – 10)*50 + (5-8))<br>
= 52000 + 4* (250 + -3)<br>
= 52000 + 4 * 247<br>
= 52000 + 988<br>
= 52988<br>
Address = 52988</p>
<p>Question 2.<br>
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.<br>
For example, If the content of array is as follows:</p>
<table border=”2″>
<tbody>
<tr>
<td style=”text-align: center;” width=”49″>115</td>
<td style=”text-align: center;” width=”49″>112</td>
<td style=”text-align: center;” width=”49″>116</td>
<td style=”text-align: center;” width=”49″>101</td>
<td style=”text-align: center;” width=”49″>125</td>
</tr>
<tr>
<td style=”text-align: center;” width=”49″>103</td>
<td style=”text-align: center;” width=”49″>101</td>
<td style=”text-align: center;” width=”49″>121</td>
<td style=”text-align: center;” width=”49″>102</td>
<td style=”text-align: center;” width=”49″>101</td>
</tr>
<tr>
<td style=”text-align: center;” width=”49″>185</td>
<td style=”text-align: center;” width=”49″>109</td>
<td style=”text-align: center;” width=”49″>109</td>
<td style=”text-align: center;” width=”49″>160</td>
<td style=”text-align: center;” width=”49″>172</td>
</tr>
</tbody>
</table>
<p>The function should display the following as output:<br>
103 101 121 102 101<br>
116 121 109</p>
<p>Answer:</p>
<pre>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]<<” “;
}</pre>
<p>Question 3.<br>
R[20][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.</p>
<p>Answer:</p>
<p>R[20] [50]<br>
Along the row<br>
A[i][j] = BA + size [(i – Sr)nc + (j-Sc)]<br>
R[8][10] = 45000<br>
R[5][15] = 45000+8* ((5 – 8)* 50 + (15 -10))<br>
= 45000 + 8* (- 3*50 + 5)<br>
= 45000 + 8* (-145)<br>
= 45000-1160<br>
R[5][15] = 43840</p>
<p><a class=”button-red” title=”NCERT Solutions Home Page” href=”http://www.learncbse.in/ncert-solutions-2/”>NCERT Solutions</a><a class=”button-blue” title=”NCERT Solutions for Class 12 Computer Science (C++)” href=”http://www.learncbse.in/ncert-solutions-class-12-computer-science-c/”>Computer science</a><a class=”button-blue” title=”NCERT Solutions for Class 12 English” href=”http://www.learncbse.in/ncert-solutions-for-class-12-english/”>English</a><a class=”button-blue” title=”NCERT Solutions for Class 12 Hindi” href=”http://www.learncbse.in/ncert-solutions-class-12-hindi-core/”>Hindi</a><a class=”button-blue” title=”NCERT Solutions for Class 12 Humanities” href=”http://www.learncbse.in/cbse-class-12-humanities/”>Humanities</a><a class=”button-blue” title=”NCERT Solutions for Class 12 Commerce” href=”http://www.learncbse.in/cbse-class-12-commerce/”>Commerce</a><a class=”button-red” title=”CBSE Class 12 Science” href=”http://www.learncbse.in/cbse-class-12-science/”>Science</a></p>
</div>