Ds
Write a C++ program to Store N(Max 10) integers in an array and implement hash table
using division method , with linear probing with replacement
#include<iostream>
const int size=10;
using namespace std;
int hashKey(int key){
return key%size;
}
void insert(int hashMap[], int key){
int index=hashKey(key);
int originalIndex=index;
int inserted=false;
while(!inserted){
if (hashMap[originalIndex]==-1){
hashMap[originalIndex]=key;
inserted=true;
}
else{
hashMap[originalIndex]=key;
inserted=true;
}
originalIndex=(originalIndex+1)%size;
if(originalIndex==index){
cout<<"Hash table full!!!";
return;
}
}
}
void display(int hashMap[]){
for (int i=0;i<size;i++){
if (hashMap[i]==-1){
cout<<i<<": "<<"Empty"<<endl;
}
else{
cout<<i<<": "<<hashMap[i]<<endl;
}
}
}
int main()
{
int n,hashMap[size];
while(1){
cout<<"Enter the number of terms: ";
cin>>n;
if(n>size){
cout<<"You have gone above the limit of number of terms!!!!!"<<endl;
cout<<"Enter again"<<endl;
continue;
}
break;
}
for (int i=0;i<size;i++){
hashMap[i]=-1;
}
for (int i=0;i<n;i++){
int key;
cout<<"Enter the key: ";
cin>>key;
insert(hashMap, key);
}
display(hashMap);
return 0;
}
with replacement~~~~
Write a C++ program to Store N(Max 10) integers in an array and implement hash table
using division method , with linear probing without replacemen
#include<iostream>
const int size=10;
using namespace std;
int hashKey(int key){
return key%size;
}
void insert(int hashMap[], int key){
int index=hashKey(key);
int originalIndex=index;
int inserted=false;
while(!inserted){
if (hashMap[originalIndex]==-1){
hashMap[originalIndex]=key;
inserted=true;
}
else{
originalIndex=(originalIndex+1)%size;
if(originalIndex==index){
cout<<"Hash table full!!!";
return;
}
}
}
}
void display(int hashMap[]){
for (int i=0;i<size;i++){
if (hashMap[i]==-1){
cout<<i<<": "<<"Empty"<<endl;
}
else{
cout<<i<<": "<<hashMap[i]<<endl;
}
}
}
int main()
{
int n,hashMap[size];
while(1){
cout<<"Enter the number of terms: ";
cin>>n;
if(n>size){
cout<<"You have gone above the limit of number of terms!!!!!"<<endl;
cout<<"Enter again"<<endl;
continue;
}
break;
}
for (int i=0;i<size;i++){
hashMap[i]=-1;
}
for (int i=0;i<n;i++){
int key;
cout<<"Enter the key: ";
cin>>key;
insert(hashMap, key);
}
display(hashMap);
return 0;
}
without replacement
Q24
#include <iostream>
#include <fstream>
using namespace std;
fstream file;
class student
{
public:
string name;
int rollNo;
int marks;
};
void addStudent()
{
student s1;
file.open("studentData.txt", ios::app);
if (!file)
{
cout << "File not found"<<endl;
return;
}
cout << "Enter roll number:";
cin >> s1.rollNo;
cin.ignore();
cout << "Enter name:";
getline(cin, s1.name);
cout << "Enter marks:";
cin >> s1.marks;
file << s1.rollNo << ' ' << s1.name << ' ' << s1.marks << endl;
file.close();
}
void findStudent(int rollno)
{
bool found = false;
student s1;
file.open("studentData.txt",ios::in);
if (!file)
{
cout << "File not found"<<endl;
return;
}
while (file >> s1.rollNo>> s1.name >> s1.marks)
{
if (rollno == s1.rollNo)
{
cout << "Student found.....\n Here are the details...\n name:" << s1.name << endl
<< "\n Roll number:" << s1.rollNo << endl
<< "\n marks:" << s1.marks << endl;
file.close();
return ;
}
}
cout<<"Not found!!!"<<endl;
file.close();
}
int main()
{
int choice;
while (1)
{
cout <<"1.Adding a student's data\n2.finding a student's data\n3.Exit\nEnter your choice:";
cin >> choice;
switch (choice)
{
case 1:
addStudent();
break;
case 2:
int rollno;
cout << "Enter the roll number of the student:";
cin >> rollno;
findStudent(rollno);
break;
case 3:
cout << "Exiting Program";
return 0;
default:
cout << "Invalid choice! Please try again.\n";
}
}
return 0;
}
Q25
#include <iostream>
#include <fstream>
using namespace std;
fstream file;
class student
{
public:
string name;
int rollNo;
int salary;
};
void addEmployee()
{
student s1;
file.open("EmployeeData.txt", ios::app);
if (!file)
{
cout << "File not found"<<endl;
return;
}
cout << "Enter roll number:";
cin >> s1.rollNo;
cin.ignore();
cout << "Enter name:";
getline(cin, s1.name);
cout << "Enter salary:";
cin >> s1.salary;
file << s1.rollNo << ' ' << s1.name << ' ' << s1.salary << endl;
file.close();
}
void findEmployee(int rollno)
{
bool found = false;
student s1;
file.open("EmployeeData.txt",ios::in);
if (!file)
{
cout << "File not found"<<endl;
return;
}
while (file >> s1.rollNo>> s1.name >> s1.salary)
{
if (rollno == s1.rollNo)
{
cout << "Student found.....\n Here are the details...\n name:" << s1.name << endl
<< "\n Roll number:" << s1.rollNo << endl
<< "\n salary:" << s1.salary << endl;
file.close();
return ;
}
}
cout<<"Not found!!!"<<endl;
file.close();
}
int main()
{
int choice;
while (1)
{
cout <<"1.Adding a employee's data\n2.finding a employee's data\n3.Exit\nEnter your choice:";
cin >> choice;
switch (choice)
{
case 1:
addEmployee();
break;
case 2:
int rollno;
cout << "Enter the roll number of the employee:";
cin >> rollno;
findEmployee(rollno);
break;
case 3:
cout << "Exiting Program";
return 0;
default:
cout << "Invalid choice! Please try again.\n";
}
}
return 0;
}
Q26
#include <iostream>
#include <fstream>
using namespace std;
fstream file;
class item
{
public:
string name;
int number;
int price;
};
void addItem()
{
item s1;
file.open("ItemData.txt", ios::app);
if (!file)
{
cout << "File not found"<<endl;
return;
}
cout << "Enter item number:";
cin >> s1.number;
cin.ignore();
cout << "Enter item name:";
getline(cin, s1.name);
cout << "Enter price:";
cin >> s1.price;
file << s1.number << ' ' << s1.name << ' ' << s1.price << endl;
file.close();
}
void findItem()
{
int count = 0;
item s1;
file.open("ItemData.txt",ios::in);
if (!file)
{
cout << "File not found"<<endl;
return;
}
while (file >> s1.number>> s1.name >> s1.price)
{
if ( s1.price<100)
{
cout << "Items found....."<<endl<<"Here are the details..."<<endl<<endl<<" name:" << s1.name << endl
<< " Item number:" << s1.number << endl
<< " price:" << s1.price << endl<<endl;
count++;
}
}
if (count==0){
cout<<"There are no items which cost less than 100 rupees"<<endl;
}
file.close();
}
int main()
{
int choice;
while (1)
{
cout <<"1.Adding a item\n2.finding a items costing less than 100 rupees\n3.Exit\nEnter your choice:";
cin >> choice;
switch (choice)
{
case 1:
addItem();
break;
case 2:
int number;
findItem();
break;
case 3:
cout << "Exiting Program";
return 0;
default:
cout << "Invalid choice! Please try again.\n";
}
}
return 0;
}
Q27
#include <iostream>
#include <fstream>
using namespace std;
fstream file,tempfile;
class Account
{
public:
string name;
int accountNumber;
float balance;
};
void addAccount()
{
Account s1;
file.open("AccountData.txt", ios::app);
if (!file)
{
cout << "File not found"<<endl;
return;
}
cout << "Enter Account number:";
cin >> s1.accountNumber;
cin.ignore();
cout << "Enter Account name:";
getline(cin, s1.name);
cout << "Enter balance:";
cin >> s1.balance;
file << s1.accountNumber << ' ' << s1.name << ' ' << s1.balance << endl;
file.close();
}
void displayAccount()
{
Account s1;
file.open("AccountData.txt",ios::in);
if (!file)
{
cout << "File not found"<<endl;
return;
}
while (file >> s1.accountNumber>> s1.name >> s1.balance)
{
cout << "Accounts found....."<<endl<<"Here are the details..."<<endl<<endl<<" name:" << s1.name << endl
<< " Account accountNumber:" << s1.accountNumber << endl
<< " balance:" << s1.balance << endl<<endl;
}
file.close();
}
void deleteAccount(int accountNumber){
Account s1;
bool found=false;
file.open("AccountData.txt",ios::in);
tempfile.open("TemporaryAccountData.txt",ios::app);
if (!file)
{
cout << "File not found"<<endl;
return;
}
if (!tempfile)
{
cout << "Temporary file not found"<<endl;
return;
}
while (file >> s1.accountNumber>> s1.name >> s1.balance)
{
if (accountNumber!=s1.accountNumber){
tempfile<< s1.accountNumber << ' ' << s1.name << ' ' << s1.balance << endl;
}
else{
found=true;
}
}
file.close();
tempfile.close();
if (found){
remove("AccountData.txt");
rename("TemporaryAccountData.txt","AccountData.txt");
}
else{
cout<<"There is no such account"<<endl;
}
}
int main()
{
int choice;
while (1)
{
cout <<"1.Adding a Account\n2.Display all account details\n3.Deleting a Accounts \n4.Exit\nEnter your choice:";
cin >> choice;
switch (choice)
{
case 1:
addAccount();
break;
case 2:
displayAccount();
break;
case 3:
int accountNumber;
cout<<"Enter account number to delete=";
cin>>accountNumber;
deleteAccount(accountNumber);
break;
case 4:
cout << "Exiting Program";
return 0;
default:
cout << "Invalid choice! Please try again.\n";
}
}
return 0;
}
Q28
#include <iostream>
#include <fstream>
using namespace std;
fstream file;
class Customer
{
public:
string name;
int ID;
int due_Amount;
};
void addCustomer()
{
Customer s1;
file.open("CustomerData.txt", ios::app);
if (!file)
{
cout << "File not found"<<endl;
return;
}
cout << "Enter customer ID:";
cin >> s1.ID;
cin.ignore();
cout << "Enter customer name:";
getline(cin, s1.name);
cout << "Enter due_Amount:";
cin >> s1.due_Amount;
file << s1.ID << ' ' << s1.name << ' ' << s1.due_Amount << endl;
file.close();
}
void displayCustomer(){
Customer s1;
file.open("CustomerData.txt",ios::in);
if (!file)
{
cout << "File not found"<<endl;
return;
}
cout << "\nHere are the Customer details...\n\n"<<endl ;
while (file >> s1.ID>> s1.name >> s1.due_Amount)
{
cout <<"name:" << s1.name << endl
<< "Customer ID:" << s1.ID << endl
<< " marks:" << s1.due_Amount <<endl<<"******************************"<< endl<<endl;
}
file.close();
}
void totalCustomerDue()
{
int totalDue=0;
Customer s1;
file.open("CustomerData.txt",ios::in);
if (!file)
{
cout << "File not found"<<endl;
return;
}
while (file >> s1.ID>> s1.name >> s1.due_Amount)
{
totalDue+=s1.due_Amount;
}
cout<<"Total Due:"<<totalDue<<endl;
file.close();
}
int main()
{
int choice;
while (1)
{
cout <<"1.Adding a Customer's data\n2.Displaying all Customer's data\n3.Displaying total due\n4.Exit\nEnter your choice:";
cin >> choice;
switch (choice)
{
case 1:
addCustomer();
break;
case 2:
displayCustomer();
break;
case 3:
totalCustomerDue();
break;
case 4:
cout << "Exiting Program";
return 0;
default:
cout << "Invalid choice! Please try again.\n";
}
}
return 0;
}