You are on page 1of 14

Computer Science Assignment

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>

void q2(int A[10][10],int r,int c)

{ for (int i=1;i<r-1;i++)

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;

cout<<"Enter number of rows";

cin>>r;

cout<<"Enter number of volumns";

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>

void r_upper_half(int a[10],int n)

{ 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 number of terms.";


cin>>n;

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

Then the output should appear as:

Product of Column 1=24

Product of Column 2=30

Product of Column 3=240

Code:

#include<iostream.h>

#include<conio.h>

#include<process.h>

void productrow(int A[10][10],int r,int c)

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

cout<<"\nProduct of Row "<<i+1<<" = "<<prod[i];

void main()

{clrscr();

int x[10][10],r,c,i=0;

cout<<"Enter number of rows";

cin>>r;

cout<<"Enter number of volumns";

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;

cout<<"how many elements would you like to enter?";

cin>>count;

for(i=0;i<count;i++)

{ cout<<"Enter number"<<(i+1)<<' ';

cin>>arr[i]; }

cout<<"Enter the no that you want to search";

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 accept(int Arr[], int s);

void display(int Arr[], int s);

void isort(int Arr[], int s);

void ssort(int Arr[], int s);

void bsort(int Arr[],int s);

void main()

{ clrscr();

int Arr[100],n,choice;

cout<<"Enter Size of Array ";

cin>>n;

do

cout<<"\n\nMENU";

cout<<"\n1. Accept elements of array";

cout<<"\n2. Display elements of array";

cout<<"\n3. Sort the array using insertion sort method";

cout<<"\n4. Sort the array using selection sort method";

cout<<"\n5. Sort the array using bubble sort method";

cout<<"\n6. Exit";

cout<<"\n\nEnter your choice 1-6 :";

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

void accept(int Arr[], int s)

for(int I=0;I<s;I++)

cout<<"Enter element "<<I+1<<":";

cin>>Arr[I];

void display(int Arr[], int s)

cout<<"The elements of the array are:\n";

for(int I=0;I<s;I++)

cout<<Arr[I]<<" ";
}

void isort(int Arr[], int s)

int I,J,Temp;

for(I=1;I<s;I++)

Temp=Arr[I];

J=I-1;

while((Temp<Arr[J]) && (J>=0))

Arr[J+1]=Arr[J];

J--;

Arr[J+1]=Temp;

void ssort(int Arr[], int s)

int I,J,Temp,Small;

for(I=0;I<s-1;I++)

Small=I;

for(J=I+1;J<s;J++) //finding the smallest element

if(Arr[J]<Arr[Small])

Small=J;

if(Small!=I)

Temp=Arr[I]; //Swapping

Arr[I]=Arr[Small];
Arr[Small]=Temp;

void bsort(int Arr[],int s)

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:

A[i][j]=B+W x [No of rows x (I-Lr) + (J-Lc)]

A[20][25] = B + [30 x(7-0)+(10-0)]

50000=B+2x[30x(20-0)+(25-0)]

B=48750

A[7][10] = 48750+ 2x[30x(7-0)+(10-0)]

=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:

Total number of rows = 30

Total size = 2 bytes

Base Address = 26500

LOC(PIIJ[J]) = Base Address+((I-LBR)+(J- LBC)*R)W

Assuming Lower Bound of Row(LBR)=0

Lower Bound of Column(LBC)=0

Total number of Rows(R)=30

Size of each element(W)=2

LOC(P[20]I1 0]) = 26500 + ((20-0)+(10-0)*30)*2

LOC(P[20][1 0]) = 26500 + 640

LOC(P[20][1 0]) = 27140

You might also like