Create a Class ArrayListClass in C++ - Create an array class that has an array o
ID: 3681085 • 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...
txt file located here: http://s000.tinyupload.com/index.php?file_id=00832161967479055564
or here: https://www.dropbox.com/s/nfureyg7ex1ar8u/260%20Names.txt?dl=0
- Read in the file one name at a time, store/add each Name in the correct index LIST element.
- Search for a Name to see if it in the LIST class.
- Print out the ALL names found on each list.
Thank you very much. I appreciate your time, and I have been trying to code this for the last 6 hours. Greatly appreciate it.
Explanation / Answer
#include <iostream>
#include <list>
#include <string>
#include <fstream>
#include <algorithm>
using namespace std;
class ArrayListClass {
public:
ArrayListClass();
void printAll();
void searchName(string name);
private:
list<string> myarray[27];
};
ArrayListClass::ArrayListClass() {
string line;
ifstream file ("names.txt");
if (file.is_open()) {
while (getline(file, line)) {
char start = line[0];
int index = start - 64;
myarray[index].push_back(line);
}
}
else {
cout << "Could not open file" << endl;
}
}
void ArrayListClass::printAll() {
for (int i = 1; i < 27; i++) {
cout << "Printing words that start with " << char(i-1 + 'A') << ' ';
list<string>::iterator iter = myarray[i].begin();
while (iter != myarray[i].end())
{
cout << ' ' << *iter << ' ';
iter++;
}
}
}
void ArrayListClass::searchName(string name) {
int index = name[0] - 64;
list<string>::iterator iter = find(myarray[index].begin(), myarray[index].end(), name);
if (iter != myarray[index].end())
cout << "Name " << name << " is present ";
else
cout << "Name "<< name << " is absent ";
}
int main() {
ArrayListClass myClass;
myClass.printAll();
myClass.searchName("ZUK");
myClass.searchName("RAKA");
}