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
11 An array Arr[15][20] is stored in the memory along the row with each element occupying 4 bytes. Find out the Base address of the location Arr[3][2], if the location Arr[5][2] is stored at the address 1500. 
Ans.
Given Data: Arr[15][20] W=4 B=? R=15 C=20 Lr =0 Lc =0
Address of Arr[3][2] = ?
Address of Arr[5][2] = 1500.
Address of an element(I,J)in row major=B+W(C(I-Lr)+(J-Lc))
Therefore, 1500 = B+4(20(5-0)+(2-0))
1500 = B+4(20*5+2)
1500 = B+4*102
1500 = B+408
B =1500-408
B=1092
Address of Arr[3][2] =1092+4(20*3+2)
=1092+4(62) =1092+248 =1340.
12 Write a function in C++ which accepts a 2D array of integers and its size as arguments and displays the elements of middle row and the elements of middle column. [Assuming the 2D Array to be a square matrix with odd dimension i.e., 3x3, 5x5, 7x7 etc…]
Example : If the array content is
3       4
7    6    9
2    1    8
Output through the function should be :
Middle Row : 7   6   9 
Middle Column : 5   6   1
Ans.
#include<conio.h>
#include<iostream.h>
void accept(int a[3][3],int size)
{
  cout<<"Middle Row:";
  for(int i=0;i<size;i++)
    for(int j=0;j<size;j++)
      if(i==size/2)
        cout<<a[i][j]<<'\t';
      cout<<"\n  Middle Column:";
      for(i=0;i<size;i++)
        for(j=0;j<size;j++)
          if(j==size/2)
        cout<<a[i][j]<<'\t';
}
void main()
{
  int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
  clrscr();
  accept(a,3);
  getch();
}
13

Write function in C++ which accepts an integer array and size as arguments and assign values into a 2D array of integers in the following format :
If the array is 1, 2, 3, 4, 5, 6
The resultant 2D array is given below
1      2      3      4      5      6
1      2      3      4      5      0
1      2      3      4      0      0
1      2      3      0      0      0
1      2      0      0      0      0
1      0      0      0      0      0
If the array is 1, 2, 3
The resultant 2D array is given :
1      2      3
1      2      0
1      0      0

Ans.
#include<conio.h>
#include<iostream.h>
void twoDArray(int aa[ ],int size)
{ int twodarr[6][6];
for (int i=0;i<size;i++)
{
for (int j=0;j<size;j++)
{
if((i+j)>=size)
twodarr [i][j]=0;
else
twodarr[i][j]=a[j];
cout<< twodarr[i][j]<<” “;
}
cout<<endl;
}
} void main()
{
int arr[6]={1,2,3,4,5,6};
clrscr();
twoDArray (arr,6);
getch();
}
14 Write a function in C++ which accepts an integer array and its size as arguments and exchanges the values of first half side elements with the second half side elements of the array.
Example :
If an array of 8 elements initial content as
2 4 1 6 7 9 23 10



The function should rearrange array as
7 9 23 10 2 4 1 6
Ans
#include<iostream.h>
void  modify(int a[],int size)
{ int i,j,temp; for(i=0,j=size/2; j<size; i++,j++) { temp=a[i]; a[i]=a[j]; a[j]=temp; } for(i=0;i<size;i++) { cout<<a[i]<<" "; } } void main() { int a[8]={2,4,1,6,7,9,23,10}; clrscr(); modify(a,8); getch(); }
15 Write function in C++ which accepts an integer array and size as arguments and assign values into a 2D array of integers in the following format :
If the array is 1, 2, 3, 4, 5, 6
The resultant 2D array is given below :
1 0 0 0 0 0
1 2 0 0 0 0
1 2 3 0 0 0
1 2 3 4 0 0
1 2 3 4 5 0
1 2 3 4 5 6









If the array is 1, 2, 3
The resultant 2D array is given :
1 0 0
1 2 0
1 2 3
Ans.
#include<conio.h>
#include<iostream.h>
void input (int a[],int size)

int b[6][6];
for (int i=0;i<size;i++)
{
for (int j=0;j<size;j++)
{
if((i<j))
b[i][j]=0;
else
b[i][j]=a[j];
cout<<b[i][j]<<" ";
}
cout<<endl;
}

void main()
{
int a[6]={1,2,3,4,5,6};
clrscr();
input(a,6);
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