Skip to main content

How To Think Like a Computer Scientist C++ Edition The Pretext Interactive Version

Exercises 15.14 Coding Practice

1.

Write a program that takes in an input file called poem.txt
 1 
Data: 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
Had worn them really about the same,
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.
and prints the first 5 lines to the terminal. Include proper file error checking.
Solution.
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.
#include <iostream>
#include <fstream>
using namespace std;

int main() {
    ifstream infile("poem.txt");
    string input;
    if (!infile.good()) {
        cout << "Error. Unable to open file." << endl;
        exit(1);
    }
    for (int i = 0; i < 5; ++i) {
        getline(infile, input);
        cout << input << endl;
    }
}

2.

Write a program that prompts a user for the name of an input file and for an integer n. Then open the file and output the first n lines of the file with each line reversed. For example, if you read in the line “hello world” you should print out “dlrow olleh” to the terminal. Include proper file error checking. Check the hint below for help with the construction of the code.
Hint.

Activity 15.14.1.

Write a program that prompts a user for the name of an input file and for an integer n. Then open the file and output the first n lines of the file with each line reversed. For example, if you read in the line “hello world” you should print out “dlrow olleh” to the terminal. Include proper file error checking. Use the lines to construct the code, then go back to complete the Activecode.

3.

Write a program that takes in an input file called heights.txt
 2 
Data: heights.txt
62  67      75      68      65
67  70      72      74      66
72  66      66      73      69
61  60      73      72      60
, finds the median of the data, and prints “The median height is: height inches” to the terminal. Include proper file error checking.
Solution.
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.
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    ifstream infile("heights.txt");
    vector<int> data;
    double median;
    int height;
    if (!infile.good()) {
        cout << "Error. Unable to open file." << endl;
        exit(1);
    }
    while (infile >> height) {
        data.push_back(height);
    }
    sort(data.begin(), data.end());
    if (data.size() % 2 == 0) {
        median = (data[data.size() / 2 - 1] + data[data.size() / 2]) / 2.0;
    }
    else {
        median = data[data.size() / 2];
    }
    cout << "The median height is: " << median << " inches" << endl;
}

4.

Write a program that prompts a user for an integer n and print the first n powers of 2 to an output file called powers.txt. Include proper file error checking. To simulate what your output file would look like, the contents of your output file will be displayed on the terminal. Check the hint below for help with the construction of the code.
Hint.

Activity 15.14.2.

Write a program that prompts a user for an integer n and print the first n powers of 2 to an output file called powers.txt. Include proper file error checking. To simulate what your output file would look like, the contents of your output file will be displayed on the terminal. Use the lines to construct the code, then go back to complete the Activecode.

5.

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
 3 
Data: message.txt
Pbatenghyngvbaf! Lbh'ir qrpelcgrq guvf zrffntr.
applies ROT13, and outputs the result to the terminal. Include proper file error checking.
Solution.
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.
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;

string ROT13 (string message) {
    for (size_t i = 0; i < message.size(); ++i) {
        if (isalpha(message[i])) {
            if (message[i] >= 'A' && message[i] <= 'Z') {
                if (message[i] <= 'M') {
                    message[i] = message[i] + 13;
                }
                else {
                    message[i] = message[i] - 13;
                }
            }
            else {
                 if (message[i] <= 'm') {
                    message[i] = message[i] + 13;
                }
                else {
                    message[i] = message[i] - 13;
                }
            }
        }
    }
    return message;
}

int main() {
    ifstream infile("message.txt");
    string message;
    if (!infile.good()) {
        cout << "Error. Unable to open file." << endl;
        exit(1);
    }
    while (getline(infile, message)) {
        cout << ROT13(message) << endl;
    }
}

6.

Write a program that reads in data about a class from the file class_data.txt
 4 
Data: 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    14
and outputs the rows of data where a student has a GPA of at least 3.5. Include proper file error checking.
Solution.
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.
#include <iostream>
#include <fstream>
using namespace std;

int main() {
    ifstream infile("class_data.txt");
    string fname, lname;
    int grade, age;
    double gpa;
    if (!infile.good()) {
        cout << "Error. Unable to open file." << endl;
        exit(1);
    }
    getline(infile, fname);
    while (infile >> fname >> lname >> grade >> gpa >> age) {
        if (gpa >= 3.5) {
            cout << fname << '\t' << lname << '\t' << grade
                 << '\t' << gpa << '\t' << age << endl;
        }
    }
}

7.

Write a program that takes an input file called shrimp.txt and outputs the contents of the file where "shrimp" is replaced by a word that the user inputs to the terminal. Include proper file error checking. Check the hint below for help with the construction of the code.
Hint.

Activity 15.14.3.

Write a program that takes an input file called “shrimp.txt” and outputs the quote with “shrimp” replaced by a word that the user inputs to the terminal. Include proper file error checking. Use the lines to construct the code, then go back to complete the Activecode.
Data: 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.
That- that's about it.

8.

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.
Solution.
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.
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main() {
    ofstream outfile("mult_table.txt");
    if (!outfile.good()) {
        cout << "Error. Unable to open file." << endl;
        exit(1);
    }
    vector<int> rows(10);
    vector<vector<int> > mat;
    for (int i = 0; i < 10; ++i) {
        mat.push_back(rows);
    }
    for (int i = 0; i < 10; ++i) {
        for (int j = 0; i < 10; ++j) {
            matrix[i][j] = (i + 1) * (j + 1);
        }
    }
    for (int i = 0; i < 10; ++i) {
        for (int j = 0; i < 10; ++j) {
            outfile << matrix[i][j] << '\t';
        }
        cout << endl;
    }
}

9.

Write a program that computes the product of two matrices. Take a look at the example below. Two find the product of two matrices, take the ith row from the first matrix and the jth column from the second matrix, find the summation of the product of each component, and that’s the value that goes into the (i, j) location of the new matrix. The product of an mxn and an nxp matrix is an mxp matrix. Check the hint below for help with the construction of the code.
Hint.

Activity 15.14.4.

Write a program that computes the product of two matrices. Take a look at the example below. To find the product of two matrices, take the ith row from the first matrix and the jth column from the second matrix, find the summation of the product of each component, and that’s the value that goes into the (i, j) location of the new matrix. The product of an mxn and an nxp matrix is an mxp matrix. Use the lines to construct the code, then go back to complete the Activecode.
You have attempted 1 of 15 activities on this page.