call A Function That Returns The Largest Sum of Contiguous Integers in The Array
call A Function That Returns The Largest Sum of Contiguous Integers in The Array
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[100],n,temp[100],ptr=0,temp1=0;
cout<<"How many number you wish to enter: ";
cin>>n;
cout<<"Enter the numbers: ";
cout<<endl;
for(int k=0;k<n;k++)
{
cout<<" ";
cin>>arr[k];
}
for(int i=0;i<100;i++)
temp[i]=0;
for(int j=0;j<n;j++)
{
ptr=0;
for(int i=j;i<n;i++) //Loop to enter numbers
{
ptr=ptr+arr[i];
if(ptr>temp[j])
{
temp[j]=ptr;
}
}
}
for(int s=0;s<n;s++)
{
if(temp[s]>temp1)
{
temp1=temp[s];
}
}
cout<<"The largest sum of contiguous integers is "<<temp1;
getch();
}
1
OUTPUT:
How many number you wish to enter: 4
Enter the numbers:
10
25
40
35
The largest sum of contiguous integers is 110
2
//Sum n largest integers in an array of integers where
every integer is between 0 //and 9
PROGRAM 2
#include<iostream.h>
#include<conio.h>
int LargestSum(int arr[100],int n,int size)
{
int sum=0,ptr,max=0,temp[100];
for(int k=0;k<100;k++) //loop to initialize array with 0
{
temp[k]=0;
}
for(int i=0;i<n;i++) //loop find max value, store it, and replace with 0
{
for(int j=0;j<size;j++)
{
if(max<arr[j])
{
max=arr[j];
ptr=j;
}
}
arr[ptr]=0;
temp[i]=max;
max=0;
}
for(i=0;i<n;i++) //loop to sum
{
sum=sum+temp[i];
}
return sum;
}
void main()
{
clrscr();
int arr[100],n,size;
cout<<"How many number you wish to enter: ";
cin>>size;
cout<<"Enter the numbers"<<endl;
for(int k=0;k<size;k++)//input array
{
cin>>arr[k];
}
cout<<"\n\nEnter the number of nos. you wish to sum: ";
cin>>n;
int sum=LargestSum(arr,n,size);
cout<<"The sum of "<<n<<" largest integers is "<<sum;
getch();
}
3
OUTPUT:
How many number you wish to enter: 4
Enter the numbers
12
34
56
23
4
/* A program reads in 20 numbers each of which is between 10 and
100 inclusive, as each number is read, validate it and store it
in the array only it is not a duplicate of a number already read.
After reading all values, display only the unique values that the
user entered.*/
PROGRAM 3
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int arr[5],temp[5];
cout<<"Enter the numbers:"<<endl;
for(int i=0;i<5;i++) //loop to input and check value
{
cin>>arr[i];
label:
for(int j=0;j<i;j++)
{
if(arr[i]==temp[j])
{
ptr=1;
cout<<"Duplicate entry. Enter again.";
cin>>arr[i];
}
}
}
cout<<"The required array: "<<endl; //display array
for(int k=0;k<5;k++)
{
cout<<arr[k]<<endl;
}
getch();
}
5
OUTPUT:
Enter the numbers
15
20
35
21
99
The required array is:
15
20
35
21
99
6
/* Write a class with following members...
Data members
Bill Number as Integer
Customer's Name as Character array
Total bill amount as float
Member functions
A 3 parameter constructor to initialize bill number, customer's name
and total bill amount.
A function takes discount percentage as parameter and returns discount
figure
A function returns Net bill amount
Net bill amount = Total bill amount - Discount.
A function to print the bill.*/
PROGRAM 4
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class Bill
{
int Bill_No;
char Cust_Name[20];
float Total_Bill;
public:
Bill(int a,char b[20], float c) //constructor to initialize object
{
Bill_No=a;
strcpy(Cust_Name,b);
Total_Bill=c;
}
float Discount(float Disc_percent);
float Net_Bill(float Disc_figure);
};
float Bill::Discount(float Disc_percent) //calculate discount
{
float result=Disc_percent/100;
float ans=result*Total_Bill;
return ans;
}
{
return (Total_Bill-Disc_figure);
}
void main()
7
{
int x,z;
float Disc_percent,Disc_fig=12,Net_Bill;
char y[20];
clrscr();
cout<<"Enter Bill number: ";
cin>>x;
cout<<"Enter your name: ";
gets(y);
cout<<"Enter the total bill amount: ";
cin>>z;
Bill Obj(x,y,z);
cout<<"Enter discount percentage ";
cin>>Disc_percent;
cout<<endl;
Disc_fig=Obj.Discount(Disc_percent);
cout<<"Discount is "<<Disc_fig;
cout<<endl;
Net_Bill=Obj.Net_Bill(Disc_fig);
cout<<"Net Bill is "<<Net_Bill;
getch();
}
8
OUTPUT:
Enter Bill number: 90
Enter your name: DHRUV
Enter the total bill amount: 8000
Enter discount percentage 20
Discount is 1600
Net Bill is 6400
9
/* Write a program which has following class-:
Class employee
Data members
emno
name
designation
department
salary
Member functions
Acceptdata()
Displaydata()
Write a program which accepts data of 50 employees in an array and
does following-:
a)Displays the details of highest earning employee
b)Displays the employee list for employees working in Marketing
department
c)Modifies the salary of employee based on employee number(use Binary
search)*/
PROGRAM 5
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class Employee
{
int eno;
char name[20];
char designation[20];
int salary;
char department[20];
public:
10
void acceptdata() //to input data
{
cout<<"Enter employee number: ";
cin>>eno;
cout<<"Enter employee name: ";
gets(name);
cout<<"Enter employee's designation: ";
gets(designation);
cout<<"Enter employee's department: ";
gets(department);
cout<<"Enter employee's salary: ";
cin>>salary;
cout<<endl;
}
void displaydata() //to display data
{
cout<<endl;
cout<<"Employee number: ";
cout<<eno;
cout<<endl;
cout<<"Employee name: ";
puts(name);
cout<<"Employee's designation: ";
puts(designation);
cout<<"Employee's department: ";
puts(department);
cout<<"Employee's salary: ";
cout<<salary;
cout<<endl;
}
};
void main()
{
clrscr();
int j,max=0,empno,var;
char dept[20];
Employee emp[3]; //array of objects
for(int l=0;l<3;l++)
{
emp[l].acceptdata();
}
for(int i=0;i<3;i++)
{
if(max<emp[i].returnsalary()) //to find max salary
{
max=emp[i].returnsalary();
j=i;
}
}
cout<<endl<<endl;
cout<<"Details of maximum earning employee: ";
emp[j].displaydata();
cout<<endl;
11
{
strcpy(dept,emp[z].returndepartment());
if(!(strcmp(dept,"Marketing")))
{
emp[z].displaydata();
}
}
cout<<endl<<endl;
cout<<"Enter the employee number of the employee whose salary you wish to
modify: ";
cin>>empno;
for(int c=0;c<3;c++)//to find employee with the required employee number
{
var=emp[c].returnempno();
if(empno==var)
{
emp[c].changesal(); //to change salary
}
}
getch();
}
12
OUTPUT:
Enter employee number: 1
Enter employee name: ROHIT
Enter employee's designation: SENIOR MANAGER
Enter employee's department: SALES
Enter employee's salary: 25000
Enter the employee number of the employee whose salary you wish to modify: 2
Enter new salary: 60000
13
/*Given a class Customer
Data members
CustNo.
Cust Name
Address
TypeofAccount
CurrentBalance
Do the following, with regards to data file handling-:
1)Add N number of customers in a file customer.dat.
2)Search for any Customer detail based on name
3)Write a function which adds 10%interest to the currentbalance of
customers belonging to Saving Account Type.
4)Delete a customer*/
PROGRAM 6
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<string.h>
class Customer
{
int cust_no;
char cust_name[20];
char cust_address[30];
char account_type[15];
int curr_bal;
public:
void getdata() //to input data
{
cout<<"Enter customer number: ";
cin>>cust_no;
cout<<"Enter name: ";
gets(cust_name);
cout<<"Enter cusomer address: ";
gets(cust_address);
cout<<"Enter account type: ";
gets(account_type);
cout<<"Enter current balance: ";
cin>>curr_bal;
cout<<endl;
}
void display() //to show data
{
cout<<"Customer number: "<<cust_no<<endl;
cout<<"Name: "<<cust_name<<endl;
cout<<"Address: "<<cust_address<<endl;
14
cout<<"Account Type: "<<account_type<<endl;
cout<<"Current balance: "<<curr_bal<<endl;
}
char *getname() //to return name
{
return cust_name;
}
int getcustno() //to return customer number
{
return cust_no;
}
};
void main()
{
clrscr();
Customer s1;
char name[20];
char found='n';
char found1='n';
int rno;
clrscr();
Customer Obj[3];
fstream filio;
filio.open("Cust.dat",ios::in|ios::out); //opening file
if(!filio)
{
cout<<"Cannot open file!! \n";
}
cout<<"Enter details of three customers\n";
cout<<endl;
for(int i=0;i<3;i++)
{
Obj[i].getdata();
filio.write((char*)&Obj[i],sizeof(Obj[i])); //writing on file
}
cout<<endl<<endl;
filio.seekg(0);
cout<<"Enter the customer name whose details you wish to see: ";
gets(name);
cout<<endl;
while(!filio.eof()) //to find required customer
{
filio.read((char *)&s1,sizeof(s1));
if(!(strcmp(name,s1.getname())))
{
found='y';
s1.display();
}
}
if(found=='n')
{
cout<<"\nNo records found\n\n";
}
cout<<endl<<endl;
15
filio.seekg(0);
ofstream filout("temp.dat",ios::out);
cout<<"Enter customer number of the customer whose record you wish to delete:
";
cin>>rno;
while(!filio.eof())//to find required customer number
{
filio.read((char*)&s1,sizeof(s1));
if(s1.getcustno()==rno)
{
found1='y';
}
else
{
filout.write((char*)&s1,sizeof(s1));
}
}
if(found1=='n')
{
cout<<"Record not found";
}
filio.close();
filout.close();
remove("Cust.dat");
rename("temp.dat","cust.dat");
filio.open("cust.dat",ios::in|ios::out);
cout<<"File now contains\n\n";
filio.seekg(0);
while(!filio.eof())
{
filio.read((char*)&s1,sizeof(s1));
s1.display();
cout<<endl;
}
filio.close();
getch();
}
16
OUTPUT:
Enter details of three customers
Enter the customer name whose details you wish to see: AYUSH
Customer number: 1
Name: AYUSH
Address: R-8 ROHINI
Account Type: SAVINGS
Current balance: 15000
Enter customer number of the customer whose record you wish to delete:20
Record not found
17
//Write a program to find the total number of words starting with
A and ending with //n from a text file.
PROGRAM 7
#include<fstream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
ofstream fout("file1"); //opening file
char text[80];
int len=0;
char file2[20];
int count=0;
cout<<"Enter the text of the file: ";
cin.get(text,80);
fout<<text; //writing on file
fout.close();
ifstream fin("file1");
fin.seekg(0);
cout<<endl;
while(!fin.eof()) //reading from file
{
fin.getline(file2,20,' ' );
len=strlen(file2)-1;
if((file2[0]=='a')&&(file2[len]=='n')) //checking for words starting with
//and ending with n
{
count++;
}
}
cout<<"No of words starting with a and ending with n are "<<count;
getch();
}
18
OUTPUT:
Enter the text of the file: IN THE END AND TOMORROW
PROGRAM 8
include<fstream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
ofstream fout("file1");
char text[80];
int len=0;
char file2[20];
int countvow=0,countcons=0;
cout<<"Enter the text of the file: ";
cin.get(text,80);
fout<<text; //writing on file
fout.close();
ifstream fin("file1");
fin.seekg(0);
cout<<endl;
while(!fin.eof()) //reading from file
{
fin.getline(file2,20,' ');
if(file2[0]=='a'||file2[0]=='e'||file2[0]=='i'||file2[0] =='o'||
file2[0]=='u'||file2[0]=='A'||file2[0]=='E'||file2[0] =='I'||
file2[0]=='O'||file2[0]=='U')
{
countvow++;
}
else
{
countcons++;
}
}
cout<<"No of words starting with vowels are "<<countvow;
cout<<endl;
cout<<"No of words starting with consonants are "<<countcons;
getch();
}
19
OUTPUT:
Enter the text of the file: WE ALL LIVE IN DELHI
20
//write a program which reads a text file named “old.txt” in
reverse order and //transfers the reversed text in “reverse.txt”
PROGRAM 9
#include<fstream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
int len;
ofstream fout("old");
char text[80];
char text1[80];
cout<<"Enter the text of the file: ";
cin.get(text,80);
fout<<text; //writing on file
fout.close();
fout.open("new");
ifstream fin("old");
fin.seekg(0);
cout<<endl;
while(!fin.eof()) //reading from file
{
fin.getline(text1,80); //writing on new file
len=strlen(text1)-1;
for(int i=len;i>=0;i--)
{
fout<<text1[i];
}
}
fin.close();
fout.close();
fin.open("new");
cout<<"The reversed text is "; //to display reversed text
while(!fin.eof())
{
fin.getline(text1,80);
cout<<text1;
}
getch();
}
21
OUTPUT:
Enter the text of the file: I LOVE NY COUNTRY
PROGRAM 10
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 100 // Shows maximum array length
int stack[MAX]; // Declares array global
variable
int top; // Declares integer top
void push(int stack[], int val, int &top); // Add stack
int pop(int stack[], int &top); // Delete stack
void show_Stack(int stack[], int top); // Show stack
void main()
{
int choice, val;
char opt = 'Y'; // To continue the do loop in case
top = -1; // Initialization of Queue
clrscr();
do
{
cout << "\n\t\t Main Menu";
cout << "\n\t1. Addition of Stack";
cout << "\n\t2. Deletion from Stack";
cout << "\n\t3. Show 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 value to be added in the stack ";
cin >> val;
push(stack, val, top);
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
22
{
val = pop(stack, top);
if (val != -1)
cout << "Value deleted from statck is " << val;
cout << "\nDo you want to delete more elements <Y/N> ? ";
cin >> opt;
} while (toupper(opt) == 'Y');
break;
case 3:
show_Stack(stack, top);
break;
case 4:
exit(0);
}
}
while (choice != 4);
void push(int stack[], int val, int &top)//Function body for add stack
with //array
{
if (top == MAX - 1)
{
cout << "Stack Full ";
}
else
{
top = top + 1;
stack[top] = val;
}
}
int pop(int stack[], int &top)//Function body for delete stack with array
{
int value;
if (top < 0)
{
cout << "Stack Empty ";
value = -1;
}
else
{
value = stack[top];
top = top - 1;
}
return (value);
}
void show_Stack(int stack[], int top)//Function body for show stack with
//array
{
int i;
if (top < 0)
{
cout << "Stack Empty";
return;
}
i = top;
clrscr();
cout << "The values are ";
23
do
{
cout << "\n" << stack[i];
i = i - 1;
}while(i >= 0);
}
OUTPUT:
Main Menu
1. Addition of Stack
2. Deletion from Stack
3. Show of Stack
4. Exit from Menu
Main Menu
1. Addition of Stack
2. Deletion from Stack
3. Show of Stack
4. Exit from Menu
Main Menu
1. Addition of Stack
2. Deletion from Stack
3. Show of Stack
4. Exit from Menu
24
/* Stack implementation as a link list */
PROGRAM 11
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
struct node // Declares a stack structure
{
int roll;
int age;
node *link;
};
// Function prototype declaration for add stack, delete stack, and show stack
node *push(node *top, int val, int tage); // Add stack
node *pop(node *top); // Delete stack
void show_Stack(node *top); // Show stack
void main()
{
node *top;
int troll, tage, choice;
char opt = 'Y';
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. Show 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> ?";
25
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);
}
Node *push(node *top, int val, int tage)//Function body for adds stack
//elements
{
node *temp;
temp = new node;
temp->roll = val;
temp->age = tage;
temp->link = NULL;
if(top ==NULL)
top = temp;
else
{
temp->link = top;
top = temp;
}
return(top);
}
node *pop(node *top) // Function body for delete stack elements
{
node *temp;
int tage, troll;
clrscr();
if (top == NULL )
{
cout << "Stack Empty ";
troll = -1;
}
else
{
temp = top;
top = top->link;
troll = temp->roll;
tage = temp->age;
26
temp->link = NULL;
cout << "\n\tPopped Roll Number is : " << temp->roll;
cout << "\n\tPopped Age is : " << temp->age;
delete temp;
}
return (top);
}
void show_Stack(node *top) // Function body for show stack elements
{
node *temp;
temp = top;
clrscr();
cout << "The values are \n";
while (temp != NULL)
{
cout << "\n" << temp->roll << "\t" << temp->age;
temp = temp->link;
}
}
27
OUTPUT:
Main Menu
1. Addition of Stack
2. Deletion from Stack
3. Show of Stack
4. Exit from Menu
Main Menu
1. Addition of Stack
2. Deletion from Stack
3. Show of Stackô
4. Exit from Menu
Main Menu
1. Addition of Stack
2. Deletion from Stack
3. Show of Stack
4. Exit from Menu
1 20
28
// This program illustrates the basic operation of add queue,
delete queue and show queue
// using linked list.
PROGRAM 12
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
struct node // Declares a queue structure
{
int Eno;
float Salary;
node *link;
};
node *add_Q(node *rear, int val,float val1); // Add queue
node *del_Q(node *front, int &val, float &val1);// Delete queue
void show_Q(node *front); // Show queue
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. Show 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;
29
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);
}
node *add_Q(node *rear, int val, float val1)//Function body to add queue
//elements
{
node *temp;
temp = new node;
temp->Eno = val;
temp->Salary = val1;
temp->link = NULL;
rear->link = temp;
rear = temp;
return (rear);
}
node *del_Q(node *front, int &val, float &val1)// Function body to delete
//queue elements
{
node *temp;
clrscr();
if (front == NULL)
{
cout << "Queue Empty ";
val = -1;
}
else
{
temp = front;
front = front->link;
val = temp->Eno;
val1 = temp->Salary;
30
temp->link = NULL;
delete temp;
}
return (front);
}
void show_Q(node *front)// Function body to show queue elements
{
node *temp;
temp = front;
clrscr();
cout << "The Queue values are";
while (temp != NULL)
{
cout <<"\nENO : "<< temp->Eno;
cout <<"\nSalary : "<<temp->Salary;
temp = temp->link;
}
}
31
OUTPUT:
Main Menu
1. Addition of Queue
2. Deletion from Queue
3. Show of Queue
4. Exit from Menu
Main Menu
1. Addition of Queue
2. Deletion from Queue
3. Show of Queue
4. Exit from Menu
Main Menu
1. Addition of Queue
2. Deletion from Queue
3. Show of Queue
4. Exit from Menu
Enter Your choice from above 4
32
// This program illustrates the basic operation of circular to
add queue, delete queue,
// and show queue using array. The queue contains data of type
character.
PROGRAM 13
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 20 // Show maximum array length
char queue[MAX]; // Declares array global variable
int front, rear; // Declares integer front and read
// Function prototypes to add queue, delete queue and show queue in array
//implementation
void add_Q(char queue[], int front, char val, int &rear); // Add queue
char del_Q(char queue[], int &front, int rear); // Delete queue
void show_Q(char queue[], int front, int rear); // Show queue
void main()
{
int choice;
char val;
char opt = 'Y';
rear = -1; // Initialization of Queue
front = -1;
clrscr();
do
{
cout << "\n\t\t Main Menu";
cout << "\n\t1. Addition of Queue";
cout << "\n\t2. Deletion from Queue";
cout << "\n\t3. Show 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;
add_Q(queue, front, val, rear);
cout << "Do you want to add more element <Y/N>? ";
33
cin >> opt;
} while (toupper(opt) == 'Y');
break;
case 2:
opt = 'Y'; // Initialize for the second loop
do
{
val = del_Q(queue, front, rear);
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(queue, front, rear);
break;
case 4:
exit(0);
}
}
while (choice != 4);
}
// Function body to add circular queue with array of character
void add_Q(char queue[], int front, char val, int &rear)
{
if ((rear + 1) % MAX == front)
{
cout << "Queue Full ";
}
else
{
rear = (rear + 1) % MAX;
queue[rear] = val;
}
}
// Function body to delete circular queue with array of character
char del_Q(char queue[], int &front, int rear)
{
char value;
if (front == rear)
{
cout << "Queue Empty ";
value = -1;
}
else
{
front = (front + 1) % MAX;
value = queue[front];
}
return (value);
}
// Function body to show circular queue with array
void show_Q(char queue[], int front, int rear)
{
clrscr();
cout << "The values are ";
34
do
{
front = (front + 1) % MAX;
cout << "\n" << queue[front];
}while(front != rear);
}
OUTPUT:
Main Menu
1. Addition of Queue
2. Deletion from Queue
3. Show of Queue
4. Exit from Menu
Main Menu
1. Addition of Queue
2. Deletion from Queue
3. Traverse of Queue
4. Exit from Menu
Main Menu
1. Addition of Queue
2. Deletion from Queue
3. Traverse of Queue
4. Exit from Menu
35
// This program illustrates the basic operation of add queue,
delete queue and show queue
// using linked list. The queue contains data of type character.
PROGRAM 14
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
// Declares a queue structure
struct node
{
char data;
node *link;
};
// Functions prototype to add queue, delete queue, and show queue
node *add_Q(node *rear, char val); // Add queue
node *del_Q(node *front, char &val);// Delete queue
void show_Q(node *front); // Show queue
void main()
{
node *front, *rear;
char val;
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;
36
rear = add_Q(rear, val);
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);
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);
}
// Function body to add queue elements
node *add_Q(node *rear, char val)
{
node *temp;
temp = new node;
temp->data = val;
temp->link = NULL;
rear->link = temp;
rear = temp;
return (rear);
}
// Function body to delete queue elements
node *del_Q(node *front, char &val)
{
node *temp;
clrscr();
if (front == NULL)
{
cout << "Queue Empty ";
val = -1;
}
else
{
temp = front;
front = front->link;
val = temp->data;
temp->link = NULL;
delete temp;
37
}
return (front);
}
// Function body to show queue elements
void show_Q(node *front)
{
node *temp;
temp = front;
clrscr();
cout << "The Queue values are";
while (temp != NULL)
{
cout <<"\n"<< temp->data;
temp = temp->link;
}
}
38
OUTPUT:
Main Menu
1. Addition of Queue
2. Deletion from Queue
3. Traverse of Queue
4. Exit from Menu
Main Menu
1. Addition of Queue
2. Deletion from Queue
3. Traverse of Queue
4. Exit from Menu
Main Menu
1. Addition of Queue
2. Deletion from Queue
3. Traverse of Queue
4. Exit from Menu
39
// Program to read a text file and create another file that is
identical
// except that every sequence of consecutive blank space is
replaced by single blank.
PROGRAM 15
# include <fstream.h>
# include <string.h>
void main()
{
char ch;
int flag;
ifstream infile("DELHI.txt");
ofstream outfile("KHERA.txt");
while (infile)
{
infile.get(ch);
if (ch ==' ')
outfile.put(ch='.');
else
outfile.put(ch);
}
infile.close();
outfile.close();
}
OUTPUT:
DELHI.TXT
DELHI IS A VERY BEAUTIFUL PLACE AND KHERA IS APLACE ON THE OUTSKIRTS OF DELHI
WHERE WE HAVE VERY BEUTIFULL FIELDS.
KHERA.TXT
40
DELHI IS A VERY BEAUTIFUL PLACE AND KHERA IS APLACE ON THE OUTSKIRTS OF DELHI
WHERE WE HAVE VERY BEUTIFULL FIELDS.
PROGRAM 16
#include<iostream.h>
#include<conio.h>
#include<limits.h>
void bs(int arr[50],int);
void main()
{
clrscr();
int arr[50],arr1[51],s,i,c,k,item;
char ch;
do
{
cout<<"\n\nEnter the size of the array: ";
cin>>s;
cout<<"Enter the elements of the array: ";
for(i=0;i<s;i++)
cin>>arr[i];
k=s+1;
for(i=1;i<k;i++)
arr1[i]=arr[i-1];
bs(arr,s);
getch();
cout<<"\n\nWish to continue(y/n)?: ";
cin>>ch;
}
while(ch=='y'||ch=='Y');
getch();
}
void bs(int arr[50],int n)
{
int temp;
for(int i=0;i<n;i++)
{
for(int j=0;j<(n-1)-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
cout<<"The sorted array is: ";
41
for(i=0;i<n;i++)
cout<<arr[i]<<" ";
}
OUTPUT:
Enter the size of the array: 9
Enter the elements of the array: 1
10
15
20
30
25
45
5
35
The sorted array is: 1 5 10 15 20 25 30 35 45
Wish to continue(y/n)?: N
42
//sorting using insertion sort in an array//
PROGRAM 17
#include<iostream.h>
#include<conio.h>
#include<limits.h>
void is(int arr[50],int);
void main()
{
clrscr();
int arr[50],arr1[51],s,i,c,k,item;
char ch;
do
{
cout<<"\n\nEnter the size of the array: ";
cin>>s;
cout<<"\nEnter the elements of the array: ";
for(i=0;i<s;i++)
cin>>arr[i];
k=s+1;
for(i=1;i<k;i++)
arr1[i]=arr[i-1];
is(arr,s);
getch();
cout<<"\n\nwish to continue(y\n)?: ";
cin>>ch;
}
while(ch=='y'||ch=='Y');
getch();
}
void is(int arr[50],int n)
{
int tmp,i,j;
int MIN=arr[0];
for(i=1;i<n;i++)
{
tmp=arr[i];
j=i-1;
while(tmp<arr[j])
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=tmp;
43
}
cout<<"The sorted array is: ";
for(i=1;i<n;i++)
{
cout<<arr[i]<<" ";
}
}
OUTPUT:
Enter the size of the array: 5
Enter the elements of the array:10
40
50
15
20
The sorted array is:10 15 20 40 50
wish to continue(y/n)?: N
44
//sorting an array using selection sort//
PROGRAM 18
#include<iostream.h>
#include<conio.h>
#include<limits.h>
void ss(int arr[50],int);
void main()
{
clrscr();
int arr[50],arr1[51],s,i,c,k,item;
char ch;
do
{
cout<<"\n\nEnter the size of the array: ";
cin>>s;
cout<<"\nEnter the elements of the array: ";
for(i=0;i<s;i++)
cin>>arr[i];
k=s+1;
for(i=1;i<k;i++)
arr1[i]==arr[i-1];
ss(arr,s);
getch();
cout<<"\n\nwish to continue(y/n)?: ";
cin>>ch;
}
while(ch=='y'||ch=='Y');
getch();
}
void ss(int arr[50],int n)
{
int i,j,l,small,tmp;
for(i=0;i<n;i++)
{
small=arr[i];
l=i;
for(j=i+1;j<n;j++)
{
if(small>arr[j])
{
small=arr[j];
l=j; } }
tmp=arr[i];
arr[i]=arr[l];
45
arr[l]=tmp;
}
cout<<"\n\n\nthe sorted array is: ";
for(i=0;i<n;i++)
cout<<arr[i]<<" ";
OUTPUT:
Enter the size of the array: 5
Enter the elements of the array: 2
5
4
1
9
The sorted array is: 1 2 4 5 9
Wish to continue(y/n)?: N
46
ACKNOWLEDGEMENT
ROHIT JAIN
47
12-B
ROLL NUMBER-6487/27
PROGRAM 21
Write SQL command for (a) to (f) and write the outputs for (g) on the basis of
tables FURNITURE and ARRIVALS
Table : FURNITURE
Table : ARRIVALS
(a) To show all the information about the Baby cot from the FURNITURE table.
(b) To list the ITEMNAME which are priced at more than 15000 from the FURNITURE table.
(c) To display ITEMNAME and TYPE of those items, in which DATEOFSTOCK is before 22/01/02 from the
FURNITURE table in descending order of ITEMNAME
(d) To display ITEMNAME and DATEOFSTOCK of those items, in which the DISCOUNT percentage is more than
25 from FURNITURE table.
(e) To count the number of items, whose TYPE is "Sofa" from FURNITURE table.
(f) To insert a new row in the ARRIVALS table with the following data.
48
14, "Velvet touch", "Double Bed" [25/03/03], 25000, 30
(g) Give the output of following SQL statements:
Note: Outputs of the below mentioned queries should be based on original data given in both the tables i.e,
without considering the insertion done in (f) part of this question.
OUTPUT:
(a) SELECT * FROM FURNITURE WHERE Type=’Baby cot’;
(b) SELECT ITEMNAME FROM FURNITURE WHERE price>15000;
(c) SELECT ITEMNAME, TYPE FROM FURNITURE WHERE DATEOFSTOCK < {22/01/02}
ORDER BY ITEMNAME DESC;
(d) SELECT ITEMNAME, DATEOFSTOCK FROM FURNITURE where discount>25;
(e) SELECT COUNT (TYPE) FROM FURNITURE WHERE TYPE=’Sofa’;
(f) INSERT INTO ARRIVALS VALUES (14,’Velvet touch’, ’Double Bed’,{25/03/03} ,
25000,30);
(g) (i) 5
(ii) 30, 25
(iii) 15
(iv) 66500
49
PROGRAM 22
(a) Study the following tables FLIGHTS and FARES and write SQL commands for
the questions (i) to (iv) and give
Output for SQL queries (v) to (vi).
TABLE : FLIGHTS
TABLE : FARES
(i) Display FL_NO and NO_FLIGHTS from "KANPUR" to "BANGALORE" from the table FLIGHTS.
(ii) Arrange the contents of the table FLIGHTS in the ascending order of FL_NO.
(iii) Display the FL_NO and fare to be paid for the flights from DELHI to MUMBAI using the tables FLIGHTS and
FARES, where the fare to be paid = FARE + FARE*TAX%/100.
(iv) Display the minimum fare "Indian Airlines" is offering from the table FARES.
(v) SELECT FL_NO, NO_FLIGHTS, AIRLINES from FLIGHTS, FARES WHERE STARTING = "DELHI" AND
FLIGHTS.FL_NO = FARES.FL_NO.
50
(vi) SELECT count (distinct ENDING) from FLIGHTS.
OUTPUT:
(i) SELECT FL_NO,NO_FLIGHTS FROM FLIGHTS
WHERE STARTING=’KANPUR’ AND ENDING=’BANGALORE’;
(ii) SELECT * FROM FLIGHTS ORDER BY FL_NO;
(iii) SELECT F.FL_NO, R.FARE*(R.TAX/100) FROM FLIGHTS F, FARES R
WHERE F.STARTING=’DELHI’ AND F.ENDING=’MUMBAI’ AND F.FL_NO=R.FL_NO;
(iv) SELECT MIN (FARE) FROM FARES WHERE AIRLINES=’Indian Airlines’;
(v)
FL_NO NO_FLIGHT AIRLINES
AM501 1 JET AIRWAYS
IC302 8 INDIAN AIRLINES
IC701 4 INDIAN AIRLINES
(vi) 7
51