New Semester
Started
Get
50% OFF
Study Help!
--h --m --s
Claim Now
Question Answers
Textbooks
Find textbooks, questions and answers
Oops, something went wrong!
Change your search query and then try again
S
Books
FREE
Study Help
Expert Questions
Accounting
General Management
Mathematics
Finance
Organizational Behaviour
Law
Physics
Operating System
Management Leadership
Sociology
Programming
Marketing
Database
Computer Network
Economics
Textbooks Solutions
Accounting
Managerial Accounting
Management Leadership
Cost Accounting
Statistics
Business Law
Corporate Finance
Finance
Economics
Auditing
Tutors
Online Tutors
Find a Tutor
Hire a Tutor
Become a Tutor
AI Tutor
AI Study Planner
NEW
Sell Books
Search
Search
Sign In
Register
study help
computer science
programming principles and practice
Programming Principles And Practice Using C++ 2nd Edition Bjarne Stroustrup - Solutions
What does isalnum(c) do?
When would you prefer line-oriented input to type-specific input?
Write a program to read a file of whitespace-separated numbers and output them in order (lowest value first), one value per line. Write a value only once, and if it occurs more than once write the count of its occurrences on its line. For example, 7 5 5 7 3 117 5 should
What happens if you position a file position beyond the end of file?
Write a program that reads a file of whitespace-separated numbers and outputs a file of numbers using scientific format and precision 8 in four fields of 20 characters per line.
What is a file position?
Write a program that reads a text file and writes out how many characters of each character classification (§11.6) are in the file.
Give two examples where a stringstream can be useful.
Define a Roman_int class for holding Roman numerals (as ints) with a << and >>. Provide Roman_int with an as_int() member that returns the int value, so that if r is a Roman_int, we can write cout << "Roman" << r << " equals " << r.as_int() << '\n';.
Write a program that reads the data from raw_temps.txt created in exercise 2 into a vector and then calculates the mean and median temperatures in your data set. Call this program temp_stats.cpp.Data from Exercise 2Write a program that creates a file of data in the form of the temperature Reading
What can a function declaration consist of?
When do we consider a program finished?
What should be in comments and what should not?
When would you not test a post-condition?
Reverse the order of words (defined as whitespace-separated strings) in a file. For example, Norwegian Blue parrot becomes parrot Blue Norwegian. You are allowed to assume that all the strings from the file will fit into memory at once.
Give an example of when it would probably be beneficial to use a binary file instead of a text file.
Reverse the order of characters in a text file. For example, asdfghjkl becomes lkjhgfdsa. Warning: There is no really good, portable, and efficient way of reading a file backward.
What is the difference between character I/O and binary I/O?
Write a function vector split(const string& s, const string& w) that returns a vector of whitespace-separated substrings from the argument s, where whitespace is defined as “ordinary whitespace” plus the characters in w.
Write a function vector split(const string& s) that returns a vector of whitespace-separated substrings from the argument s.
Split the binary I/O program from §11.3.2 into two: one program that converts an ordinary text file into binary and one program that reads binary and converts it to text. Test these programs by comparing a text file with what you get by converting it to binary and back.
Use the program from the previous exercise to make a dictionary (as an alternative to the approach in §11.7). Run the result on a multi-page text file, look at the result, and see if you can improve the program to make a better dictionary.
Modify the program from the previous exercise so that it replaces don't with do not, can't with cannot, etc.; leaves hyphens within words intact (so that we get “ do not use the as-if rule ”); and converts all characters to lower case.
Write a program that replaces punctuation with whitespace. Consider . (dot), ; (semicolon), , (comma), ? (question mark), - (dash), ' (single quote) punctuation characters. Don’t modify characters within a pair of double quotes ("). For example, “ - don't use the as-if rule.” becomes “ don
Write a program that reads strings and for each string outputs the character classification of each character, as defined by the character classification functions presented in §11.6. Note that a character can have several classifications (e.g., x is both a letter and an alphanumeric).
Write a program called multi_input.cpp that prompts the user to enter several integers in any combination of octal, decimal, or hexadecimal, using the 0 and 0x base suffixes; interprets the numbers correctly; and converts them to decimal form. Then your program should output the values in properly
Write a program that removes all vowels from a file (“disemvowels”). For example, Once upon a time! becomes nc pn tm!. Surprisingly often, the result is still readable; try it on your friends.
Write a program that given a file name and a word outputs each line that contains that word together with the line number.
Write a program that reads a text file and converts its input to all lower case, producing a new file.
What are the usual function declarations for << and >> for a user-defined type X?
What are the two most common uses of the istream member function clear()?
Why do we (often) want to separate input and output from computation?
In what way is output usually harder than input?
Write a program that produces the sum of all the whitespace-separated integers in a text file. For example, bears: 17 elephants 9 end should output 26.
In what way is input usually harder than output?
Add a command from x to the calculator from Chapter 7 that makes it take input from a file x. Add a command to y to the calculator that makes it write its output (both standard output and error output) to file y. Write a collection of test cases based on ideas from §7.3 and use that to test the
Discuss how the following input problems can be resolved:a. The user typing an out-of-range valueb. Getting no value (end of file)c. The user typing something of the wrong type
Write a program that takes two files containing sorted whitespace-separated words and merges them, preserving order.
Write a program that accepts two file names and produces a new file that is the contents of the first file followed by the contents of the second; that is, the program concatenates the two files.
Make a version of the calculator from Chapter 7 that accepts Roman numerals rather than the usual Arabic ones, for example, XXI + CIV == CXXV.
Write the function print_year() mentioned in §10.11.2.
Modify the store_temps.cpp program from exercise 2 to include a temperature suffix c for Celsius or f for Fahrenheit temperatures. Then modify the temp_stats.cpp program to test each temperature, converting the Celsius readings to Fahrenheit before putting them into the vector.Data from Exercise
Write a program that creates a file of data in the form of the temperature Reading type defined in §10.5. For testing, fill the file with at least 50 “temperature readings.” Call this program store_temps.cpp and the file it creates raw_temps.txt.
Write a program that produces the sum of all the numbers in a file of whitespace-separated integers.
Give an example of a calculation where a Rational gives a mathematically better result than double.
Give an example of a calculation where a Rational gives a mathematically better result than Money.
Define an input operator (>>) that reads monetary amounts with currency denominations, such as USD1.23 and DKK5.00, into a Money variable. Also define a corresponding output operator (<<).
Refine the Money class by adding a currency (given as a constructor argument). Accept a floating-point initializer as long as it can be exactly represented as a long int. Don’t accept illegal operations. For example, Money*Money doesn’t make sense, and USD1.23+DKK5.00 makes sense only if you
Design and implement a Money class for calculations involving dollars and cents where arithmetic has to be accurate to the last cent using the 4/5 rounding rule (.5 of a cent rounds up; anything less than .5 rounds down). Represent a monetary amount as a number of cents in a long int, but input and
Design and implement a rational number class, Rational. A rational number has two parts: a numerator and a denominator, for example, 5/6 (five-sixths, also known as approximately .83333). Look up the definition if you need to. Provide assignment, addition, subtraction, multiplication, division, and
Change the representation of a Date to be the number of days since January 1, 1970 (known as day 0), represented as a long int, and re-implement the functions from §9.8. Be sure to reject dates outside the range we can represent that way (feel free to reject days before day 0, i.e., no negative
Design and implement a set of useful helper functions for the Date class with functions such as next_workday() (assume that any day that is not a Saturday or a Sunday is a workday) and week_of_year() (assume that week 1 is the week with January 1 in it and that the first day of a week is a Sunday).
Implement leapyear() from §9.8.
Why are “helper functions” best placed outside the class definition?
Create a Library class. Include vectors of Books and Patrons. Include a struct called Transaction. Have it include a Book, a Patron, and a Date from the chapter. Make a vector of Transactions. Create functions to add books to the library, add patrons to the library, and check out books. Whenever a
What does adding const to a member function do?
Create a Patron class for the library. The class will have a user’s name, library card number, and library fees (if owed). Have functions that access this data, as well as a function to set the fee of the user. Have a helper function that returns a Boolean (bool) depending on whether or not the
Why should the public interface to a class be as small as possible?
Create an enumerated type for the Book class called Genre. Have the types be fiction, nonfiction, periodical, biography, and children. Give each book a Genre and make appropriate changes to the Book constructor and member functions.
When should operator overloading be used in a program? Give a list of operators that you might want to overload (each with a reason).
Add operators for the Book class. Have the == operator check whether the ISBN numbers are the same for two books. Have != also compare the ISBN numbers. Have a << print out the title, author, and ISBN on separate lines.
When should functions be put in the class definition, and when should they be defined outside the class? Why?
This exercise and the next few require you to design and implement a Book class, such as you can imagine as part of software for a library. Class Book should have members for the ISBN, title, author, and copyright date. Also store data on whether or not the book is checked out. Create functions for
What is an invariant? Give examples.
Look at the headache-inducing last example of §8.4. Indent it properly and explain the meaning of each construct. Note that the example doesn’t do anything meaningful; it is pure obfuscation.
Why is a constructor used for the Date type instead of an init_day() function?
Replace Name_pair::print() with a (global) operator << and define == and != for Name_pairs.
Design and implement a Name_pairs class holding (name,age) pairs where name is a string and age is a double. Represent that as a vector (called name) and a vector (called age) member. Provide an input operation read_names() that reads a series of names. Provide a read_ages() operation that prompts
List sets of plausible operations for the examples of real-world objects in §9.1 (such as toaster).
What is namespace std?
Why should you avoid using directives in a header?
What is a using declaration?
How does a namespace differ from a class?
What is the purpose of a namespace?
What is a call stack and why do we need one?
What goes into an activation record?
Which of the following is standard-conforming C++: functions within functions, functions within classes, classes within classes, classes within functions?
What do x&&y and x||y, respectively, mean?
Give an example of undefined order of evaluation. Why can undefined order of evaluation be a problem?
Would you ever define a function with a vector-by-value parameter?
What is a swap()?
What is the difference between pass-by-reference and pass-by-const-reference?
Can we declare a non-reference function argument const (e.g., void f(const int);)? What might that mean? Why might we want to do that? Why don’t people do that often? Try it; write a couple of small programs to see what works.
What is the difference between pass-by-value and pass-by-reference?
Write a function that takes a vector argument and returns a vector containing the number of characters in each string. Also find the longest and the shortest string and the lexicographically first and last string. How many separate functions would you use for these tasks? Why?
Why should a programmer minimize the number of global variables?
Improve print_until_s() from §8.5.2. Test it. What makes a good set of test cases? Give reasons. Then, write a print_until_ss() that prints until it sees a second occurrence of its quit argument.
What is the difference between a class scope and local scope?
Write a function that finds the smallest and the largest element of a vector argument and also computes the mean and the median. Do not use global variables. Either return a struct containing the results or pass them back through reference arguments. Which of the two ways of returning several
What kinds of scope are there? Give an example of each.
Write a function maxv() that returns the largest element of a vector argument.
What is the scope of a declaration?
Write a function that given two vectors price and weight computes a value (an “index”) that is the sum of all price[i]*weight[i]. Make sure to have weight.size()==price.size().
What are header files used for?
Then, do that exercise again but allowing an arbitrary number of names.
What good does indentation do?
Read five names into a vector name, then prompt the user for the ages of the people named and store the ages in a vector age. Then print out the five (name[i],age[i]) pairs. Sort the nam es (sort(name .begin(),name.end())) and print out the (name[i],age[i]) pairs. The tricky part here
Write versions of the functions from exercise 5, but with a vector.Data from Exercise 5Write two functions that reverse the order of elements in a vector. For example, 1, 3, 5, 7, 9 becomes 9, 7, 5, 3, 1. The first reverse function should produce a new vector with the reversed sequence, leaving its
Why is it a good idea to initialize variables as they are declared?
Write two functions that reverse the order of elements in a vector. For example, 1, 3, 5, 7, 9 becomes 9, 7, 5, 3, 1. The first reverse function should produce a new vector with the reversed sequence, leaving its original vector unchanged. The other reverse function should reverse the elements of
Showing 300 - 400
of 628
1
2
3
4
5
6
7
Step by Step Answers