Concept explainers
Write a
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.
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;
}
Output:
Want to see more full solutions like this?
Chapter 6 Solutions
Problem Solving with C++ (10th Edition)
Additional Engineering Textbook Solutions
Elementary Surveying: An Introduction To Geomatics (15th Edition)
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Concepts Of Programming Languages
SURVEY OF OPERATING SYSTEMS
Degarmo's Materials And Processes In Manufacturing
- True or False: Given the sets F and G with F being an element of G, is it always ture that P(F) is an element of P(G)? (P(F) and P(G) mean power sets). Why?arrow_forwardCan you please simplify (the domain is not empty) ∃xF (x) → ¬∃x(F (x) ∨ ¬G(x)). Foarrow_forwardHistogramUse par(mfrow=c(2,2)) and output 4 plots with different argument settings.arrow_forward
- (use R language)Scatter plot(a). Run the R code example, and look at the help file for plot() function. Try different values for arguments:type, pch, lty, lwd, col(b). Use par(mfrow=c(3,2)) and output 6 plots with different argument settings.arrow_forward1. Draw flow charts for each of the following;a) A system that reads three numbers and prints the value of the largest number.b) A system reads an employee name (NAME), overtime hours worked (OVERTIME), hours absent(ABSENT) and determines the bonus payment (PAYMENT).arrow_forwardScenario You work for a small company that exports artisan chocolate. Although you measure your products in kilograms, you often get orders in both pounds and ounces. You have decided that rather than have to look up conversions all the time, you could use Python code to take inputs to make conversions between the different units of measurement. You will write three blocks of code. The first will convert kilograms to pounds and ounces. The second will convert pounds to kilograms and ounces. The third will convert ounces to kilograms and pounds. The conversions are as follows: 1 kilogram = 35.274 ounces 1 kilogram = 2.20462 pounds 1 pound = 0.453592 kilograms 1 pound = 16 ounces 1 ounce = 0.0283 kilograms 1 ounce = 0.0625 pounds For the purposes of this activity the template for a function has been provided. You have not yet covered functions in the course, but they are a way of reusing code. Like a Python script, a function can have zero or more parameters. In the code window you…arrow_forward
- make a screen capture showing the StegExpose resultsarrow_forwardWhich of the following is not one of the recommended criteria for strategic objectives? Multiple Choice a) realistic b) appropriate c) sustainable d) measurablearrow_forwardManagement innovations such as total quality, benchmarking, and business process reengineering always lead to sustainable competitive advantage because everyone else is doing them. a) True b) Falsearrow_forward
- Vision statements are more specific than strategic objectives. a) True b) Falsearrow_forwardThe three components of the __________ approach to corporate accounting include financial, environmental, and social performance measures. Multiple Choice a) stakeholder b) triple dimension c) triple bottom line d) triple efficiencyarrow_forwardCompetitors, as internal stakeholders, should be included in the stakeholder management consideration of a company and in its mission statement. a) True b) Falsearrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrCOMPREHENSIVE MICROSOFT OFFICE 365 EXCEComputer ScienceISBN:9780357392676Author:FREUND, StevenPublisher:CENGAGE L