C++: This software stores all the data in array . Each person has SSN and name i
ID: 3732030 • Letter: C
Question
C++:
This software stores all the data in array. Each person has SSN and name information. This information is provided in separated files.
This is the data of a sample file:
i 586412373 NICOLA EVANGELISTA
i 177228167 MEAGAN LEKBERG
i 586412373 JEFF DUTTER
i 760846483 KITTY MANZANERO
i 061899135 CATHERIN MCCREIGHT
i 087300880 CARMA KULHANEK
i 177264549 VALERY KOSAKOWSKI
i 210044984 SHEILAH MONGES
d 760846483 KITTY MANZANERO
r 760846483 KITTY MANZANERO
r 007980295 DELPHIA SIMISON
i 493515916 VERONIKA TADENA
d 401991909 MCKINLEY WESTERFELD
i 793267575 TEMIKA MESHEW
i 319373939 MARGIT EBLIN
AN EXAMPLE OF RUNNING PROGRAM
ashk ./a.out 15-idr
The Number of Valid Insertation :10
The Number of Valid Deletion: 1
The Number of Valid Retrival: 0
item numbers in the Array Size is :9
Array Size is :500
In above file, each row represents one person’s information. The leading character is either “i”, “d”, or “r”. The second column is the SSN of the person, the following string are the first name and last name.
The character “r” means retrieval. If there is a match with the given SSN, increase the retrieval counter by one. In the above example, there are two “r” rows. The first one has a match, the second one does not have match.
In this project, we store the data in the array. Here are several requirements for the array:
-The initial size of the array is 1000.
-When you add a new entry to the array, if the array is not full, add the entry. If the array is full, creates a new array with doubled size. Copy the data from the old array to the new array. Release the old array.
-When you delete an entry from the array. If the number of entry is less than 14 of the array size. Creates a new array with half size of the current array. Copy the data from the old array to the new array, then release the old array.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CREATE THE RETRIEVAL METHOD:
retrieval(): Checks whether the given SSN and name matches entry in the array. If there is a match, increase the retrieval count by one.
CODE GIVEN:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
struct PERSON{
int SSN;
string firstName;
string lastName;
};
int insertionCount = 0;
int size = 1000;
int deletionCount;
/* Inserts data to the end of the array. If the provided data
has duplicated SSN with an existing entry, the software will discard the
provided data. Otherwise, increase the insertion counter by one. */
PERSON* insert(PERSON array[], int personSSN, string fName, string lName){
for(int i = 0; i < insertionCount; i++){
if(array[i].SSN == personSSN){
return array; //if it is duplicate SSN, the array is returned without inserting.
}
}
if(insertionCount >= size){ //if this condition is true, then
size = size * 2;
PERSON *newArray = new PERSON[size];//new array of person is created with double of previous size
for(int i = 0; i < insertionCount; i++){
newArray[i] = array[i];//new array is populated with the old array values.
}
delete[] array; //deletes the array, previous array
//current details are inserted
newArray[insertionCount].SSN = personSSN;
newArray[insertionCount].firstName = fName;
newArray[insertionCount].lastName = lName;
insertionCount++;
return newArray;
}
else{
array[insertionCount].SSN = personSSN;
array[insertionCount].firstName = fName;
array[insertionCount].lastName = lName;
insertionCount++;
return array;
}
}
/* The software deletes the entry with given SSN and name from the array.
It is possible that provided SSN does not match any record in the array, if
that is the case, the software ignore this deletion request. If there is a
match in the array, delete the record from the array, increase the delete counter by
one. */
PERSON *deletion(PERSON array[], int ssn, string fname, string lname){
int count = insertionCount - deletionCount;
for (int i = 0; i < count; i++){
if (array[i].SSN == ssn){
for (int j = i; j <count - 1; j++){
array[j].SSN = array[j + 1].SSN;
array[j].firstName = array[j+1].firstName;
array[j].lastName = array[j+1].lastName;
}
deletionCount++;
count = insertionCount - deletionCount;
if (count < size/4){
PERSON *newArray = new PERSON[size/4];
for (int i = 0; i<count; i++){
newArray[i].SSN = array[i].SSN;
newArray[i].firstName = array[i].firstName;
newArray[i].lastName = array[i].lastName;
}
size = size/4;
return newArray;
}
else {
return array;
}
}
}
return array;
}
int main(int argc, const char * argv[]) {
ifstream input(argv[1]);
string fName, lName;
int ssn;
PERSON *array = new PERSON[size];
while(!input.eof()){
char operation; // this character reads the character from the file 'i' 'd' 'r'
input >> operation >> ssn >> fName >> lName;
if(operation == 'i'){
array = insert(array, ssn, fName, lName);
}else if(operation == 'd'){
array = deletion(array, ssn, fName, lName);
}
}
input.close();
cout << "The Number of Valid Insertation :" << insertionCount << endl;
cout << "The Number of Valid Deletion :" << deletionCount << endl;
cout << "The Number of Valid Retrieval :" << endl;
cout << "Item numbers in the array :" << insertionCount - 1 << endl;
cout << "Array Size is :" << size << endl;
return 0;
}
Explanation / Answer
main.cpp
#include <iostream>
#include <fstream>
#include <cstdio>
#include <ctime>
using namespace std;
// Declare a struct containing personal info
struct Person{
string ssn;
string fullName;
};
// Check to see if array needs to be resized
void checkArraySize(struct Person *&ledger, int &ledgerSize, char option) {
switch(option) {
// Check if array needs to be increased
case 'i': {
if (ledger[ledgerSize-1].ssn.length() != 0) { // If last element in array contains an object
struct Person *newLedger = new Person[ledgerSize*2]; // Create a new temp array with double the size
// Transfer data from the old array to the new larger array
for (int x = 0; x < ledgerSize; x++) {
newLedger[x] = ledger[x];
}
delete [] ledger; // Release the old array
ledger = newLedger; // Init the old array with the new larger array
ledgerSize *= 2; // Update the array size variable
}
break;
}
// Check if the array needs to be decreased
case 'd': {
if (ledger[(ledgerSize/4)-1].ssn.length() == 0) { // If the array is less than 1/4 fullName
struct Person *newLedger = new Person[ledgerSize/2]; // Create a new temp array with half the size
// Transfer data from the old array to the new smaller array
for (int x = 0; x < ledgerSize; x++) {
if (ledger[x].ssn.length() == 0) {break;}
newLedger[x] = ledger[x];
}
delete [] ledger; // Release the old array
ledger = newLedger; // Init the old array with the new smaller array
ledgerSize /= 2; // Update the array size variable
}
break;
}
default : {break;}
}
}
void insertInfo(struct Person *&ledger, int &ledgerSize, fstream &input, int &index, int &insertCount) {
// Declare variables
string firstName, lastName;
string ssnX;
string skipInput;
input >> ssnX; // Input the ssn of the entry that needs to be inserted into the array
checkArraySize(ledger, ledgerSize, 'i'); // Check to see if the array is full before attempting to insert a new entry into the array
// Iterate through the ledger
for (int x = 0; x < ledgerSize; x++) {
if (ssnX == ledger[x].ssn) { // Check if the ssn is already in the array
input >> skipInput;
input >> skipInput;
break;
} else if (ledger[x].ssn.length() == 0) {
// If that ssn is NOT in the array then add that entry
input >> firstName >> lastName;
ledger[index].ssn = ssnX;
ledger[index].fullName = firstName + " " + lastName;
// Update counter variables
index++;
insertCount++;
break;
}
}
}
void deleteInfo(struct Person *&ledger, int &ledgerSize, fstream &input, int &index, int &deleteCount) {
// Declare variables
string ssnX;
string skipInput;
input >> ssnX; // Input the ssn of the entry that needs to be deleted from the array
// Iterate through the array
for (int x = 0; x < ledgerSize; x++) {
if (ledger[x].ssn.length() != 0) { // Check if element is not empty
if (ssnX == ledger[x].ssn) { // Check if the inputed ssn matches any entries
// Starting at the index of the entry to be deleted
// shift all of the entries 1 to the left
// which overwrites the entry to be deleted
while (x < ledgerSize && ledger[x].ssn.length() != 0) {
if (x == ledgerSize-1) { // Bounds check, assign last element to ""
ledger[x].ssn = "";
ledger[x].fullName = "";
} else {
ledger[x].ssn = ledger[x+1].ssn;
ledger[x].fullName = ledger[x+1].fullName;
}
x++;
}
checkArraySize(ledger, ledgerSize, 'd'); // Check to see if the array is more than 3/4 empty and should be downsized
// Update counter variables
deleteCount++;
index = x-1;
break;
}
} else {break;}
}
input >> skipInput;
input >> skipInput;
}
void retrieveInfo(struct Person *&ledger, int &ledgerSize, fstream &input, int &retrieveCount) {
// Declare variables
string ssnX;
string skipInput;
input >> ssnX; // Input ssn of entry to be retrieved
// Iterate through the array
for (int x = 0; x < ledgerSize; x++) {
if (ssnX == ledger[x].ssn) { // Check if the ssn matches an entry in the array
retrieveCount++; // Update the counter
} else if (ledger[x].ssn.length() == 0) { break;}
}
input >> skipInput;
input >> skipInput;
}
void printLedger(const struct Person *ledger, const int ledgerSize, const int insertCount, const int deleteCount, const int retrieveCount) {
cout << "The Number of Valid Insertation: " << insertCount << endl;
cout << "The Number of Valid Deletion: " << deleteCount << endl;
cout << "The Number of Valid Retrieval: " << retrieveCount << endl;
cout << "Item numbers in the array: " << (insertCount - deleteCount) << endl;
cout << "Array Size is: " << ledgerSize << endl;
/*cout << endl;
cout << "Index SSN Name" << endl;
for (int x = 0; x < ledgerSize; x++) {
if (ledger[x].ssn.length() != 0) {
cout << x << " " << ledger[x].ssn << " " << ledger[x].fullName << endl;
} else {
break;
}
}*/
}
int main(int argc, char *argv[]) {
// Init and start timer
clock_t start, end;
start = clock();
// Declare and init the entry array
int recordSize = 1000;
struct Person *record = new Person[recordSize];
// Declare variables
double duration;
int index = 0;
int insertionCount = 0;
int deletionCount = 0;
int retrievalCount = 0;
string option; // Possible values 'i', 'd', 'r'
string ssnX; // SSN to search for
string firstName, lastName;
string skipInput; // Variable to skip over undesired input
// Create input stream from file
fstream input(argv[1]);
// Iterate through the file
while (!input.eof()) {
// Take in input from the file and assign it to the record
input >> option;
switch (option[0]) {
// Insert entry
case 'i': {
insertInfo(record, recordSize, input, index, insertionCount);
break;
}
// Delete entry
case 'd': {
deleteInfo(record, recordSize, input, index, deletionCount);
break;
}
// Retrieve entry
case 'r': {
retrieveInfo(record, recordSize, input, retrievalCount);
break;
}
default :{
input >> skipInput;
input >> skipInput;
input >> skipInput;
break;
}
}
option = "";
firstName = "";
lastName = "";
if (!input) break; // Break at the end of the file
}
input.close(); // Close the text file
// Print out program results
printLedger(record, recordSize, insertionCount, deletionCount, retrievalCount);
end = clock();
duration = ( end - start ) / (double) CLOCKS_PER_SEC;
cout << "Time elapsed: " << duration << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.