15.17. Activecode ExercisesΒΆ
Answer the following Activecode questions to assess what you have learned in this chapter.
Question
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.
1#include <iostream>
2// YOUR CODE HERE
Answer
Below is one way to write code that makes sure the file was opened properly.
1#include <cstdlib>
2#include <fstream>
3#include <iostream>
4
5int main() {
6 std::ifstream infile("locations.txt");
7 if (infile.good() == false) {
8 std::cout << "Unable to open the file." << std::endl;
9 std::exit(1);
10 }
11}
Question
Let's write a program that prompts the user for a filename and opens that file.
1#include <iostream>
2// YOUR CODE HERE
Answer
Below is one way to write the program.
1#include <cstdlib>
2#include <fstream>
3#include <string>
4#include <iostream>
5
6int main() {
7 std::string filename;
8 std::cout << "Enter the name of the file: ";
9 std::cin >> filename;
10 std::ifstream infile(filename);
11 if (infile.good() == false) {
12 std::cout << "Unable to open the file." << std::endl;
13 std::exit(1);
14 }
15}
Question
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#include <iostream>
2// YOUR CODE HERE
Answer
Below is one way to write the program.
1#include <cstdlib>
2#include <fstream>
3#include <iostream>
4
5int main() {
6 std::ofstream outfile("average.txt");
7 if (outfile.good() == false) {
8 std::cout << "Unable to open the file." << std::endl;
9 std::exit(1);
10 }
11 int sum = 0;
12 int n1, n2, n3, n4, n5;
13 std::cout << "Enter five integers separated by spaces: ";
14 if (!(std::cin >> n1 >> n2 >> n3 >> n4 >> n5)) return 1;
15 sum = n1 + n2 + n3 + n4 + n5;
16 outfile << "The average is " << sum / 5.0 << std::endl;
17}
Question
We are given a file called "data.txt" with an unknown number of
doublevalues. Write a program that finds the minimum, maximum, and number of data and outputs these values to a file called "summary.txt". 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#include <iostream>
2// YOUR CODE HERE
Answer
Below is one way to write the code.
1#include <cstddef>
2#include <cstdlib>
3#include <fstream>
4#include <iostream>
5
6int main() {
7 std::ifstream infile("data.txt");
8 std::ofstream outfile("summary.txt");
9 if (infile.good() == false || outfile.good() == false) {
10 std::cout << "Unable to open a file." << std::endl;
11 std::exit(1);
12 }
13 std::size_t num_data = 1;
14 double min, max, value;
15 if (!(infile >> value)) { return 1; }
16 min = value;
17 max = value;
18 while (infile >> value) {
19 ++num_data;
20 if (value < min) { min = value; }
21 if (value > max) { max = value; }
22 }
23 outfile << "Number of data: " << num_data << ", min: " << min << ", max: " << max << std::endl;
24}
Question
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
employeebefore you write yourmainfunction. Open and check the file before working with the data.
1#include <iostream>
2#include <string>
3#include <vector>
4// YOUR CODE HERE
Answer
Below is one way to define
employee.
1#include <cstdlib>
2#include <fstream>
3#include <iostream>
4#include <string>
5#include <vector>
6
7struct employee {
8 std::string fname;
9 std::string lname;
10 int age;
11 int phone;
12 std::string email;
13 employee(std::string f, std::string l, int a, int p, std::string e) {
14 fname = f;
15 lname = l;
16 age = a;
17 phone = p;
18 email = e;
19 }
20};
21
22int main() {
23 std::ifstream infile("employee_data.txt");
24 if (infile.good() == false) {
25 std::cout << "Unable to open the file." << std::endl;
26 std::exit(1);
27 }
28 std::vector<employee> data;
29 std::string fname, lname, email;
30 int age, phone;
31 while (infile >> fname >> lname >> age >> phone >> email) {
32 employee e(fname, lname, age, phone, email);
33 data.push_back(e);
34 }
35}
Question
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_lowerfirst. Separate the words with spaces.
1#include <iostream>
2// YOUR CODE HERE
Answer
Below is one way to write the program and
to_lowerfunction.
1#include <cctype>
2#include <cstddef>
3#include <cstdlib>
4#include <fstream>
5#include <iostream>
6#include <string>
7
8std::string upper_to_lower(std::string upper) {
9 for (std::size_t i = 0; i < upper.length(); ++i) {
10 upper[i] = static_cast<char>(std::tolower(static_cast<unsigned char>(upper[i])));
11 }
12 return upper;
13}
14
15int main() {
16 std::ifstream infile("UPPER.txt");
17 std::ofstream outfile("lower.txt");
18 if (infile.good() == false || outfile.good() == false) {
19 std::cout << "Unable to open a file." << std::endl;
20 std::exit(1);
21 }
22 std::string word;
23 while (infile >> word) {
24 std::string upper = upper_to_lower(word);
25 outfile << upper << " ";
26 }
27}
Question
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.
1#include <iostream>
2// YOUR CODE HERE
Answer
Below is one way to write the prgram.
1#include <cstdlib>
2#include <fstream>
3#include <iostream>
4
5int main() {
6 std::ifstream odds("odds.txt");
7 std::ifstream evens("evens.txt");
8 std::ofstream outfile("numbers.txt");
9 if (!odds.good() || !evens.good() || !outfile.good()) {
10 std::cout << "Unable to open a file." << std::endl;
11 std::exit(1);
12 }
13 int odd, even;
14 while (odds >> odd && evens >> even) {
15 outfile << odd << " " << even << " ";
16 }
17}
Question
Convert a vector of names to a set of unique names.
1#include <cstddef>
2#include <iostream>
3#include <map>
4#include <set>
5#include <string>
6#include <vector>
7
8// Write your function and a main that demonstrates it.
Answer
1#include <cstddef>
2#include <iostream>
3#include <map>
4#include <set>
5#include <string>
6#include <vector>
7
8std::set<std::string> vector_to_set(const std::vector<std::string>& data) {
9 std::set<std::string> names;
10 for (const auto& name : data) {
11 names.insert(name);
12 }
13 return names;
14}
15
16int main() {
17 auto names = vector_to_set({"Boston", "Chicago", "Boston"});
18 for (const auto& name : names) { std::cout << name << '\n'; }
19}
Question
Create a rectangular table with rows and columns supplied at run time. Initialize all entries to zero.
1#include <cstddef>
2#include <iostream>
3#include <map>
4#include <set>
5#include <string>
6#include <vector>
7
8// Write your function and a main that demonstrates it.
Answer
1#include <cstddef>
2#include <iostream>
3#include <map>
4#include <set>
5#include <string>
6#include <vector>
7
8std::vector<std::vector<int>> make_table(std::size_t rows, std::size_t columns) {
9 return std::vector<std::vector<int>>(rows, std::vector<int>(columns, 0));
10}
11
12int main() {
13 auto table = make_table(2, 3);
14 table.at(1).at(2) = 7;
15 std::cout << table.at(1).at(2) << '\n';
16}
Question
Count repeated city names with a map. Return the frequency of each name.
1#include <cstddef>
2#include <iostream>
3#include <map>
4#include <set>
5#include <string>
6#include <vector>
7
8// Write your function and a main that demonstrates it.
Answer
1#include <cstddef>
2#include <iostream>
3#include <map>
4#include <set>
5#include <string>
6#include <vector>
7
8std::map<std::string, std::size_t> count_cities(const std::vector<std::string>& names) {
9 std::map<std::string, std::size_t> counts;
10 for (const auto& name : names) {
11 ++counts[name];
12 }
13 return counts;
14}
15
16int main() {
17 auto counts = count_cities({"Boston", "Chicago", "Boston"});
18 for (const auto& [name, count] : counts) {
19 std::cout << name << ": " << count << '\n';
20 }
21}