cbsecsnip-logo                         cbse-syllabus
cbse 100%
An online library for CBSE Computer Science and Informatics Practices students

CLASS XII COMPUTER SCIENCE ARRAY BASED IMPORTANT QUESTIONS

Page No. -> 1 | 2 | 3 | 4
6 Write a function in C++ to print the product of each row of a two dimensional array passed as the arguments of the function Example: if the two dimensional array contains Q6

Then the output should appear as:
Product of Row 1 = 8000
Product of Row 2 = 6000
Product of Row 3 =3600
Product of Row 4 = 2400
Ans.
#include<conio.h>
#include<iostream.h>
void rowProduct(int  arr[4][3],int r,int c)
{
  long arr2[4];
  for(int i=0;i<r;i++)
  {
    arr2[i]=1;
    for(int j=0;j<c;j++)
    {
      arr2[i] *= arr[i][j];
    }
    cout<<"Product of Row  "<<i+1<<"= "<<arr2[i]<<endl;
  }
}
void main()
  {
     int arr[4][3]={{20,40,10},{40,50,30},{60,30,20},{40,20,30}};
     clrscr();
     rowProduct(arr,4,3);
     getch();
}
7 Write function in C++ which accepts an integer array and size as arguments and replaces elements having odd values with thrice its value and elements having even values with twice its value.
Example : if an array of five elements initially contains elements as
3 4 5 16 9


The the function should rearrange the content of the array as
9 8 75 32 27
Ans.
#include<conio.h>
#include<iostream.h>
void manipulate(int a[],int size)
{
for(int i=0;i<size;i++)
{
   if (a[i]%2==1)
     a[i]=a[i]*3;    
   else      
     a[i]=a[i]*2;    
   cout<<a[i]<<',';}
}
void main()
{
   int a[5]={2,3,4,5,6};
   clrscr();
   manipulate(a,5);
   getch();
}
8 An array Array[20][15] is stored in the memory along the column with each element occupying 8 bytes. Find out the base address of the element Array[2][3] if the element Array[4][5] is stored at the address 1000.
Ans.
Given Data: Aray[20][15]  W=8  B=?  R=20  C=15  Lr=0  Lc=0
  Address of Array [2][3] =?
Address of Array[4][5] =1000. Address of an element(I,J) in column major=B+W((I-Lr)+R(J-Lc)) Therefore 1000=B+8*((4-0)+20(5-0)) 1000=B+8*(4+20*5) 1000 =B+8*104 1000=B+832 B=1000-832 B=168 Therefore Address of Array[2][3]=168+8*((2-0)+20(3-0)) =168+8*(2+20*3) =168+8*62 =168+496 =664
9 Write a function in C++ which accepts a 2D array of integers and its size as arguments and displays the elements which lie on diagonals. [Assuming the 2D Array to be a square matrix with odd dimension i.e., 3x3, 5x5, 7x7 etc…]
Example: if the array content is
5   4   3
6   7   8
1   2   9
Output through the function should be :
Diagonal One : 5   7   9
Diagonal Two : 3   7   1
Ans.
#include<conio.h>
#include<iostream.h>
void diag(int a[3][3],int size)
{
cout<<"First Diagonal:";
for (int i=0;i<size;i++) for(int j=0;j<size;j++) if(i==j) cout<<a[i][j]<<" "; cout<<"\n Second Diagonal:"; for(i=0;i<size;i++) for(j=0;j<size;j++) if((i+j)==(size-1)) cout<<a[i][j]<<" "; } void main() { int a[3][3]={{5,4,3},{6,7,8},{1,2,9}}; clrscr(); diag(a,3); getch(); }
10 Write a function in C++ which accepts an integer array and its size as arguments and replaces elements having even values with its half and elements having odd values with twice its value .
Example : If an array of five elements initially contains the elements as
3 4 5 16 9


then the function should rearrange content of the array as
6 2 10 8 18
Ans.
#include<conio.h>
#include<iostream.h>
void accept(int a[ ],int size)
{ for (int i=0;i<size;i++) { if(a[i]%2==0)
a[i]=a[i]/2;
else
a[i]=a[i]*2;
cout<<a[i]<<',';
} } void main()
{
int a[5]={3,4,5,16,9};
clrscr();
accept(a,5);
getch(); }

Receive all updates via Facebook. Just Click the Like Button Below...

Powered By EXEIdeas

Copyright © 2012, concatenate | Disclaimer | Site Map | |Terms of Service | Privacy Policy