Pages

Tuesday, 10 March 2020

Chapter 11 // Exercise 16 - Principles & Practice Using C++

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

Chapter 11 // Exercise 16

Write a program to read a file of whitespace-separated numbers and output
them in order (lowest value first), one value per line. Write a value only
once, and if it occurs more than once write the count of it's occurrences on
it's line. For example, 7 5 5 7 3 117 5 should give
3
5 3
7 2
117
This one took me a stupid amount of time. It could've been done quite quickly with some brute force and a lot of if statements but I knew there had to be a better way (that didn't involve using maps). I also didn't want to rely on output trickery to get the data and eventually settled on a custom struct that holds a number and how many times it appears. There is a function that sorts the vector, then removes an extra values whilst increasing the count of the number.

No comments:

Post a Comment