Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Expert Solution & Answer
Chapter 6.3, Problem 29STE
Explanation of Solution
Program:
//Include the needed headers
#include <fstream>
#include <iostream>
using namespace std;
//main() Method
int main( )
{
//creating object for ifstream
ifstream fin;
//a handle for opening the input file
fin.open("stuff...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Write a cpp program that contains class “candidate”. A candidate has a name, number of electoral votes and number of popular votes.In your program, write a function that reads an input file containing elections results using input file stream and output to the console the name of the winner, his electoral votes and his popular votes.Your function should be prominent to faulty data; for example, if the number of votes for one of the candidates was a negative number, the function should exit and print a proper error message on the screen (You must use exceptions to do this)
Answer the given question with a proper explanation and step-by-step solution.
Write a function named file_sum that takes as a parameter the name of a text file that contains a list of numbers, one to a line, like this:```23.7711694-12.8014.999```The function should sum the values in the file and write the sum (just that number) to a text file named sum.txt.The file must be named: **file_sum.py**
Suppose a library is processing an input file containing the titles of books in order to identify duplicates. Write a program that reads all of the titles from an input file called bookTitles.inp and writes them to an output file called duplicateTitles.out. When complete, the output file should contain all titles that are duplicated in the input file. Note that the duplicate titles should be written once, even though the input file may contain same titles multiple times. If there are not duplicate titles in the input file, the output file should be empty. Create the input file using Notepad or another text editor, with one title per line. Make sure you have a number of duplicates, including some with three or more copies.
Chapter 6 Solutions
Problem Solving with C++ (10th Edition)
Ch. 6.1 - Prob. 1STECh. 6.1 - Prob. 2STECh. 6.1 - Suppose that you are still writing the same...Ch. 6.1 - Prob. 4STECh. 6.1 - Prob. 5STECh. 6.1 - Prob. 6STECh. 6.1 - Suppose bla is an object, dobedo is a member...Ch. 6.1 - Prob. 8STECh. 6.1 - Prob. 9STECh. 6.1 - A program has read half of the lines in a file....
Ch. 6.1 - Prob. 11STECh. 6.2 - Prob. 12STECh. 6.2 - Prob. 13STECh. 6.2 - Prob. 14STECh. 6.2 - What output will be sent to the stuff.dat when the...Ch. 6.2 - Prob. 16STECh. 6.2 - In formatting output, the following flag constants...Ch. 6.2 - Here is a code segment that reads input from...Ch. 6.2 - Prob. 19STECh. 6.2 - Write the definition for a void function called...Ch. 6.2 - (This exercise is for those who have studied the...Ch. 6.3 - Suppose c is a variable of type char. What is the...Ch. 6.3 - Suppose c is a variable of type char. What is the...Ch. 6.3 - Prob. 24STECh. 6.3 - Consider the following code (and assume that it is...Ch. 6.3 - Consider the following code (and assume that it is...Ch. 6.3 - Suppose that the program described in Self-Test...Ch. 6.3 - Consider the following code (and assume that it is...Ch. 6.3 - Prob. 29STECh. 6.3 - Define a function called copyLine that takes one...Ch. 6.3 - Prob. 31STECh. 6.3 - (This exercise is for those who have studied the...Ch. 6.3 - (This exercise is for those who have studied the...Ch. 6.3 - Suppose ins is a file input stream that has been...Ch. 6.3 - Write the definition for a void function called...Ch. 6.3 - Consider the following code (and assume that it is...Ch. 6.3 - Write some C++ code that will read a line of text...Ch. 6 - Write a program that will search a file of numbers...Ch. 6 - Write a program that takes its input from a file...Ch. 6 - a. Compute the median of a data file. The median...Ch. 6 - Write a program that takes its input from a file...Ch. 6 - Write a program that gives and takes advice on...Ch. 6 - Write a program that reads text from one file and...Ch. 6 - Prob. 7PCh. 6 - Write a program to generate personalized junk...Ch. 6 - Write a program to compute numeric grades for a...Ch. 6 - Enhance the program you wrote for Programming...Ch. 6 - Prob. 4PPCh. 6 - Write a program that will correct a C++ program...Ch. 6 - Write a program that allows the user to type in...Ch. 6 - This project is the same as Programming Project 6,...Ch. 6 - This program numbers the lines found in a text...Ch. 6 - Write a program that computes all of the following...Ch. 6 - The text file babynames2012.txt, which is included...Ch. 6 - To complete this problem you must have a computer...Ch. 6 - Write a program that prompts the user to input the...Ch. 6 - The following is an old word puzzle: Name a common...
Knowledge Booster
Similar questions
- Suppose a library is processing an input file containing the titles of books in order to identify duplicates. Write a program that reads all of the titles from an input file called bookTitles.inp and writes them to an output file called duplicateTitles.out. When complete, the output file should contain all titles that are duplicated in the input file. Note that the duplicate titles should be written once, even though the input file may contain same titles multiple times. If there are not duplicate titles in the input file, the output file should be empty. Create the input file using Notepad or another text editor, with one title per line. Make sure you have a number of duplicates, including some with three or more copies. (Programming Language: Java)arrow_forward1. Consider the following incomplete C++ program: #include int main() { … } a. Write a statement that includes the header files fstream, string, and iomanip in this program. b. Write statements that declare inFile to be an ifstream variable and outFile to be an ofstream variable. c. The program will read data from the file inData.txt and write output to the file outData.txt. Write statements to open both of these files, associate inFile with inData.txt, and associate outFile with outData.txt. d. Suppose that the file inData.txt contains the following data: Giselle Robinson Accounting 5600 5 30 450 9 75 1.5 The first line contains a person's first name, last name, and the department the person works in. In the second line, the first number represents the monthly gross salary, the bonus (as a percent), and the taxes (as a percent). The third line contains the distance traveled and the traveling time. The fourth line contains the number of coffee cups sold and the cost of each coffee cup. Write statements so that after the program executes, the contents of the file outData.txt are as shown below. If necessary, declare additional variables. Your statements should be general enough so that if the content of the input file changes and the program is run again (without editing and recompiling), it outputs the appropriate results. Name: Giselle Robinson, Department: Accounting Monthly Gross Salary: $5600.00, Monthly Bonus: 5.00, Taxes: 30.000 Paycheck: $4116.00 Distance Traveled 450.00 miles, Traveling Time: 9.00 hours Average Speed: 50.00 miles per hour Number of coffee Cups Sold: 75, Cost: $1.50 per cup Sales Amount = $112.50 e. Write statements that close the input and output files. f. Write a C++ program that tests the statements in parts a through e.arrow_forwardQuestion 3 Explain the difference between a script file and a function file i) Write a program using MATLAB that calculates the average, å = 1 N x x1 N i =1 and the standard deviation for a set of values (X), S = Make a script file ‘date.m’ with which the user can call up de desired date. How to solve the problem Ask the user (with the command >> input if he/she wants to know the date (1=yes, 2=no). - If the answer is ‘yes’ the date must be displayed (>> date). If the answer is ‘no’ display on the screen that the user does not want to know the date (>> disp) Write the m-file in MATLAB. Given the function prototype, Function [out] = examfcn (in) Write four (4) different calls to the function, each of…arrow_forward
- C Program Task Write a programme called pgmReduce which takes 3 arguments:1. an input file (either ASCII or binary)2. an integer factor n, and3. an output fileand reduces the input file by that factor in each dimension. For example, if the invocation is:pgmReduce inputFile 5 outputFileand inputFile is a 13x17 image, then outputFile should be a 2x3 image in which only thepixels with row and column of 0 modulo 5 in the inputFile exist.arrow_forwardWrite a C program, called reverse, using standard I/O functions, to take a file as inputthen copies it to another file in reverse order. That is, the last byte becomes the first,the byte just before the last one becomes the second, etc.The program call should look like:reverse fileIn fileOutarrow_forwardWrite a C++ function (along with helper functions if required) that takes a file stream as a parameter and prints out the maximum negative number in the given file. Do not need to write the main function. An example: [Example file contents] 4 -2 8 12 -8 9 3Prints: Maximum Negative number -2arrow_forward
- In Python Define a function writeScores with two parameters. The first should be the name of a file, and the second should be a list of integer scores. The set of scores should be appended to the given file, one score per line.arrow_forwardWrite a C++ function that meets the following specification.Name: wordFinderInputs: 1) input filename (string)2) pattern to find (string)Output: An integer representing the number of lines that contain the provided patternPurpose: The goal of this function is to open the file and read it line by line. In each line, you want to see if the pattern exists. If it does, then a counter should be incremented. If the file is unable to be open for any reason, a -1 should be returned. Otherwise, the number of lines containing the pattern should be returned. Assuming line is a string variable that holds the current line and pattern is a string variable holding the pattern to check for, the following code will determine if the pattern is in a string holding the current line:if (line.find(pattern) != string::npos)//Then the pattern is in the lineelse//The pattern is not in the linearrow_forwardWrite a C++ function that meets the following specification.Name: wordFinderInputs: 1) input filename (string)2) pattern to find (string)Output: An integer representing the number of lines that contain the provided patternPurpose: The goal of this function is to open the file and read it line by line. In each line, you want to see if the pattern exists. If it does, then a counter should be incremented. If the file is unable to be open for any reason, a -1 should be returned. Otherwise, the number of lines containing the pattern should be returned. Assuming line is a string variable that holds the current line and pattern is a string variable holding the pattern to check for, the following code will determine if the pattern is in a string holding the current line:if (line.find(pattern) != string::npos)//Then the pattern is in the lineelse//The pattern is not in the lineOptional Test: You can use the TOOP_Final.txt file and your answer from above to check how many lines contain " the "…arrow_forward
- Modify the program 11-10 from pages 753-755 as follows: 1. To Length class files (Length.h and Length.cpp) add the operator overload for stream insertion (<<) and stream extraction (>>) from page 759. You should not use the modified version of class Length from page 759 because it contains more operator overloads. Instead of typing you can use copy/paste from Length1.h and Length1.cpp respectively. 2. In main() replace the lines 11-16 with appropriate usage of stream extraction overload (>>) to change "first" and "second" objects with values inputted by the end user. 3. In main(), replace the output on lines 21,22 and 25,26 with appropriate usage of stream insertion (<<) operator. The resulting output should be identical with the unmodified version of the program. 11-10: // This program demonstrates the Length class's overloaded // +, -, ==, and < operators. #include <iostream> #include "Length.h" using namespace std; int main() { Length first(0),…arrow_forwardWrite a function that accepts names of two files: source and target as arguments and copies alternate characters from the source file to the target file. The function should return -1 if the source file does not exist.arrow_forwardWrite a C code to take input from an input .txt file and write into an output file. The input file should have int type values and the output file should contain the square of each inputfrom the input file. Must use file type pointers implementation. A sample input and output are given as follows:Input:1 2 3 4 5Output:1 4 9 16 25arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning