Question
in c++ please
Write a program in c+ to do the following: 1.Save the data shown below (in the same format) to make a text file called Input

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ CODE:

#include<iostream>
#include<fstream>
using namespace std;
class Node
{
public:
string fname,lname,birthdate;
bool occupied;
Node(string f,string l,string b)
{
fname=f;
lname=l;
birthdate=b;
occupied=true;
}
Node()
{
fname=lname=birthdate="";
occupied=false;
}
};
int main()
{
ifstream fin;
fin.open("InputData.txt");
int count=0;
string data;
while(fin)
{
fin>>data;
fin>>data;
fin>>data;
count++;
}
fin.close();
count-=1;
Node table[count+1];
fin.open("InputData.txt");
string fname,lname,birthdate;
int collisions=0;
for(int i=0;i<count;i++)
{
fin>>fname;
fin>>lname;
fin>>birthdate;
int hash_value=0;
for(int j=0;j<birthdate.size();j++)
{
if(birthdate[j]!='-')
hash_value+=(birthdate[j]-'0');
}
hash_value=hash_value%10+hash_value/10;
int k=0;
while(table[(hash_value+k*k)%count+1].occupied!=false)
{
k++;
collisions++;
}
table[(hash_value+k*k)%count+1].fname=fname;
table[(hash_value+k*k)%count+1].lname=lname;
table[(hash_value+k*k)%count+1].birthdate=birthdate;
table[(hash_value+k*k)%count+1].occupied=true;
}
cout<<"Data items loaded: "<<count<<endl;
cout<<"Load factor: "<<(float)count/(float)(count+1)<<endl; //Because no. of data items +1 = no. of slots in hash table
cout<<"Total collisions: "<<collisions<<endl;
while(true)
{
cout<<"Enter 1 to search by birthdate and 0 to exit: ";
int i;
cin>>i;
if(i==1)
{
cout<<"Enter birthdate: ";
string bdate;
cin>>bdate;
int hash_value=0;
for(int j=0;j<bdate.size();j++)
{
if(bdate[j]!='-')
hash_value+=(bdate[j]-'0');
}
hash_value=hash_value%10+hash_value/10;
int k=0;
int found=0;
while(table[(hash_value+k*k)%count+1].occupied==true)
{
if(table[(hash_value+k*k)%count+1].birthdate==bdate)
{
cout<<table[(hash_value+k*k)%count+1].fname<<" "<<table[(hash_value+k*k)%count+1].lname<<" corresponds to birthdate "<<bdate<<endl;
found=1;
break;
}
k++;
}
if(found==0)
cout<<"Not found"<<endl;
}
else if(i==0)
break;
else
{
cout<<"Invalid input...Try again"<<endl;
}
}
fin.close();
return 0;
}

Input file: InputData.txt

InputData Notepad File Edit Format View Help Dohn Wick 2019-01-31 David Watson 2018-02-01 Jane Pattinson 2017-03-28 Ros Buttl

Output

CAUsers Ashish Shrivastav\Desktop C,C Programs\hashtable.exe Data items loaded: 10 Load factor: 0.909091 Total collisions:

Add a comment
Know the answer?
Add Answer to:
in c++ please Write a program in c+ to do the following: 1.Save the data shown...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++. You are to write a program to set up a data base for a phone...

    C++. You are to write a program to set up a data base for a phone index. The key will be a person’s name and the table will supply the phone number. The data base must be implemented as a hash table with the names as the key. The format will be a person name (multiple parts) followed by a phone number. Example:   John Doe   601-5433   or O E Vay 921-1234. Only one name one phone number will be on...

  • (C++) Write a program that declares a struct to store the data of a football player...

    (C++) Write a program that declares a struct to store the data of a football player (player’s name, player’s position, number of touchdowns, number of catches, number of passing yards, number of receiving yards, and the number of rushing yards). Declare an array of 10 components to store the data of 10 football players. Your program must contain a function to input data and a function to output data. Add functions to search the array to find the index of...

  • C++ : Please include complete source code in answer This program will have names and addresses...

    C++ : Please include complete source code in answer This program will have names and addresses saved in a linked list. In addition, a birthday and anniversary date will be saved with each record. When the program is run, it will search for a birthday or an anniversary using the current date to compare with the saved date. It will then generate the appropriate card message. Because this will be an interactive system, your program should begin by displaying a...

  • Programming in C/C++ Submit your source code files (all .h and .cpp files) NO GLOBAL VARIABLES...

    Programming in C/C++ Submit your source code files (all .h and .cpp files) NO GLOBAL VARIABLES Program: Use operator overloaded functions for a birthday club – Based on Chapter 11 lecture (25 pts) Make a class called Birthday that will have a date of month, day, and year and the name. Need to have overloaded operators to compare values along with regular functions like: Overload operator == that takes a Birthdate, compares against the current date values, and returns a...

  • Write a C program that will act as a database access tool. Three separate functions are...

    Write a C program that will act as a database access tool. Three separate functions are needed to: 1. Print dates 2. Print names 3. Print category numbers and the average of the numbers. A random text file (from notepad) is supplied for the dates, names, and category's. For Example: Storm name: Storm Date: Storm category number: The program must ask the user which of the three items listed above they want to run instead of dumping all of the...

  • LINUX C programming Please make sure the output is correct Please do read data from file,...

    LINUX C programming Please make sure the output is correct Please do read data from file, the example below is just a example Third: Hash table (20 points) In this part, you will implement a hash table containing integers. The hash table has 10,000 buckets. An important part of a hash table is collision resolution. In this assignment, we want you to use chaining with a linked list to handle a collision. This means that if there is a collision...

  • Implement a software program in C++ t stores and searches the Student records using double-hashing algorithm.  Double...

    Implement a software program in C++ t stores and searches the Student records using double-hashing algorithm.  Double hashing uses the idea of applying a second hash function to key when a collision occurs.   The software program will be based on the following requirements: Development Environment: If the software program is written in C++, its project must be created using Microsoft Visual Studio 2017. If the software program is written in Java, its project must be created using NetBeans v8.2. Algorithm: If...

  • Please complete the following task: Create a C++ hash table program for generating a hash from a string. Create a hash f...

    Please complete the following task: Create a C++ hash table program for generating a hash from a string. Create a hash function then test and evaluate the results. The set of strings are from a "words.txt" file (there are 45,402 words in the file, no repeating words. Map the words to 45,402 buckets using a hash function). Requirements: Create an array of 45,402 integers and initialize each to zero. These will be used to store how many times each index...

  • in c++ Write a program that stores the following data about SJDC Pizza orders in a...

    in c++ Write a program that stores the following data about SJDC Pizza orders in a structure: Customer’s Name Order Number Date Order Placed Cost of Pizza The program should keep an array of 12 of these structures. Each element is for a different order. When the program runs it should ask the user to enter the data for each order. It should then show a table that lists each order’s name, order number, date order placed and pizza price....

  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT