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
1 Write a function in C++, which accepts an integer array and its size as parameters and rearranges the array in descending order.
Example: If an array of nine elements initially contains the elements as
4 2 5 1 6 7 8 12 10


Then the function should rearrange the array as
12 10 8 7 6 5 4 2 1
Ans.
#include<iostream.h>
#include<conio.h>
void select_sort(int a[ ], int n)
{
    int i, j, p,large;
    for(i=0;i<n-1;i++)
    {
        large=a[i];
        p=i;
        for(j=i+1; j<n; j++)
        {
            if(a[j]>large)
            {
                large=a[j];
                p=j;
            }
        }
        a[p]=a[i];
        a[i]=large;
    }
}
void main()
{
    int a[9]={4,2,5,1,6,7,8,12,10};
    int n=0;
    int i=0;
    clrscr();
    n=sizeof a/sizeof(int); //total size of array/size of array data type
    cout<<n;
    cout<<"\n Original array is  :\n";
    for(i=0;i<n;i++)
    cout<<a[i]<<", ";
    select_sort(a,n);
    cout<<"\nThe sorted array is:\n";
    for(i=0; i<n; i++)
    cout<<a[i]<<", ";
    getch();
}
2 An array Arr[40][10] is store in the memory along the column with each element occupying 4 bytes. Find out the base address of the location Arr[3][6] if the location Arr[30][10] is stored at the address 9000.
Ans.
Given Data: Aray[40][10]  W=4  B=?  R=40  C=10  Lr = 0  Lc  = 0
    Address of Array [3][6] =?
    Address of Array[30][10] =9000.
    Address of an element (I,J) in  column major =B + W ( (I-Lr) + R(J-Lc ) )
Therefore 9000=B+4*((30-0)+40(10-0))
    9000=B+4*(30+40*10)
    9000 =B+4*430
    9000=B+1720
    B =9000-1720
    B =7280
Therefore Address of Array[3][6]=7280+4*((3-0)+40(6-0))
    =7280+4*(3+40*6)
    =7280+4*243
    =7280+972
    =8252
3 Write a function in C++ to print the product of each column of a two dimensional array passed as the arguments of the function.
Example: If the two dimensional array contains Q3
Then the output should appear as:
Product of Column 1 = 24
Product of Column 2 = 30
Product of Column 3 =240
Ans.
#include<conio.h>
#include<iostream.h>
void colProduct(int  arr[4][3],int r,int c)
{
  int arr2[3];
  for(int i=0;i<c;i++)        //loop for column
  {
    arr2[i]=1;
    for(int j=0;j<r;j++)      //loop for rows
      arr2[i] *= arr[j][i];
cout<<"Product of Column  "<<i+1<<"= "<<arr2[i]<<endl;
  }
}
void main()
{
    int  arr[4][3]={{1,2,4},{3,5,6},{4,3,2},{2,1,5}};
    clrscr();
    colProduct(arr,4,3);
    getch();
}
4 Write a function in C++, which accepts an integer array and its size as arguments and swap the elements of every even location with its following odd location.
Example: If an array of nine elements initially contains the elements as
2 4 1 6 5 7 9 23 10


then the function should rearrange the array as
4 2 6 1 7 5 23 9 10
Ans.
#include<conio.h>
#include<iostream.h>
void swapElement(int arr[ ], int no)
{
    int temp;
    for(int i=0;i<no-1;i+=2)
    {
        temp=arr[i];
        arr[i]=arr[i+1];
        arr[i+1]=temp;
    }
    cout<<"\nThe elements after completed the alterations";
    for(i=0;i<no;i++)
        cout<<arr[i]<<" ";
}
void main()
{
    int arr[9]={2,4,1,6,5,7,9,23,10};
    clrscr();
    swapElement(arr,9);
    getch();
}
5 An array Arr[50][10] is store in the memory along the row with each element occupying 2 bytes. Find out the Base address of the location Arr[20][50], if the location Arr[10][25] is stored at the address 10000.
Ans.
This question was misprinted and  was controversial.
Which one will be correct –
(i) in  place of Arr[50][10] it shall be Arr[50][100]
                  Or 
(ii) in place of location  Arr[20][50], if the location Arr[10][25]
it shall be be location Arr[20][5], if  the location Arr[10][2]

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