Sie sind auf Seite 1von 30

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

100000 #include<iostream.h>
#include<conio.h>
000000
#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 Arrayproduct(int a[10][10], int r, int c)
{
int product=1, x,y;
for(x=0; x < c; x++)
{
for(product=1, y=0; y < r; y++)
{
product = product * a[y][x];
}
cout << "Product of column: " << x+1 << "= " << product<<endl;
}
}

void main()
{clrscr();
int x[10][10],r,c,i=0;
cout<<"Enter number of rows";
cin>>r;
cout<<"Enter number of columns";
cin>>c;
cout<<"Enter Array";
for(i=0;i<r;i++)
for(int j=0;j<c;j++)
cin>>x[i][j];
Arrayproduct(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
SQL
1. To select the itemname, whose warranty period is less than 3 years and
detefopurchase is in 2016.
2. To display all the items whose name starts with “c”.
3. To list the item in ascending order of the date of purchase WHERE quantity is more
than 3.
4. To increase the cost of all the items by 10 %
5. Display items bought between May 2014 to Dec 2016.
6. Display device wise total cost to the company.
7. Display device wise average cost for devices having total sum of quantity greater
than 5.
8. To display itemname, quantity along with supplier details.
9. Insert a record of your choice in table lab
10. Command to create table supplier
11. Display items whose warranty has expired
12. DISPLAY ITEM NAME, DATEOF PURCHASE, SUPPLIERNAME FOR ITES COSTING MORE
THAN 10000;
13. Display Supplier name wise total quantity
14. Change the length of Supplier Address as char(50).
15. Give the output of the following sql commands:
A. Select min(distinct quantity) from lab;
B. Select max(warranty) from lab where operational =1;
C. Select sum(costperitem) from lab;
D. Select count(*) from lab group by warranty;
Answers:
1.

2.

3.
4.

5.

6.

7.
8.

9.

10.

11.

12.
15
A

D
Classes And Objects
Q1. Define a class Bill in OOP with the following specification: -
Private members:
1. Bill_no - type long(bill number)
2. Bill_period - type integer(number of months)
3. No_of_calls - type integer(number of mobile calls)
4. Payment_mode - type string(“online” or “offline”)
5. Amount - type float(amount of bill)
8 PLOTTER 20000 3 12/5/2019 2 OUTPUT 20
Calculate_Bill() function to calculate the amount of bill given as per the
following conditions:
No_of_calls Calculation Rate/call
(in rupees)
<=500 1.0
501-1200.1
>1200 4.0
Also, the value of Amount should be reduced by 5% if Payment_mode is
“online”.
Public members:
1. A member function New_Bill() that will accept the values for Bill_no,
Bill_period, No_of_calls, Payment_mode from the user and invoke
Caluclate_Bill() to assign the value of Amount.
2. A member function Print_Bill() that will display all details of a Bill.
Implement the above class for n number of objects and display total number of Bis
paid “Online” and “Offline”

Sol:
#include<iostream.h>
#include<conio.h>
class bill
{
long bill_no;
int bill_period;
int no_of_calls;
int payment_mode;
float amount;
void calculate_bill(int noc, int pmode)
{
int amt;
if(noc<=500)
amt=noc;
else if ((noc>500) && (noc <=1200))
amt=noc *2;
else
amt=noc*4;
if(mode==1)
amount= (95/100)*amt;
else
amount=amt;
}

public:
int new_bill()
{
int val;
cout<<"Enter bill number";
cin>>bill_no;
cout<<"Enter number of months";
cin>>bill_period;
cout<<"Enter number of calls";
cin>>no_of_calls;
cout<<"Enter payment mode(offline=0/online=1)";
cin>>payment_mode;
if(payment_mode==0)
val=0;
else val=1;
calculate_bill(no_of_calls,payment_mode);
return val;
}
void print_bill()
{
cout<<"Bill number: "<<bill_no<<endl;
cout<<"Bill period: "<<bill_period<<endl;
cout<<"Number of calls: "<<no_of_calls<<endl;
cout<<"Payment Mode: "<<payment_mode<<endl;
cout<<"Bill Amount: "<<amount<<endl;
cout<<"______________________"<<endl;
cout<<endl;
}
};
void main()
{
clrscr();
int n,value,offcount=0,oncount=0;
bill b[50];
cout<<"Enter no of bills to be entered: ";
cin>>n;
for(int i=0; i<n; i++)
{value=b[i].new_bill();
if (value==0)
ofcount++;
else
oncount++;
}
cout<<"Total number of bills paid online= "<<oncount<<endl;
cout<<"Total number of bills paid offline= "<<offcount<<endl;
getch();
}
Q2:Define a class Candidate in C++ with the following specification:
Private Members:
A data members Rno(Registration Number) type long
A data member Cname of type string
A data members Agg_marks (Aggregate Marks) of type float
A data members Grade of type char
A member function setGrade () to find the grade as per the aggregate marks
obtained by the student. Equivalent aggregate marks range and the respective grade as
shown below.
Aggregate Marks Grade
>=80 A
Less than 80 and >=65 B
Less than 65 and >=50 C
Less than 50 D
Public members:
A function Getdata () to allow users to enter values for Rno. Cname, Agg_marks and call
function setGrade () to find the grade.
A function dispResult( ) to allow user to view the content of all the data members.
Implement the above class for n number of Objects and sort the Array on the basis of
descending order of marks using any sorting algorithm of your choice.

Sol:
#include<iostream.h>
#include<conio.h>
class candidate
{
long rno;
char cname[25];
char grade;
float agg_marks;
void setgrade(float amarks)
{ if(amarks>=80)
grade='A';
else if (9amarks>=65)&&(amarks<80))
grade='B';
else if((amarks>=50)&&(amarks<80))
grade='C';
else
grade='D' }
public:
int getdata(){
cout<<"Enter registration number: "<<endl;
cin>>rno;
cout<<"Enter name: "<<endl;
cin>>cname;
cout<<"Enter Aggregate Marks: "<<endl;
cin>>agg_marks;
setgrade(agg_marks);
return agg_marks;}
void dispresult()
{cout<<"Registration number: "<<rno<<endl;
cout<<"Name: "<<cname<<endl;
cout<<"Aggregate Marks: "<<agg_marks<<endl;
cout<<"Grade: "<<grade<<endl;} };
void main()
{ clrscr();
int n,g[50],temp=0;
candidate c[50];
cout<<"Enter number of candidates; ";
cin>>n;
for(int i=0; i<n; i++)
{ g[i]=c[i].getdata();
c[i].dispresult();
}
for(i=0;i<n-1;i++)
{for(int j=0;j<n-1-i;j++)
{ if(g[j]<g[j+1])
{ temp=g[j];
g[j]=g[j+1];
g[j+1]=temp;
}
}
}
cout<<"Results in descending order are: ";
for(i=0;i<n;i++)
cout<<[i]<<" ";
getch();
}
Q1. Given a text file “ABC.txt”, find out total number of words starting with Vowels.
#include<fstream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
int ctr=0;
char str[10]="";
ifstream fin;
fin.open("file.txt");
while(!fin.eof())
{fin>>str;
if(str[0]=='a'||str[0]=='e'||str[0]=='i'||str[0]=='o'||str[0]=='u')
ctr++;}
fin.close();
cout<<"number="<<ctr;
getch();
}
Q2. Given a text File “old.txt” , read its content and transfer all the content to a new file
called as “new.txt” , by changing all occurrences of the word “this” with “that”.

#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
create_file();
char str[20];
ifstream f1;
ofstream f2;
f2.open("new.txt");
f1.open("old.txt");
do
{f1>>str;
if(strcmp(str,"this")==0)
f2<<"that";
else
f2<<str;
} while(!f1.eof());
cout<<"completed...";
getch();
}

Das könnte Ihnen auch gefallen