C++ Maps/ Vectors TASK: create 2 empty projects-vector.cpp and map.cpp Part 1: v
ID: 665413 • Letter: C
Question
C++ Maps/ Vectors
TASK: create 2 empty projects-vector.cpp and map.cpp
Part 1:
vector.cpp
-Remove the resizeArray() declaration and function, and replace with the dynamic array with a vector
-Replace the loops using iterators
-Remove the output line that outputs the maximum size of the array
-Remove and unnecessary variables or functions remaining.
PART 2:
map.cpp
-remove the resizeArray() declaration and function, and remove the Word class references.
-replace it with a map to replace the same key-value pair that the Word class used.
-rewrite the loops to use iterators
-remove the output line that outputs the maximum size of the array
-remove and unnecessary variables/functions that remain. (Word class is no longer needed)
**Code should not only be shorter than original assignment, it should be shorter than Part 1**
Main.cpp
Word.cpp
Word.h
#include "word . h" #include stream» #include iostream» #include using namespace std; // Function to increase our dynamic array int resizeArray(Word &array;, int size); // Starting point int main() // File I/o ifstream fin("input.txt"); ofstream fout("output.txt"); // Dynamic array for Words int max = 10; int size = 0; word" array = new Word [max]; // Read in file string wordIn; // While there are more words while(fin >> wordIn) // It's not found by default bool found = false; // For each element in the array, if it exists, increment its count, and mark it as found for(int i = 0; iExplanation / Answer
#include<string>
using namespace std;
//class to store a word object
class Word
{
private:
//Word that was found
string word;
//Number of times a word has been found
int count;
public:
//Default constructor
Word();
//constructor to store the word with it
Word(string theWord);
//Returns the word stored
string getWord();
//Returns the count stored
int getCount();
//increments the counter
void increment();
//sets a new word to the object
void setWord(string theWord);
//sets the count to a value
void setCount(int theCount);
};
--------------------------------------------------------------------------------------------------------
word.h
#include "Word.h"
//default constructor
Word::Word()
{
word = "";
count = 0;
}
//constructor to store the word with it
Word::word(string theWord)
{
word = theWord;
count=0;
}
//Returns the word stored
string Word::getWord(){
return word;
}
//Returns the count stored
int Word::getCount()
{
return count;
}
//Increments the counter
void Word::increment()
{
count++;
}
//sets a new word to be object
void Word::setWord(string theWord)
{
word = theWord;
}
//sets the count to a value
void Word::setCount(int theCount)
{
count = theCount;
}
--------------------------------------------------------------------------------------
#include "Word.h"
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
using namesoace std;
/function to increase our dynamic array
int resizeArray(std::vector<int>* &array, int size);
//starting point
int main()
{
//File I/o
ifstream fin("input.txt");
ofstream fout("output.txt");
//Dynamic array of words
int max = 10;
int size = 0;
std::vector<int> *array;
array = new std::vector<int>[max];
//Read in file
string wordIn;
//while(fin >> wordIn)
{
//It's not found by defualt
bool found = false;
//For each element in the array
for(int i=0; i<size;i++)
{
if(array[i].getWord() == wordIn)
{
array[i].increment();
found = true;
//quit for loop early
break;
}
}
//If not found
if(!found)
{
if(size == max)
{
max = resizeArray(array, size);
}
array[size].setWord(wordIn);
array[size].increment();
size++;
}
}
//Print list of words and counts
fout << "Words found: "<<size<<endl;
fout <<"array's max size "<<max<<endl;
for(int i=0;i<size;i++)
{
fout << array[i].getWord() << " - "<<array[i].getCount()<<endl;
}
}
//returns new size of array
int resizeArray(Word* &arr, int oldSize){
int newSize = oldSize * 2;
std::vector<int> *newArray;
newArray = new std::vector<int>[newSize];
//Deep copy each element for(int i=0;i<oldSize; i++){
newArray[i].setCount(arr[i].getCount());
newArray[i].setWord(arr[i].getWord());
}
delete [] arr;
return newSize;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.