15.16. Coding Practice

Activecode

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. Select the Parsonsprob tab for hints for the construction of the code.

Example c192_cp_15_ac_2q
1#include <iostream>
2#include <fstream>
3
4// Write your code here.

Parsonsprob

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

  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." << std::endl;
       std::exit(1);
    }
  3. int main () {
  4. std::cout << "Enter an integer: ";
    std::cin >> n;
    std::cout << n << std::endl;
  5. std::cout << "Enter the name of the file: ";
    std::cin >> file_in;
    std::cout << file_in << std::endl;
  6. std::cout << line << std::endl;
  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. }

Input File

Below are the contents of the input file.

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.

Activecode

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. Select the Parsonsprob tab for hints for the construction of the code.

Example c192_cp_15_ac_4q
 1#include <string>
 2#include <iostream>
 3#include <fstream>
 4#include <cmath>
 5
 6int main() {
 7   // Write your code here.
 8
 9
10
11
12
13   // Do not modify the code below
14   std::ifstream student_output("powers.txt");
15   if (!student_output.good()) {
16         std::cout << "Error opening student's output." << std::endl;
17   }
18   std::string answer;
19   while (std::getline(student_output, answer)) {
20         std::cout << answer << std::endl;
21   }
22}

Parsonsprob

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

  1. for (std::size_t i = 0; i < n; i++) {
  2. if (!student_output.good()) {
       std::cout << "Error opening student's output." << std::endl;
    }
  3. if (outfile.good() == false) {
       std::cout << "Unable to open output file." << std::endl;
       std::exit (1);
    }
  4. int main() {
  5. outfile << std::pow(2,i) << std::endl;
  6. std::cin >> n;
  7. std::cout << "Enter an integer: ";
  8. std::cout << n << std::endl;
  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 << std::endl;
    }
  14. while (true) {
  15. }
  16. }
  17. }

Activecode

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. Select the Parsonsprob tab for hints for the construction of the code.

Example c192_cp_15_ac_6q
1#include <iostream>
2#include <fstream>
3
4// Write your code here.

Parsonsprob

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 go back to complete the Activecode tab.

  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." << std::endl;
       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 << std::endl;
  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. }

Input File

Below are the contents of the input file.

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?

Activecode

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. Select the Parsonsprob tab for hints for the construction of the code.

Example c192_cp_15_ac_8q
1#include <iostream>
2#include <fstream>
3
4// Write your code here.

Parsonsprob

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

  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<< std::endl;
  9. std::cout << word << std::endl;
  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. }

Input File

Below are the contents of the input file.

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.

Activecode

Multiply a two-by-three array by a three-by-two array. The result has two rows and two columns. For each result element, sum the products of the corresponding row and column entries.

Example c192_cp_15_ac_10q
1#include <array>
2#include <cstddef>
3#include <iostream>
4
5int main() {
6    std::array<std::array<int, 3>, 2> a{{{1, 2, 3}, {4, 5, 6}}};
7    std::array<std::array<int, 2>, 3> b{{{7, 8}, {9, 10}, {11, 12}}};
8    // Write your multiplication and print the result.
9}

Parsonsprob

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. }