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
You are required to implement a program which will read in a text file (.txt) containing words which are arranged in sentences. Your program should first ask the user for the name of the file and then count the number of individual words and characters (including numbers and punctuation) in the file. The word and character count result should be printed on the screen. Furthermore, your program should count the number of individual words starting with the letter 'A', 'B', 'C' and so on to 'Z'. Again, these statistics should be printed on the screen. Your program should display the letter that appears at the start of most words. The program must be written in C. You should illustrate the operation of your program by generating a text file containing 3 sentences from a lead news story on the day of testing.
Write a program in Python that reads student information from a text file, then creates a text file that records the course grades of the students and a final grade. Each row of the .txt file contains the Last Name, First Name, Midterm1 score, Midterm2 score, and the Final score of a student, each separated by a space. A sample of the student information is provided in StudentInfo.txt below. Assume the number of students is at least 1 and at most 20. The program performs the following tasks: • Read the file name of the .txt file from the user. • Open the .txt file and read the student information using readline() or readlines(). • Compute the average exam score of each student. • Assign a letter grade to each student based on the average exam score in the following scale: ◦ A: 90 =< x ◦ B: 80 =< x < 90 ◦ C: 70 =< x < 80 ◦ D: 60 =< x < 70 ◦ F: x < 60 • Output the first names, last names, exam scores, and letter grades of the students into a text file named…
Write a program that opens a file me.txt and write your first name followed by a space followed by your last name, then your major on the second line. If the file does not exist, create it. If the file exists, have the file's contents be erased before writing your name. Be sure to close the file after. Example: Barack Obama Political Science Major

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