Quote:
i didn't get your question,sorry.
Timing is meaningless if you include user input in the timing.
Move the beginning of timing after any user input, and avoid output during timing.
Quote:
How can I reduce the computational time taken by my code ?
Your code is simple minded, but it is at expense of running time.
Instead of using the array
words
which have an access time linear with number of words, use should use a
map
.
map - C++ Reference[
^]
std::map : The C++ dictionary class – Modern C++[
^]
Suppose you have a list of 1000 words
In an array, to know if a word exist, you check the 1000 words.
In a map container, the words are sorted alphabetically and the container uses a tree search or dichotomy, this means that you know if a word exist in list with only 10 search.
Because 2^10= 1024 > 1000
Your algorithm is: (say there is 1000 words)
for each input word build list of words
for each word in list for each previous words Check if word already counted if not counted
for each remaining words count this word and print word and count
Algorithm with map container:
for each input word copy word in temp variable
check if word exist
if not exist
insert new word in map add 1 to count of that word for each word in map print word and count