Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++: This software stores all the data in array . Each person has SSN and name i

ID: 3732071 • 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 INSIDE THE CODE:

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

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#define TRUE 1
#define FALSE 0

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;
}

/* The software retrieves the entry with given SSN and name from the array.
It is possible that provided SSN and name does not match any record in the array, if
that is the case, the software ignore this retrieval request. If there is a
match in the array, increase the retrieval counter by one. */

int retrieval(PERSON array[], int ssn, string fname, string lname, int retrieveCount){
int count = insertionCount - deletionCount;
for (int i = 0; i < count; i++){
  if ((array[i].SSN == ssn) && !fname.compare(array[i].firstName) && !lname.compare(array[i].lastName)){

   return retrieveCount + 1;
  }
}
return retrieveCount;
}

int main(int argc, const char * argv[]) {
ifstream input(argv[1]);
string fName, lName;
int ssn;
int retrievalCount = 0; //To keep retrieval counter, initialize to zero

PERSON *array = new PERSON[size];
deletionCount = 0;  //Declared Global since it need to be accessed in 2 functions(deletion and retrieval, initialize to zero

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);
  }
  /* if operation is 'r' then retrieval function must be called with current retrieval value as parameter
  and returned value as updated retrieval value*/
  else if (operation == 'r'){
   retrievalCount = retrieval(array, ssn, fName, lName,retrievalCount);
  }
}

input.close();
cout << "The Number of Valid Insertation :" << insertionCount << endl;
cout << "The Number of Valid Deletion :" << deletionCount << endl;
cout << "The Number of Valid Retrieval :" << retrievalCount << endl;
cout << "Item numbers in the array :" << insertionCount - 1 << endl;
cout << "Array Size is :" << size << endl;

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote