C programming Write a program that uses dynamic arrays Creat
C++ programming
Write a program that uses dynamic arrays.
Create a list of items (user supplies)
Sort the list into a new array, eliminate duplicates
test case:
cat
giraffe
gorilla
cat
dog
lion
tiger
giraffe
Solution
#include <iostream>
#include <list>
#include <fstream>
#include <algorithm>
using namespace std;
int main(){
ifstream in(\"input.txt\");
list<string> myList;
string *dynamicArray;
string str;
while( in >> str ){
myList.push_back( str );
}
//create new array
int size = myList.size();
dynamicArray = new string[size];
//sort the list in new array
//sort the list
myList.sort();
//remove duplicates and add in new array
int index = 0;
list<string>::iterator it = myList.begin();
string last = *it;
dynamicArray[ index++ ] = *it;
it++;
for(; it != myList.end(); it++ ){
if( *it != last ){
dynamicArray[ index++ ] = *it;
}
last = *it;
}
//print the final array
for(int i = 0; i < index; i++){
cout << dynamicArray[i] << endl;
}
}

