Professional Documents
Culture Documents
void main()
{
clrscr();
int n;
cout<<"\nEnter the number of customers:\t";
cin>>n;
for(int i=0; i<n; i++)
{
cout<<"\nEnter Customer Number:\t";
cin>>b[i].customerno;
cout<<"\nEnter number of units consumed: ";
cin>>b[i].units;
if(b[i].units<=100)
b[i].bill=(b[i].units*0.40);
if((b[i].units>100)&&(b[i].units<=300))
b[i].bill=(b[i].units-100)*0.50+40;
if((b[i].units>300)&&(b[i].units<=600))
b[i].bill=(b[i].units-300)*0.75+140;
if((b[i].units>600)&&(b[i].units<=1000))
b[i].bill=(b[i].units-600)*1+365;
if(b[i].units>1000)
b[i].bill=(b[i].units-1000)*1.5+765;
}
void main()
{
student s[100];
float max, min;
int jmax, jmin;
clrscr();
for(int i=0; i<10; i++)
{
cout<<"\nEnter name of student"<<(i+1)<<":";
gets(s[i].name);
cout<<"Enter total marks of the student: ";
cin>>s[i].marks;
}
max=min=s[0].marks;
jmax=jmin=0;
for(i=0; i<10; i++)
{
if(s[i].marks>max)
{
max=s[i].marks;
jmax=i;
}
if(s[i].marks<min)
{
min=s[i].marks;
jmin=i;
}
}
cout<<"\n"<<s[jmax].name<<" scored the highest marks: "<<s[jmax].marks;
cout<<"\n"<<s[jmin].name<<" scored the lowest marks: "<<s[jmin].marks;
getch();
}
Program 4: WAP to enter marks in 5 subjects and to a lot streams
accordingly.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class student
{
char name[30];
int rno;
char sub[20];
float m1,m2,m3,m4,m5;
float perc;
void calc()
{
perc=(m1+m2+m3+m3+m4+m5)/5;
}
public:
void getdata();
void display();
};
void student::getdata()
{
cout<<"\n Enter roll no: ";
cin>>rno;
cout<<"\n Enter name: ";
gets(name);
cout<<"\n Enter marks for 5 subjects ";
cin>>m1>>m2>>m3>>m4>>m5;
calc();
if(perc>=96)
strcpy(sub,"Computer Science");
else if(perc>=91)
strcpy(sub,"Electronics");
else if(perc>=86)
strcpy(sub,"Mechanical");
else if(perc>=81)
strcpy(sub,"Electrical");
else if(perc>=76)
strcpy(sub,"Chemistry");
else if(perc>=71)
strcpy(sub,"Civil");
}
void student::display()
{
cout<<"Alloted subjects is "<<sub;
}
void main()
{
clrscr();
student s;
s.getdata();
s.display();
getch();
}
Program 5: WAP to read a telephone directory with name, phone and write
it to a file in the format “Hi <name>, your number is <phone>”.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
struct tel
{
char name[50];
long pno;
};
void main()
{
clrscr();
tel t; fstream f1,f2; char ch;
f1.open("teledir.txt",ios::out);
do
{
cout<<"\n Enter name: ";
cin>>t.name;
cout<<"\n Enter telephone no :";
cin>>t.pno;
f1.write((char*)&t,sizeof(t));
cout<<"\n Want to enter more?(Y/N): ";
cin>>ch;
}
while(ch=='y'||ch=='Y');
f1.close();
f1.open("teledir.txt",ios::in);
f2.open("newfile.txt",ios::out);
while(f1.read((char*)&t,sizeof(t)))
{
f2<<"Hi "<<t.name<<", your number is "<<t.pno<<"\n";
}
f1.close();
f2.close();
cout<<"\n\t New file created";
cout<<"\n\n\t Showing new file :";
f2.open("newfile.txt",ios::in);
char st[80];
while(!f2.eof())
{
cout<<"\n";
f2.getline(st,80,'\n');
cout<<st;
}
f1.close();
f2.close();
getch();
}
struct date
{
int dd,mm,yy;
};
class student
{
private:
int admn;
char name[20];
char address[100];
date dob;
int clas;
char section;
public:
void getdata()
{
cout<<"\n Enter admission number: ";
cin>>admn;
cout<<"\n Enter name: ";
cin>>name;
cout<<"\n Enter address: ";
cin>>address;
cout<<"\n Enter date of birth (dd-mm-yy): ";
cin>>dob.dd>>dob.mm>>dob.yy;
cout<<"\n Enter clas: ";
cin>>class;
cout<<"\n Enter section: ";
cin>>section;
}
void showdata()
{
cout<<"\n Name: "<<name;
cout<<"\n Class: "<<clas;
cout<<"\n Section: "<<section;
}
}ob[20];
void main()
{
clrscr();
fstream file("admission.txt",ios::in|ios::out);
int n;
cout<<"\n How many entries? ";
cin>>n;
for(int i=0; i<n; i++)
{
ob[i].getdata();endl;endl;
file.write((char*)&ob,sizeof(ob));
}
cout<<"\n\t Student Information "<<endl;
for(int k=0; k<n; k++)
{
file.read((char*)&ob,sizeof(ob));
cout<<"\n Your roll no: "<<k+1;
ob[k].showdata();
}
getch();
}
int queue::AddPatientAtBeginning(patient p)
{
// adds a critically ill patient to the beginning of the queue.
// returns 1 if successful, 0 if queue is full.
int i;
if (NumberOfPatients >= MAXPATIENTS)
{
// queue is full
return 0;
}
// move all patients one position back in queue
for (i = NumberOfPatients-1; i >= 0; i--)
{
List[i+1] = List[i];
}
// put in new patient
List[0] = p; NumberOfPatients++;
return 1;
}
patient queue::GetNextPatient (void)
{
// gets the patient that is first in the queue.
// returns patient with no ID if queue is empty
int i; patient p;
if (NumberOfPatients == 0) {
// queue is empty
strcpy(p.ID,"");
return p;}
// get first patient
p = List[0];
// move all remaining patients one position forward in queue
NumberOfPatients--;
for (i=0; i<NumberOfPatients; i++)
{
List[i] = List[i+1];
}
// return patient
return p;
}
int queue::RemoveDeadPatient (patient * p)
{
// removes a patient from queue.
// returns 1 if successful, 0 if patient not found
int i, j, found = 0;
// search for patient
for (i=0; i<NumberOfPatients; i++)
{
if (stricmp(List[i].ID, p->ID) == 0)
{
// patient found in queue
*p = List[i]; found = 1;
// move all following patients one position forward in queue
NumberOfPatients--;
for (j=i; j<NumberOfPatients; j++)
{
List[j] = List[j+1];
}
}
}
return found;
}
void queue::OutputList (void)
{
// lists entire queue on screen
int i;
if (NumberOfPatients == 0)
{
cout << "\nQueue is empty";
}
else
{
for (i=0; i<NumberOfPatients; i++)
{
cout << " \n" << List[i].FirstName;
cout << " \n" << List[i].LastName;
cout << " \n" << List[i].ID;
}
}
}
struct node
{
int year;
int houseno;
node *link;
};
// Function prototype declaration for add stack, delete stack, and show stack
void main()
{
node *top;
int tyear, thouseno, choice;
char opt = 'Y'; // To continue the do loop in case
top = NULL; // Initialization of Stack
clrscr();
do
{
cout << "\n\t\t Main Menu";
cout << "\n\t1. Addition of Stack";
cout << "\n\t2. Deletion from Stack";
cout << "\n\t3. Traverse of Stack";
cout << "“\n\t4. Exit from Menu";
cout << "\n\nEnter your choice from above ";
cin >> choice;
switch (choice)
{
case 1:
do
{
cout << "Enter the year of birth: ";
cin >> tyear;
cout << "Enter house number : ";
cin >> thouseno;
top = push(top, tyear, thouseno);
cout << "\nDo you want to add more elements <Y/N> ? ";
cin >> opt;
}
while (toupper(opt) == 'Y');
break;
case 2:
opt = 'Y'; // Initialize for the second loop
do
{
top = pop(top);
if (tyear != -1)
cout << "Value deleted from Stack is " << tyear;
cout << "\nDo you want to delete more elements <Y/N> ? ";
cin >> opt;
}
while (toupper(opt) == 'Y');
break;
case 3:
show_Stack(top);
break;
case 4:
exit(0);
}
}
while (choice != 4);
}
struct node
{
int Eno;
float Salary;
node *link;
};
void main()
{
node *front, *rear;
int val;
float val1;
int choice;
char opt = 'Y'; // To continue the do loop in case
front = rear = NULL; // Initialization of Queue
clrscr();
do
{
cout << "\n\t\t Main Menu";
cout << "\n\t1. Addition of Queue";
cout << "\n\t2. Deletion from Queue";
cout << "\n\t3. Traverse of Queue";
cout << "\n\t4. Exit from Menu";
cout << "\n\nEnter Your choice from above ";
cin >> choice;
switch (choice)
{
case 1:
do
{
cout << "Enter the value to be added in the queue ";
cin >> val;
cin >> val1;
rear = add_Q(rear, val,val1);
if (front == NULL)
front = rear;
cout << "\nDo you want to add more element <Y/N>? ";
cin >> opt;
}
while (toupper(opt) == 'Y');
break;
case 2:
opt = 'Y'; // Initialize for the second loop
do
{
front = del_Q(front, val, val1);
if (front == NULL)
rear = front;
if (val != -1)
cout << "Value deleted from Queue is " << val;
cout << "\nDo you want to delete more element <Y/N>? ";
cin >> opt;
}
while (toupper(opt) == 'Y');
break;
case 3:
show_Q(front);
break;
case 4:
exit(0);
}
}
while (choice != 4);
}
class telephone
{
char name[30];
char address[50];
double tno;
public :
void input()
{
cout<<"\n Enter the name ";
gets(name);
cout << "\n Enter address ";
gets(address);
cout<<"\n Enter the telephone number ";
cin>>tno;
}
void show()
{
cout << "\n Name "<<name;
cout << "\n Address "<<address;
}
double rt_tno()
{
return tno;
}
}tele;
void append()
{
ofstream tfile;
telephone tele;
tfile.open("tele.dat", ios :: app);
int n,i;
cout<< "Enter how many customers ";
cin>>n;
for (i =0; i<n ;i++)
{
tele.input();
tfile.write((char *)& tele,sizeof(tele));
}
tfile.close();
}
void display()
{
ifstream tfile;
tfile.open("tele.dat",ios :: binary);
int no,flag;
flag = 0;
cout<< "\n Enter telephone number to be searched ";
cin>>no;
while(tfile)
{
tfile.read((char *)&tele , sizeof(tele));
if(!tfile)
break;
if (tele.rt_tno() == no)
{
tele.show();
flag = 1;
}
}
if (flag == 0)
cout<< "\n Record does not exist ";
}
void main()
{
clrscr();
int ch;
cout << " 1. For append record ";
cout <<"\n 2. For search ";
cout << "\n 3. For exit";
cin >> ch;
switch (ch)
{
case 1:
append();
break;
case 2:
display();
break;
case 3:
exit(0);
}
getch();
}
Program 13: Declare a class having S.No. , SName , Fees and required
functions. Write a menu driven program to:
1. Append record in a file
2. Delete the record a given S. No.
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
class Students
{
char sname[30];
int sno;
float fees;
public:
void input()
{
cout<<"\nEnter Student Name: ";
gets(sname);
cout<<"Enter Roll No.: ";
cin>>sno;
cout<<"Enter Fees: ";
cin>>fees;
}
void display()
{
cout<<"\nStudent Name: "<<sname<<"\tRoll No.: "<<sno<<"\tFees: "<<fees<<"\t";
}
int getsno()
{
return sno;
}
};
void main()
{
clrscr();
students s;
int n,i,j;
fstream ofile,afile;
char ch,ch1;
do
{
cout<<"\n\t1.Add records\n\t2.Search Records\n\t3.Delete Records\n\t4.Exit";
cin>>ch;
switch(ch)
{
case '1' :
ofile.open("std.dat",ios::out|ios::binary);
cout<<"\nEnter no. of records to be Entered: ";
cin>>n;
for(i=0;i<n;i++)
{
s.input();
ofile.write((char*)&s,sizeof(students));
}
ofile.close();
break;
case '2' :
cout<<"\nEnter Roll No. to be searched: ";
int sn,flag=0;
cin>>sn;
afile.open("std.dat",ios::in);
while(afile)
{
afile.read((char *)&s,sizeof(students));
if(!afile)
break;
if (sn==s.getsno())
{
s.display();
flag=1;
break;
}
}
if(flag==0)
cout<<"\n No record Found";
afile.close();
break;
case '3' :
cout<<"\nEnter Roll No. to be Deleted ";
int sn1,flag1=0;
cin>>sn1;
afile.open("std.dat",ios::in|ios::binary);
ofile.open("tmpstd.dat",ios::out|ios::binary);
while(afile)
{
afile.read((char *)&s,sizeof(students));
if(!afile)
break;
if (sn1==s.getsno())
{
flag1=1;
}
else
{
ofile.write((char *)&s,sizeof(students));
}
}
if(flag1==0)
cout<<"\n No record Found";
afile.close();
ofile.close();
afile.open("tmpstd.dat",ios::in|ios::binary);
ofile.open("std.dat",ios::out|ios::binary);
while(afile)
{
afile.read((char *)&s,sizeof(students));
ofile.write((char *)&s,sizeof(students));
}
afile.close();
ofile.close();
break;
case '4' :
exit(0);
}
cout<<"\n\t DO U want to continue ";
cin>>ch1;
}
while(ch1=='Y'||ch1=='y');
getch();
}
Program 14: Each node of a queue contains ENo., salary, pointer field.
Front is the first node and rear is the last node. Write a menu driven program
to:
1. Add elements in the queue
2. Delete elements from queue
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
struct node
{
int Eno;
float Salary;
node *link;
};
void main()
{
node *front, *rear;
int val;
float val1;
int choice;
char opt = 'Y'; // To continue the do loop in case
front = rear = NULL; // Initialization of Queue
clrscr();
do
{
cout << "\n\t\t Main Menu";
cout << "\n\t1. Addition of Queue";
cout << "\n\t2. Deletion from Queue";
cout << "\n\t3. Traverse of Queue";
cout << "\n\t4. Exit from Menu";
cout << "\n\nEnter Your choice from above ";
cin >> choice;
switch (choice)
{
case 1:
do
{
cout << "Enter the value to be added in the queue ";
cin >> val;
cin >> val1;
rear = add_Q(rear, val,val1);
if (front == NULL)
front = rear;
cout << "\nDo you want to add more element <Y/N>? ";
cin >> opt;
}
while (toupper(opt) == 'Y');
break;
case 2:
opt = 'Y'; // Initialize for the second loop
do
{
front = del_Q(front, val, val1);
if (front == NULL)
rear = front;
if (val != -1)
cout << "Value deleted from Queue is " << val;
cout << "\nDo you want to delete more element <Y/N>? ";
cin >> opt;
}
while (toupper(opt) == 'Y');
break;
case 3:
show_Q(front);
break;
case 4:
exit(0);
}
}
while (choice != 4);
}
Program 15: Each node of a stack contains Roll no., Age, pointer fields. Top
is the first node of the stack. Write a menu driven program to:
1. Push an element
2. Pop an element
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
struct node
{
int roll;
int age;
node *link;
};
// Function prototype declaration for add stack, delete stack, and show stack
void main()
{
node *top;
int troll, tage, choice;
char opt = 'Y'; // To continue the do loop in case
top = NULL; // Initialization of Stack
clrscr();
do
{
cout << "\n\t\t Main Menu";
cout << "\n\t1. Addition of Stack";
cout << "\n\t2. Deletion from Stack";
cout << "\n\t3. Traverse of Stack";
cout << "“\n\t4. Exit from Menu";
cout << "\n\nEnter your choice from above ";
cin >> choice;
switch (choice)
{
case 1:
do
{
cout << "Enter the roll no. : ";
cin >> troll;
cout << "Enter age : ";
cin >> tage;
top = push(top, troll, tage);
cout << "\nDo you want to add more elements <Y/N> ? ";
cin >> opt;
}
while (toupper(opt) == 'Y');
break;
case 2:
opt = 'Y'; // Initialize for the second loop
do
{
top = pop(top);
if (troll != -1)
cout << "Value deleted from Stack is " << troll;
cout << "\nDo you want to delete more elements <Y/N> ? ";
cin >> opt;
}
while (toupper(opt) == 'Y');
break;
case 3:
show_Stack(top);
break;
case 4:
exit(0);
}
}
while (choice != 4);
}
Program 16: WAP which accepts a 2-D array of integers and its size as
arguments and display elements which are exactly 2-digit number.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10][10],i,j,m,n;
cout<<"\nEnter no. of rows:\n";
cin>>m;
cout<<"\nEnter the no. of columns:\n";
cin>>n;
cout<<"\nEnter the Elements of the array:\n";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
twodigit(a,m,n);
getch();
}
void main()
{
clrscr();
char str1[500],str2[500];
cout<<"\n\n1. Length of string"<<endl;
cout<<"2.Compare two strings"<<endl;
cout<<"3.Concatenate two strings"<<endl;
char ch;
cout<<"\n\nEnter your choice:";
cin>>ch;
switch(ch)
{
case '1':cout<<"\n\nEnter a string-";
gets(str1);
cout<<"\nLength of string:"<<mystrlen(str1)<<endl;
break;
case '2':cout<<"\n\nEnter string 1-";
gets(str1);
cout<<"\nEnter string 2-";
gets(str2);
if(mystrcmp(str1,str2))
{
cout<<"\nStrings do not match"<<endl;
} else
{
cout<<"\nStrings match"<<endl;
} break;
}
getch();
}
int mystrlen(char st[])
{
int I=0;
for(I=0;st[I]!='\0';I++);
return I;
}
switch(ch)
{
case '1': cout <<"\n\n Enter radius:";
cin>>a;
ar=area(a); break;