15.14. Mixed-Up Code Exercises

Arrange the following Mixed-Up Code exercises to assess what you have learned in this chapter.

15.14.1. Parsons exercises

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 complete the code in the correct order.

  1. for (std::size_t i = 0; i < n; i++) {
  2. if (in_file.good() == false) {
       std::cout << "Unable to open the file named " << file_in << " and output " << n << " lines." << '\n';
       std::exit(1);
    }
  3. int main () {
  4. std::cout << "Enter an integer: ";
    std::cin >> n;
    std::cout << n << '\n';
  5. std::cout << "Enter the name of the file: ";
    std::cin >> file_in;
    std::cout << file_in << '\n';
  6. std::cout << line << '\n';
  7. std::getline(in_file, line);
  8. std::ifstream in_file(file_in);
  9. std::size_t n = 0;
  10. std::string file_in;
  11. std::string line;
  12. }
  13. }

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 code tab.

  1. for (std::size_t i = 0; i < n; i++) {
  2. if (!student_output.good()) {
       std::cout << "Error opening student's output." << '\n';
    }
  3. if (outfile.good() == false) {
       std::cout << "Unable to open output file." << '\n';
       std::exit (1);
    }
  4. int main() {
  5. outfile << std::pow(2,i) << '\n';
  6. std::cin >> n;
  7. std::cout << "Enter an integer: ";
  8. std::cout << n << '\n';
  9. std::ifstream student_output("powers.txt");
  10. std::ofstream outfile ("powers.txt");
  11. std::size_t n = 0;
  12. std::string answer;
  13. while (std::getline(student_output, answer)) {
       std::cout << answer << '\n';
    }
  14. while (true) {
  15. }
  16. }
  17. }

Write a program that takes an input file called "dream.txt" and outputs the number of times the string "you" appears in the file to the terminal. Include proper file error checking. Use the lines to construct the code, then complete the code in the correct order.

  1. char w[ ] = {'y', 'o', 'u'};
  2. count++;
  3. for (std::size_t i = 0; i < line.size(); i++) {
  4. if (!in_file.good()) {
       std::cout << "Unable to open file." << '\n';
       std::exit(1);
    }
  5. if (line.at (i+2) == w[2]) {
  6. if (line.at(i) == w[0]) {
  7. if (line.at(i+1) == w[1]) {
  8. int main() {
  9. std::cout << count << '\n';
  10. std::ifstream in_file("dream.txt");
  11. std::size_t count = 0;
  12. std::string line;
  13. while (std::getline(in_file,line)) {
  14. }
  15. }
  16. }
  17. }
  18. }
  19. }

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 complete the code in the correct order.

  1. for (std::size_t i = 0; i < 4; i++) {
  2. for (std::size_t j = 0; j < line.size(); j++) {
  3. if (pos != std::string::npos) {
  4. int main() {
  5. line.replace(pos,replace.length(),word);
  6. std::cin >> word;
  7. std::cout << "Enter word to replace 'shrimp': ";
  8. std::cout << line<< '\n';
  9. std::cout << word << '\n';
  10. std::getline(in_file,line);
  11. std::ifstream in_file("shrimp.txt");
  12. std::size_t pos = line.find(replace);
  13. std::string line;
  14. std::string replace = "shrimp";
  15. std::string word;
  16. }
  17. }
  18. }
  19. }

Construct the multiplication program. Include the array, cstddef, and iostream headers. The result should be 58 64 on the first row and 139 154 on the second row.

  1. for (const auto& row : product) {
  2. for (int value : row) { std::cout << value << ' '; }
  3. for (std::size_t col = 0; col < b[0].size(); ++col) {
  4. for (std::size_t k = 0; k < b.size(); ++k) {
  5. for (std::size_t row = 0; row < a.size(); ++row) {
  6. int main() {
  7. product[row][col] += a[row][k] * b[k][col];
  8. std::array<std::array<int, 2>, 2> product{};
  9. std::array<std::array<int, 2>, 3> b{{{7, 8}, {9, 10}, {11, 12}}};
  10. std::array<std::array<int, 3>, 2> a{{{1, 2, 3}, {4, 5, 6}}};
  11. std::cout << '\n';
  12. }
  13. }
  14. }
  15. }
  16. }

We have a file called "locations.txt" that we want to read data from. Check to make sure that the file was opened properly; if it wasn't, display an error message and exit with a status of 1. Put the necessary blocks of code in the correct order.

  1. if (infile.good() == false) {
  2. if (infile.good()) {
  3. int main() {
  4. return 1;
  5. std::cout << "Unable to open the file." << '\n';
  6. std::exit(1);
  7. std::ifstream infile("locations.txt");
  8. std::ifstream infile(locations.txt);
  9. }
  10. }

Let's write a program that prompts the user for a filename and opens that file. Put the necessary blocks of code in the correct order.

  1. if (infile.good() == false) {
  2. int main() {
  3. std::cin >> filename;
  4. std::cout << "Enter the name of the file: ";
  5. std::cout << "Unable to open the file." << '\n';
  6. std::exit(1);
  7. std::ifstream filename;
  8. std::ifstream infile(filename);
  9. std::ofstream infile(filename);
  10. std::string filename;
  11. }
  12. }

Now let's write some output to a file. Write a program that prompts a user for a list of 5 integers separated by spaces, calculates the average of those integers, and outputs "The average is average" to an output file called "average.txt". Put the necessary blocks of code in the correct order. Declare the output file first and check that it is opened correctly.

  1. if (outfile.good() == false) {
       std::cout << "Unable to open the file." << '\n';
       std::exit(1);
    }
  2. int main() {
  3. int n1, n2, n3, n4, n5;
  4. int sum = 0;
  5. outfile << "The average is " << sum / 5.0 << '\n';
  6. std::cin >> n1 >> n2 >> n3 >> n4 >> n5;
  7. std::cout << "Enter five integers separated by spaces: ";
  8. std::cout << "The average is " << sum / 5.0 << '\n';
  9. std::ofstream outfile("average.txt");
  10. std::vector<int> list;  #distractor
  11. sum = n1 + n2 + n3 + n4 + n5;
  12. }

We are given a file called "data.txt" with an unknown number of double values. Write a program that finds the minimum, maximum, and number of data and outputs these values to a file called "summary.txt". Put the necessary blocks of code in the correct order. Declare the input and output files first, and check to see that both are opened correctly before dealing with data. Increment the number of data points before checking for the min and max.

  1. ++num_data;
  2. double min, max, value;
  3. if (!(infile >> value)) { return 1; }
  4. if (infile.good() == false || outfile.good() == false) {
       std::cout << "Unable to open a file." << '\n';
       std::exit(1);
    }
  5. if (value < min) { min = value; }
  6. if (value > max) { max = value; }
  7. int main() {
  8. max = value;
  9. min = value;
  10. outfile << "Number of data: " << num_data << ", min: " << min << ", max: " << max << '\n';
  11. std::ifstream infile("data.txt");
  12. std::ofstream outfile("summary.txt");
  13. std::size_t num_data = 1;
  14. std::vector<int> data;  #distractor
  15. while (infile >> value) {
  16. }
  17. }

You are given a file called "employee_data.txt" and you want to store the information from that file into a vector of data. The file contains information about an employee's first and last name, age, phone number, and email. Write the definition of an employee before you write your main function. Open and check the file before working with the data. Put the necessary blocks of code in the correct order.

  1. data.push_back(e);
  2. employee e(fname, lname, age, phone, email);
  3. employee(std::string f, std::string l, int a, int p, std::string e) {
       fname = f;
       lname = l;
       age = a;
       phone = p;
       email = e;
    }
  4. if (infile.good() == false) {
       std::cout << "Unable to open the file." << '\n';
       std::exit(1);
    }
  5. int age, phone;
  6. int main() {
  7. std::ifstream infile("employee_data.txt");
  8. std::string fname, lname, email;
  9. std::vector<employee> data;
  10. struct employee {
       std::string fname;
       std::string lname;
       int age;
       int phone;
       std::string email;
  11. while (infile >> fname >> lname >> age >> phone >> email) {
  12. while (infile) {
  13. }
  14. }
  15. };

You are given a file but it appears that someone's capslock key was stuck because everything is in uppercase. Write a program that takes the input from the file "UPPER.txt" and converts all the words to lowercase and prints out the modified message to a file called "lower.txt". Write the definition of the function to_lower first. Separate the words with spaces. Put the necessary blocks of code in the correct order.

  1.    }
       return upper;
    }
  2. if (infile.good() == false || outfile.good() == false) {
       std::cout << "Unable to open a file." << '\n';
       std::exit(1);
    }
  3. int main() {
  4. outfile << upper << ' ';
  5. std::ifstream infile("UPPER.txt");
  6. std::ofstream outfile("lower.txt");
  7. std::string upper = upper_to_lower(word);
  8. std::string upper_to_lower(std::string upper) {
       for (std::size_t i = 0; i < upper.length(); ++i) {
  9. std::string word;
  10. std::tolower(upper[i]);
  11. upper[i] = static_cast<char>(std::tolower(static_cast<unsigned char>(upper[i])));
  12. upper_to_lower(word);
  13. while (infile >> word) {
  14. }
  15. }

Nobody ever put a limit on how many files we can work with. Does this mean we can open two or more files at once? Yes we can! Write a program that combines two files "odds.txt" and "evens.txt" into one output file "numbers.txt". You should combine them in a way such that "numbers.txt" contains the first odd number then the first even number then the second odd number and so on. You are guaranteed that there are equal amounts of odd and even numbers. Put the necessary blocks of code in the correct order.

  1. if (!odds.good() || !evens.good() || !outfile.good()) {
       std::cout << "Unable to open a file." << '\n';
       std::exit(1);
    }
  2. int main() {
  3. int odd, even;
  4. outfile << odd << ' ' << even << ' ';
  5. std::ifstream evens("evens.txt");
  6. std::ifstream odds("odds.txt");
  7. std::ofstream outfile("numbers.txt");
  8. while (odds >> odd && evens >> even) {
  9. }
  10. }

Convert a vector of names to a set of unique names. Assume the necessary standard-library headers are included.

  1. for (const auto& name : data) {
  2. names.insert(name);
  3. return names;
  4. std::set<std::string> names;
  5. std::set<std::string> vector_to_set(const std::vector<std::string>& data) {
  6. }
  7. }

Create a rectangular table with rows and columns supplied at run time. Initialize all entries to zero. Assume the necessary standard-library headers are included.

  1. return std::vector<std::vector<int>>(rows, std::vector<int>(columns, 0));
  2. std::vector<std::vector<int>> make_table(std::size_t rows, std::size_t columns) {
  3. }

Count repeated city names with a map. Return the frequency of each name. Assume the necessary standard-library headers are included.

  1. ++counts[name];
  2. for (const auto& name : names) {
  3. return counts;
  4. std::map<std::string, std::size_t> count_cities(const std::vector<std::string>& names) {
  5. std::map<std::string, std::size_t> counts;
  6. }
  7. }