Problem Solving with C++ (10th Edition)
Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134521176
Author: SAVITCH
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 prompts the user for an input file name, reads all words from the input file, and writes the words to the output file sentences.txt. Start a new line whenever a word ends in a period, question mark, or exclamation mark. C++
Write a program that prompts the user for an input file name, reads all words from the input file, and writes the words to the output file named sentences.txt. Start a new line whenever a word ends in a period, question mark, or exclamation mark.
Write a program that reads data from a file containing integers that ends with -999.   Output the numbers that are divisible by 7   Output the numbers that are divisible by 11  Output the numbers that are not divisible by 7 or 11

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
How do you return a value from a function?

Starting Out with C++ from Control Structures to Objects (9th Edition)

Pennies for Pay Design a program that calculates the amount of money a person would earn over a period of time ...

Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)

When displaying a Java applet, the browser invokes the _____ to interpret the bytecode into the appropriate mac...

Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)

The command from the JDK compiles a Java program.

Java How To Program (Early Objects)

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
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
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