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.

Monday, 9 March 2020

Chapter 11 // Exercise 15 - 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 15

 Write a program that reads a file of whitespace-separated numbers and outputs
 a file of numbers using scientific format and precision 8 in four fields of 
 20 characters per line.
.

Sunday, 8 March 2020

Chapter 11 // Exercise 14 - 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 14

 Write a program that reads a text file and writes out how many characters of each character classification are in the file.
.

Saturday, 7 March 2020

Chapter 11 // Exercise 13 - 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 13

Reverse the order of words (defined as whitespace-separated strings) in a file. For example, Norwegian Blue Parrot becomes parrot Blue Norwegian. You are allowed to assume that all the strings from the file will fit into memory at once.
.

Friday, 6 March 2020

Chapter 11 // Exercise 12 - 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 12

Reverse the order of characters in a text file. For example, asdfghjkl becomes
 lkjhgfdsa. Warning: There is no really good, portable, and efficient way of
 reading a file backward.
This one was pretty simple. I did originally do it with just an fstream as we're reading and writing the same file however, it appears an fstream, by default, appends new text rather than overwrite. I tried using ios::trunc to make it overwrite but it didn't work so I just switched to if/ostream.