Problem Solving with C++ (10th Edition)
Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
Publisher: PEARSON
bartleby

Concept explainers

bartleby

Videos

Textbook Question
Book Icon
Chapter 6, Problem 1P

Write a program that will search a file of numbers of type int and write the largest and the smallest numbers to the screen. The file contains nothing but numbers of type int separated by blanks or line breaks. If this is being done as a dass assignment, obtain the file name from your instructor.

Expert Solution & Answer
Check Mark
Program Plan Intro

Largest and Smallest numbers

Program Plan:

  • Include the required headers.
  • Define “main()” method.
    • Declare the required “int” and “char” variables.
    • Prompt the user for input file.
    • Read the required input file.
    • Create the object for “ifstream”.
    • Open the required file.
    • Check the file can be opened or not using “if” condition.
    • If the file includes any error, print file “cannot be opened”.
    • Initialize the input and check for largest and smallest numbers present in the file.
    • Print the output using “cout”
    • Return the required variable.
  • Close the “main()” method.
Program Description Answer

The below C++ program describes about the displaying of largest and smallest values among the numbers present in a given file.

Explanation of Solution

Program:

//Include the needed headers

#include <iostream>

#include <fstream>

#include <cstdlib>

#include <climits>

using namespace std;

//main() Method

int main()

{

//Declaration of int variable sum

int sum = 0;

//Declaration of char variable file_name

char file_name [31];

//Declaration of input and index

int input, i = 0;

//Declaration of largest

int largest = -INT_MAX;

//Declaration of smallest

int smallest = INT_MAX;

//prompt the user for input file name

cout << "Enter a file name."

<< " This Program limits file names to"

<< endl

<< " a maximum of 30 characters. " << endl;

//read the input file name

cin >> file_name;

//creating object for ifstream

ifstream infile;

//a handle for opening the input file

infile.open(file_name);

//check the condition

if(!infile)

{

//display the error

cout << "Cannot open file " << file_name

<< " Aborting program " << endl;

//stop and exit

exit (1);

}

//initialize the input

infile >> input;

//start the loop

while(infile)

{

//check the condition

if(input > largest)

//get the largest value

largest = input;

//check the file

if(input < smallest)

//get the smallest value

smallest = input;

cout << input << " " << endl;

infile >> input;

}

//display the output

cout << "smallest in file = " << smallest

<< " largest in file = " << largest << endl;

getchar();

getchar();

//return the required value

return 0;

}

Sample Output

Output:

Problem Solving with C++ (10th Edition), Chapter 6, Problem 1P

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Write a program that queries information from three files(given to you). The first file contains the names and telephone numbers of a group of people. The second file contains the names and Social Security numbers of a group of people. The third file contains the names and annual income of a group of people. The groups of people should overlap. Your program should ask the user for a telephone number and then print the name, Social Security number, and annual income, if it can determine that information. Sample run1: Enter the phone number (7 digits, with a dash): 555-1234 555-1234 is associated with Bob Bob's SSN is 000300021 Bob's salary is 55000 Sample run2: Enter the phone number (7 digits, with a dash): 675-4566 Couldn't find a name associated with that number. Sample run3: Enter the phone number (7 digits, with a dash): 000-2345 000-2345 is associated with John John's SSN is 000000004 John's salary is 65000 python language
The objective of this problem is to show that you can write a program that reads a text file and uses string methods to manipulate the text. File cart.txt contains shopping cart type data from golfsmith.com representing the gifts you have purchased for your dad for this past Fathers Day. Good for you. You have been very generous! The first item in each row of the file is the part number, the second the quantity, the third the price and the fourth the description. Your program will read the file line by line and compute the total cost of all of the items in the list. You will then display a message that looks like this, assuming the cost of each item adds up to 168.42: Here's what your output should look like: The total price for the awesome Fathers Day gifts you bought is $168.42. The shopping cart can be found here: cart.txt. Right click on the link and save the file to the folder where you have your Python files.
The program takes a single argument as the name of the file to be read. A sample file is named (Movies.txt). The program reads this file which has labeled columns separated by commas. Every row of this text file includes information about a movie. You are asked to read every row as a string and decompose the given information (separated by commas).   If a costumer wants too see "List of the Genres" (or genre,score,year) all genres type must be show.   I need this answer in C language. Please explain nicely. Thanks.

Chapter 6 Solutions

Problem Solving with C++ (10th Edition)

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...

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Constants, Variables, Data types, Keywords in C Programming Language Tutorial; Author: LearningLad;https://www.youtube.com/watch?v=d7tdL-ZEWdE;License: Standard YouTube License, CC-BY