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.
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.
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.
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.
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.
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 toLower
first. Separate the words with spaces.
Put the necessary blocks of code in the correct order.
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.
In chapter 15.7 we defined the Set
data structure.
Write a function vectorToSet
which takes a vector
of data and returns a Set
object with the data.
Put the necessary blocks of code in the correct order.
Put the Set
definition first in your answer.
Let’s write the struct
definition for a Matrix
! The underlying
data structure is a vector
of vector
s of int
s. Write
the constructor and at
function, which returns the data stored
at a given row and column. Put the necessary blocks of code in the correct order.
Now that we have the basic structure of a Matrix
, let’s write
a function that allows us to add data to a matrix. Write the Matrix
member function setData
which takes a row and column index as well
as a data value and stores the data value in the Matrix
at the
given location. Then read data in from a file called data.txt
.
The first line contains the number of rows and columns, separated by a space.
Data values begin on the next line. Put the necessary blocks of code in the correct order.