XII CS : Array Questions with Solution in C++ (3+2 MARKS)


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   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  
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]

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          
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     15 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();
}


Popular posts from this blog

XII- CS-- SOLVED SQL queries (SQL PRACTICAL ASSIGNMENS)

SQL--HOME ASSIGNMENTS(XII Class)

Python-MySQL Connectivity