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...

  • 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...

  • 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...

  • 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...

  • Task: Comparing the performance between Linear Probing and Double Hashing. Requirements: Please make sure your program...

    Task: Comparing the performance between Linear Probing and Double Hashing. Requirements: Please make sure your program compiles, otherwise your submission will not be graded and you will receive zero. Point deduction rule: Compile warning: 3 points each. Minor error: such as not meeting the assignment input/output requirement, 5 points each. Major error: examples include, infinite loop, runtime errors, any runtime exception, 15 points each. Any code not compliant to the assignment requirement (e.g., not using List interface and AbstractMap and...

  • This program is in C++ Write a program that validates charge account numbers read in from...

    This program is in C++ Write a program that validates charge account numbers read in from the file Charges.txt following this assignment (A23 Data). Use a selection sort function on the array to sort the numbers then use a binary search to validate the account numbers. Print out the sorted list of account numbers so you can see the valid ones. Prompt the user to enter an account number and the validate that number. If the account number entered is...

  • please write this program in C++(Linux). Also write all the explanation of codes, the comment for...

    please write this program in C++(Linux). Also write all the explanation of codes, the comment for your code. Description You are an avid reader and have decided that you would like to keep track of the books that you read. You will define a structure called book_list that will hold 1. a number associated with each book (int) 2. book title (string), 3. author (string), 4. a description of the book (string), 5. a date when the book was read...

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
ADVERTISEMENT