Showing posts with label chapter 11 exercise. Show all posts
Showing posts with label chapter 11 exercise. Show all posts

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.

Thursday, 5 March 2020

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

Write a function vector<string> split(const string& s, const string& w) that
returns a vector of whitespace-separated substrings from the argument s, where
whitespace is defined as "ordinary whitespace" plus the characters in w.
Again, on this one I'm assuming that he means add a break at whitespace and the string w? Eg if w is "break" and the line "I like to eat donuts on my break everyday" is entered; the following would be entered in the vector:
[0] I
[1]
[2] like
[3]
[4] to
[5]
[6] eat
[7]
[8] donuts
[9]
[10] on
[11]
[12] my
[13]
[14] everyday




Wednesday, 4 March 2020

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

 Write a function vector<string> split(const string& s) that returns a vector of whitespace-separated substrings from the argument s.
On this one I'm assuming he just meant "read in the entire thing push back single words and spaces". So that's what I did. It's a vector that goes "word", " ", "word", " ", etc.

Tuesday, 3 March 2020

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

Split the binary I/O program from section 11.3.2 into two; one program that
converts an ordinary text file into binary and one program that reads binary
and converts it to text. Test these programs by comparing a text file with
what you get by converting it to binary and back.
When just running the code given in the book on a random file, I noticed that when reading it out to a new file, it cut off the last two characters. They weren't special ones just 'e' and '.'...After staring at the code and the book for a good 15 minutes I realised that I truly am an idiot because the program in the book is designed to read in integers...

After changing int to char it worked as expected, however I'm now wondering what was so different about 'e' compared to the other letters.




Monday, 2 March 2020

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

         Use the program from the previous exercise to make a dictionary (as an alternative to the approach in section 11.7). Run the result on a multi-page text file, look at the result, and see if you can improve the program to make a better dictionary.
Make a dictionary? Is this guy having a giraffe? Also what is a multi-page text file? A txt document doesn't have pages. Also, having read over section 17 again I think he just wants us to run the previous program on a text file and remove all punctuation and contractions, whilst displaying each word on a new line. I can do that easy however that's not a dictionary. A dictionary has definitions of what each word means.

Also, most dictionaries allow some forms of contractions; but they do tend to differ on what is allowed depending on the country. The same applies to hyphenated words as some are common enough to be included in dictionaries. So I decided to allow contractions, hyphens and remove any other form of punctuation and numbers and then output each word on a separate line after being sorted. I started getting a bit pedantic about things as well like removing duplicated words.

Sunday, 1 March 2020

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

Modify the program from the previous exercise so that it replaces don't with
do not, can't with cannot, etc.; leaves hyphens within words intact (so that
we get " do not use the as-if rule "); and converts all characters to lower 
case.
Eurgh. He just had to say 'etc'  on the contraction words. There are 17 common words that use the 't contraction and only one of them doesn't have a space (cannot). I'm super lazy, so there is no way I'm checking to see if the second to last character is a single quote and then checking if that word is equal to 1 of 17 contractions. 

I ended up putting the contractions and their counterparts into a text file, then reading them into vectors at the start of the program. When a user comes along and then inputs 'don't' it will then just compare the string against the vector of contractions, if there is a match, the index will be used to return the matching 'normal phrase'. You can find the contractions.txt in the main section of Chapter 11 on the git.

That wasn't even the hardest part. I spent the longest ensuring that hyphens were only removed when not wrapped in quotes and were by themselves.

Saturday, 29 February 2020

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

 Write a program that replaces punctuation with whitespace. Consider .(dot),
 ; (semicolon), ,(comma), ? (question mark), - (dash), ' (single quote)
 punctuation characters. Don't modify characters within a pair of double
 quotes ("). For example "-don't use the as-if rule." becomes " don t use the
 as if rule ".
I'm sure there's a more elegant way to do this than if's and counting quotes but it isn't too ugly to I'll allow it.

Friday, 28 February 2020

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

 Write a program that reads strings and for each string outputs the character
 classification of each character, as defined by the character classification
functions presented in section 11.6. Note that a character can have several
 classifications (e.g, x is both a letter and an alphanumeric).


This was suspiciously easy and it makes me wonder if I did it correctly.

Thursday, 27 February 2020

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

Write a program called multi_input.cpp that prompts the user to enter several
integers in any combination of octal, decimal, or hexadecimal, using the 0
and 0x base suffixes; interpret the numbers correctly; and converts them to
decimal form. Then your program should output the values in properly spaced
columns like this:
0x43 hexadecimal converts to 67 decimal
0123 octal converts to 83 decimal
  65         decimal converts to 65 decimal


The useful item I learnt from this exercise is that stoi() takes in 3 parameters, the first is the string, the second is a size_t pointer (used for finding the next value) and last is a number which indicates the base. I noticed that when you use stoi() on an int, the base is default set to 10 (deicmal), therefore it cut off the 0 and 0x, even if the cout stream was set to oct and hex. To convert the number correctly, set the base to 0 as this will check for all 3 bases.

If you just want to check for oct, set the base to 8 and hex to 16. Other than that, the hardest part was getting it to output with all the fields lined up. I really hate that setw().

Wednesday, 26 February 2020

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

Write a program that removes all vowels from a file ("disemvowels"). For example, Once upon a time! becomes nc pn tm!. Surprisingly often, the result is still readable; try it on your friends. 


Once again the find functions on a string come to the rescue. I think I'm becoming a bit addicted to using find_first_not_of and find_first_of; they're just so god damn handy. I know these functions haven't been introduced in the book yet but this isn't prior knowledge I already had. I only learnt about them when googling how to solve problems like "find a character in a string" so I don't think it's cheating. Also rdbuf() and stringstreams are definitely ones I'll have to remember.

Tuesday, 25 February 2020

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

Write a program that given a file name and a word outputs each line that contains that word together with the line number. Hint: getline().


.