15.18. Mixed Up Code Practice

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