# 15.14. Coding Practice¶

Data file: poem.txt
Two roads diverged in a yellow wood,
And sorry I could not travel both
And be one traveler, long I stood
And looked down one as far as I could
To where it bent in the undergrowth;
Then took the other, as just as fair,
And having perhaps the better claim
Because it was grassy and wanted wear,
Though as for that the passing there
And both that morning equally lay
In leaves no step had trodden black.
Oh, I kept the first for another day!
Yet knowing how way leads on to way
I doubted if I should ever come back.
I shall be telling this with a sigh
Somewhere ages and ages hence:
Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference


Write a program that takes in an input file called poem.txt and prints the first 5 lines to the terminal. Include proper file error checking.

Below are the contents of the input file.

Two roads diverged in a yellow wood,
And sorry I could not travel both
And be one traveler, long I stood
And looked down one as far as I could
To where it bent in the undergrowth;
Then took the other, as just as fair,
And having perhaps the better claim
Because it was grassy and wanted wear,
Though as for that the passing there
And both that morning equally lay
In leaves no step had trodden black.
Oh, I kept the first for another day!
Yet knowing how way leads on to way
I doubted if I should ever come back.
I shall be telling this with a sigh
Somewhere ages and ages hence:
Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference.


Below is one way to implement this program. We create an ifstream object to open our file. We check to make sure the file is opened correctly before we use getline in a for loop to retrieve and print the first 5 lines of the poem.

Data file: speech.txt
We choose to go to the Moon. We choose to go to the Moon...
We choose to go to the Moon in this decade and do the other things,
not because they are easy, but because they are hard; because that goal
will serve to organize and measure the best of our energies and skills,
because that challenge is one that we are willing to accept, one we are
unwilling to postpone, and one we intend to win, and the others, too


Selecting from: cp_15_AC_2q, cp_15_AC_2q_pp

Data file: heights.txt
62	67	75	68	65
67	70	72	74	66
72	66	66	73	69
61	60	73	72	6


Write a program that takes in an input file called heights.txt, finds the median of the data, and prints “The median height is: height inches” to the terminal. Include proper file error checking.

Below are the contents of the input file.

62  67      75      68      65
67  70      72      74      66
72  66      66      73      69
61  60      73      72      60


Below is one way to implement this program. We create an ifstream object to open our file. We check to make sure the file is opened correctly before we read the data values into a vector. After sorting the vector, we find the median depending on whether the number of data values was even or odd. Finally, we output our result to the terminal.

Data file: powers.txt
Student output fil


Selecting from: cp_15_AC_4q, cp_15_AC_4q_pp

Data file: message.txt
Can you encrypt this message and decrypt the message below?
Pbatenghyngvbaf! Lbh'ir qrpelcgrq guvf zrffntr


ROT13 is a simple Caesar cipher that replaces each letter in a string with the 13th letter after it in the alphabet. For example, using ROT13 on the letter “a” would turn it into “n”. Notice how since 13 is exactly half the number of characters in the alphabet, using ROT13 on the letter “n” would turn it into “a”. Thus, ROT13 can be used to encrypt and decrypt messages. Write a program that takes in an input file called message.txt, applies ROT13, and outputs the result to the terminal. Include proper file error checking.

Below are the contents of the input file.

Can you encrypt this message and decrypt the message below?
Pbatenghyngvbaf! Lbh'ir qrpelcgrq guvf zrffntr.


Below is one way to implement this program. We create an ifstream object to open our file. We check to make sure the file is opened correctly before we read the data values into a string. We call our ROT13 function and output the result to the output file.

Data file: dream.txt
Have you ever had a dream that you,
um, you had, your, you- you could,
you’ll do, you- you wants, you, you
could do so, you- you’ll do, you could-
you, you want, you want them to do you
so much you could do anything


Selecting from: cp_15_AC_6q, cp_15_AC_6q_pp

Data file: class_data.txt
First    Last       Grade    GPA    Age
Alex     Jones      9        3.4    14
Beth     Hamilton   12       3.7    18
Charles  White      11       3.5    16
Daniel   Kim        10       3.8    16
Ethan    Brooks     11       3.9    17
Faith    Flemmings  10       3.0    15
Gina     Zhou       9        3.2    1


Write a program that reads in data about a class from the file class_data.txt and outputs the rows of data where a student has a GPA of at least 3.5. Include proper file error checking.

Below are the contents of the input file.

First    Last       Grade    GPA    Age
Alex     Jones      9        3.4    14
Beth     Hamilton   12       3.7    18
Charles  White      11       3.5    16
Daniel   Kim        10       3.8    16
Ethan    Brooks     11       3.9    17
Faith    Flemmings  10       3.0    15
Gina     Zhou       9        3.2    14


Below is one way to implement this program. We create an ifstream object to open our file. We check to make sure the file is opened correctly before we read the data values into corresponding variables. We check if the GPA is at least 3.5, and print the data values to the terminal if so.

Data file: shrimp.txt
There's pineapple shrimp, lemon shrimp, coconut shrimp,
pepper shrimp, shrimp soup, shrimp stew, shrimp salad,
shrimp and potatoes, shrimp burger, shrimp sandwich.


Selecting from: cp_15_AC_8q, cp_15_AC_8q_pp

Data file: mult_table.txt
Student output fil


Write a program that creates a multiplication table for the first 10 numbers using a matrix and outputting the table to an output file called mult_table.txt. Include proper file error checking.

Below is one way to implement this program. We create a 10x10 matrix and fill in the products. Then we traverse through the matrix and output the values into the output file.