Professional Documents
Culture Documents
Q1. Write a PROGRAM in C++ which accepts a 2D array of integers and its size as arguments and
displays elements which are clubbed between two even numbers vertically. If 2D array is
Output is 25 45 327
Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
for(int j=0;j<c;j++)
if(A[i-1][j]%2==0&&A[i+1][j]%2==0)
cout<<A[i][j]<<" ";
void main()
{clrscr();
int x[10][10],r,c,i=0;
cin>>r;
cin>>c;
cout<<"Enter Array";
for(i=0;i<r;i++)
for(int j=0;j<c;j++)
cin>>x[i][j];
q2(x,r,c);
getch();}
Output:
Q2: Write a 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
123456
123450
123400
123000
120000
100000
000000
Code:
#include<iostream.h>
#include<conio.h>
#include<process.h>
{ int b[10][10],i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(i+j<=n-1)
b[i][j]=a[j];
else
b[i][j]=0;
for(i=0;i<n;i++)
{ cout<<endl;
for(j=0;j<n;j++)
cout<<b[i][j]<<" ";
void main()
{clrscr();
int x[10],n,i=0;
cout<<"Enter Array";
for(i=0;i<n;i++)
cin>>x[i];
r_upper_half(x,n);
getch();
Output:
Q3: Write a function in C++ to print the product of each column of a two-dimensional integer array
passed as the argument of the function. Example: if the two-dimensional arrays contains
Code:
#include<iostream.h>
#include<conio.h>
#include<process.h>
{ int i,j,prod[3];
for(i=0;i<r;i++)
{prod[i]=1;
for(j=0;j<c;j++)
{ prod[i]=prod[i]*A[i][j];
void main()
{clrscr();
int x[10][10],r,c,i=0;
cin>>r;
cin>>c;
cout<<"Enter Array";
for(i=0;i<r;i++)
for(int j=0;j<c;j++)
cin>>x[i][j];
productrow(x,r,c);
getch();
Output:
Q4: Write a function int bsearch() which takes as an argument an integer array and a number if the
number is found else returns false if not found after performing binary search on Array.
Code:
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
int bnsearch()
{int count,i,arr[30],num,first,last,middle;
cin>>count;
for(i=0;i<count;i++)
cin>>arr[i]; }
cin>>num;
first=0;
last=count-1;
middle=(first+last)/2;
while(first<=last)
{if (arr[middle]<num)
{first=middle+1;}
else if(arr[middle]==num)
{return 1;}
else{ last=middle-1;}
middle=(first+last)/2;}
if(first>last)
{return 0;}
void main()
{ clrscr();
if (bnsearch()==1)
cout<<"true";
else
cout<<"false";
getch();
Output:
Q5: Write a menu driven program which calls three functions 1) Bubble() 2) Insertion 3) Selection().
Each of the function takes as input an array of integers and sorts it with the help of Bubble sort,
Insertion sort and Selection sort respectively.
Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{ clrscr();
int Arr[100],n,choice;
cin>>n;
do
cout<<"\n\nMENU";
cout<<"\n6. Exit";
cin>>choice;
switch(choice)
{
case 1: accept(Arr,n);
break;
case 2: display(Arr,n);
break;
case 3: isort(Arr,n);
break;
case 4: ssort(Arr,n);
break;
case 5: bsort(Arr,n);
break;
case 6: break;
default:cout<<"\nInvalid choice";
}while(choice!=6);
getch();}
for(int I=0;I<s;I++)
cin>>Arr[I];
for(int I=0;I<s;I++)
cout<<Arr[I]<<" ";
}
int I,J,Temp;
for(I=1;I<s;I++)
Temp=Arr[I];
J=I-1;
Arr[J+1]=Arr[J];
J--;
Arr[J+1]=Temp;
int I,J,Temp,Small;
for(I=0;I<s-1;I++)
Small=I;
if(Arr[J]<Arr[Small])
Small=J;
if(Small!=I)
Temp=Arr[I]; //Swapping
Arr[I]=Arr[Small];
Arr[Small]=Temp;
int I,J,Temp;
for(I=0;I<s-1;I++)
for(J=0;J<(s-1-I);J++)
if(Arr[J]>Arr[J+1])
Temp=Arr[J]; //swapping
Arr[J]=Arr[J+1];
Arr[J+1]=Temp;
Output:
Q6: Let us assume Data[20][15] is a two-dimensional array, which is stored in the memory along the
row with each of its elements occupying 2 bytes. Find the address of the element Data[10][5], if the
element Data[15][10] is stored at the memory location 15000.
Sol:
15000=B+2x(15x15+10)
15000=B+2(235)
B=15000-470
B=14530
L=14530+2(15x10+5)
L=14840
Q7: 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].
Sol:
50000=B+2x[30x(20-0)+(25-0)]
B=48750
=49190
Q8: 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].
Sol: