You are to write a C++ program to set up a data base for a phone index. The key
ID: 653911 • Letter: Y
Question
You are to write a C++ 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 an input line.
Set your hash table up to be 31 entries, hash table is 31 in size.
Print out the final data base (hash table) and indicate how many collisions were made in building the data base. The contents of the input file needed for this program are given below.
Contents of "HashNamesAndPhone.txt" input file:
Taylor marie 939-1512
james wilis thomas 261-8342
Stone Rock 544-2372
lea high lee 266-8324
stephens reynolds 696-9231
mack russell 123-1234
Lewis Michelle Tee 828-2148
john marshall 888-2891
Moto hasey 823-8000
oe vey 177-1423
Twoseeor knocksee 823-8321
Legg T. 587-2839
Hofstra M. 601-3225
Malioneyh P. J. 287-4344
Morier G. E. 544-2319
Hauser M. 606-2940
Currie W. D. 701-4281
Hoos R. Dadie 818-3821
Smelly Tau 707-7281
Tobe Cir 613-2414
Tyosn Chicken 602-3152
TooB OrNot 283-5214
SixOne Other 843-2343
Big Tow 384-5624
Zevent Heaven 834-2563
Mighty Mouse 222-2222
G P Morier 832-4562
Explanation / Answer
If you can go with a database method, please got horgh the below things , it may help you more than with using .txt file
=======
Following C code segment shows how we can fetch and display records from our COMPANY table created in above example:
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
static int callback(void *data, int argc, char **argv, char **azColName){
int i;
fprintf(stderr, "%s: ", (const char*)data);
for(i=0; i<argc; i++){
printf("%s = %s ", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf(" ");
return 0;
}
int main(int argc, char* argv[])
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
char *sql;
const char* data = "Callback function called";
/* Open database */
rc = sqlite3_open("test.db", &db);
if( rc ){
fprintf(stderr, "Can't open database: %s ", sqlite3_errmsg(db));
exit(0);
}else{
fprintf(stderr, "Opened database successfully ");
}
/* Create SQL statement */
sql = "SELECT * from COMPANY";
/* Execute SQL statement */
rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);
if( rc != SQLITE_OK ){
fprintf(stderr, "SQL error: %s ", zErrMsg);
sqlite3_free(zErrMsg);
}else{
fprintf(stdout, "Operation done successfully ");
}
sqlite3_close(db);
return 0;
}
When above program is compiled and executed, it will produce the following result:
Opened database successfully
Callback function called: ID = 1
NAME = Paul
AGE = 32
ADDRESS = California
SALARY = 20000.0
Callback function called: ID = 2
NAME = Allen
AGE = 25
ADDRESS = Texas
SALARY = 15000.0
Callback function called: ID = 3
NAME = Teddy
AGE = 23
ADDRESS = Norway
SALARY = 20000.0
Callback function called: ID = 4
NAME = Mark
AGE = 25
ADDRESS = Rich-Mond
SALARY = 65000.0
Operation done successfully
================================================
Create a Table
Following C code segment will be used to create a table in previously created database:
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
static int callback(void *NotUsed, int argc, char **argv, char **azColName){
int i;
for(i=0; i<argc; i++){
printf("%s = %s ", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf(" ");
return 0;
}
int main(int argc, char* argv[])
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
char *sql;
/* Open database */
rc = sqlite3_open("test.db", &db);
if( rc ){
fprintf(stderr, "Can't open database: %s ", sqlite3_errmsg(db));
exit(0);
}else{
fprintf(stdout, "Opened database successfully ");
}
/* Create SQL statement */
sql = "CREATE TABLE COMPANY("
"ID INT PRIMARY KEY NOT NULL,"
"NAME TEXT NOT NULL,"
"AGE INT NOT NULL,"
"ADDRESS CHAR(50),"
"SALARY REAL );";
/* Execute SQL statement */
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
if( rc != SQLITE_OK ){
fprintf(stderr, "SQL error: %s ", zErrMsg);
sqlite3_free(zErrMsg);
}else{
fprintf(stdout, "Table created successfully ");
}
sqlite3_close(db);
return 0;
}
When above program is compiled and executed, it will create COMPANY table in your test.db and final listing of the file will be as follows:
-rwxr-xr-x. 1 root root 9567 May 8 02:31 a.out
-rw-r--r--. 1 root root 1207 May 8 02:31 test.c
-rw-r--r--. 1 root root 3072 May 8 02:31 test.db
INSERT Operation
Following C code segment shows how we can create records in our COMPANY table created in above example:
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
static int callback(void *NotUsed, int argc, char **argv, char **azColName){
int i;
for(i=0; i<argc; i++){
printf("%s = %s ", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf(" ");
return 0;
}
int main(int argc, char* argv[])
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
char *sql;
/* Open database */
rc = sqlite3_open("test.db", &db);
if( rc ){
fprintf(stderr, "Can't open database: %s ", sqlite3_errmsg(db));
exit(0);
}else{
fprintf(stderr, "Opened database successfully ");
}
/* Create SQL statement */
sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "
"VALUES (1, 'Paul', 32, 'California', 20000.00 ); "
"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "
"VALUES (2, 'Allen', 25, 'Texas', 15000.00 ); "
"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)"
"VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );"
"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)"
"VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );";
/* Execute SQL statement */
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
if( rc != SQLITE_OK ){
fprintf(stderr, "SQL error: %s ", zErrMsg);
sqlite3_free(zErrMsg);
}else{
fprintf(stdout, "Records created successfully ");
}
sqlite3_close(db);
return 0;
}
===================================
Please use the above example source code for developing the database setup in phone index.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.