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
(Short-answer) b. Continue from the previous question. Suppose part of the data you extracted from the data warehouse is the following.   Identify the missing values you think exist in the dataset. Use Column letter and Row number to refer to each missing value in the dataset. Please write down how you want to address each particular missing value (you can group them if they receive same treatment). For imputation, you do not need to calculate the exact imputed values but just describe what kind of value you want to use to impute.
Please original work Locate data warehousing solutions offered by IBM, Oracle, Microsoft, and Amazon Compare and contrast the capabilities of each solution and provide several names of some organizations that utilize each of these solutions. Please cite in text references and add weblinks
Need Help: Which of the following statements about confusion matrix is wrong A) Confusion matrix is a performance measure for probability prediction techniques B) Confusion matrix is derived based on classification rules with cut-off value 0.5 C) Confusion matrix is derived based on training partition to measure a model’s predictive performance D) None of the above

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