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

Create a Class ArrayListClass in C++ Create an array class that has an array of

ID: 3681062 • Letter: C

Question

Create a Class ArrayListClass in C++

Create an array class that has an array of 27 List Classes

Ignore index 0... Indexes 1...26 correspond to the first letter of a Last name.

Again - ignore index 0. index 1 is A, index 2 is B, index 3 is C... index 26 is z.

Create a file of 260 random ordered last names... 10 each for each letter...

Read in the file one name at a time, store/add each Name in the correct index LIST element.

( See File I/O lecture - this week if you need a review of File I/O)

Search for a Name to see if it in the LIST class.

Print out the ALL names found on each list..

Explanation / Answer

file ArrayList.h

#ifndef ARRAYLISTH
#define ARRAYLISTH

#include <iostream>

using namespace std;

class ArrayList {

public:
// The size we use to increment arrays. I use a small increment for testing.
const static int ArrayIncrement = 2;

// Constructor to initialize the list.
ArrayList();

// Destructor to clean up the list.
~ArrayList();

// Empties out the array list structure.
void MakeEmpty( );

// Tests to see if the array list structure is empty.
bool isEmpty( );

// Returns the size of the array.
int listSize() { return nelts; }

// Finds a specifies element of the array list. Returns -1 if not found.
int Find( std::string y );

// Inserts an element at a specified position in the array list.
void Insert( std::string y, int position );

// Deletes a specified element in the array list. ASSIGNED AS LAB
void Delete( std::string y );

// Displays the list. This is a good debugging aid
void PrintList( );

private:

int ArraySize;// Size of the array.
int nelts; // Number of elements in the array.
std::string *array;// Pointer to the array.
  
// Makes sure that there is space for a new element in the list.
void MakeSpace( );
};

#endif

file ArrayList.cpp

// Functions to support list operations on an array.

#include "ArrayList.h"
#include <iostream>
#include <stdlib.h>
#include <cstring>
#include <fstream>

using namespace std;

// Contructor sets valriables to indicate that we have an empty list.
ArrayList::ArrayList() {

ArraySize = 0;
nelts = 0;
array = NULL;
}
// Destructor used to clean up allocated resources/
ArrayList::~ArrayList() {

delete [] array;
}
// Delete all the elements from the list.
void ArrayList::MakeEmpty( )
{
delete [] array;
nelts = 0;
ArraySize = 0;
array = NULL;
}
// Tests to see if there are no elements in the array.
bool ArrayList::isEmpty( )
{
return nelts == 0;
}
// Return the index of the first occurrance of a specified list element. Returns -1 if not found.
int ArrayList::Find( std::string y )
{
for( int i = 0; i < nelts; i++ ) {
if( array[i] == y ) {

// The user is considering the first element to be at position 1.
return i +1;
}
}
return -1;
}
// Insert the specied number in the list position.
void ArrayList::Insert(std::string y, int position )
{
// Abort the program if a bad position is specified.
assert( position >= 1 && position <= nelts + 1 );

// Note the array position. Recall that lists are numbered starting with 1.
int ia = position - 1;

// Make sure there is space in the array for a new element.
MakeSpace( );

// If we are adding a new element to the list, open up space if necessary.
if( ia < nelts ) {
memmove( &array[ia + 1], &array[ia], (nelts - ia) * sizeof(std::string ) );
}
array[ia] = y;
nelts++;
}
// Display the elements of the list.
void ArrayList::PrintList( )
{
// Display the list.
for( int i = 0; i < nelts; i++ ) {
cout << i + 1<< ". " << array[i] << endl;
}
}
void ArrayList::MakeSpace( )
{
// If there is space for another array element, return.
if( nelts < ArraySize ) return;

// Create storage to make a larger array.
std::string *tmp;
tmp = new std::string[ArraySize + ArrayIncrement];

// Note that exceptions now occur when new fails unless you tell the compiler otherwise.
assert( tmp != NULL );

// If the previous array was not empty, must copy its contents.
if( nelts > 0 ) {
memcpy( tmp, array, nelts * sizeof(std::string) );
delete [] array;
}
ArraySize += ArrayIncrement;
array = tmp;
}

int main( )
{
ArrayList aaa;
// Initialize the list.
cout << "Array size = " << aaa.listSize() << endl;
  
string line;
  
ifstream myfile ("lastname.txt");
if (myfile.is_open()){
while ( getline (myfile,line) ){
//cout << line << ' ';
aaa.Insert(line, (int)(line[0]-65)); // code ascii A = 65 +1 ignore pos 0
}
myfile.close();
}else
cout << "Unable to open file";
  
aaa.PrintList( );

if (aaa.Find("PEREZ")>-1)
cout << "Find";
  
system("pause");
}