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
- I need help with a java problem: Write a program that reads the student information from a tab separated values (tsv) file. The program then creates a text file that records the course grades of the students. Each row of the tsv file contains the Last Name, First Name, Midterm1 score, Midterm2 score, and the Final score of a student. A sample of the student information is provided in StudentInfo.tsv. Assume the number of students is at least 1 and at most 20. Assume also the last names and first names do not contain whitespaces. - (The rest in described in the attached image) Code - import java.util.Scanner;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.PrintWriter;import java.io.IOException; public class LabProgram { public static void main(String[] args) throws IOException { Scanner scnr = new Scanner(System.in); /* TODO: Declare any necessary variables here. */ /* TODO: Read a file name from the user and read the…arrow_forwardCreate a text file of names, (3 names per line, 5 lines in total) followed by an age for each name. Go through the file and sum the ages. Create an output file with each persons name followed by age and then a line at the end of the output file that says “sum of all ages is: x”. Simple Java programming. Should include things like bufferedReader, fileReader, printWriter, fileWriter, file close, try-with-resources, Scanner, catch/try, etc. Include Java file and txt file. Thank you.arrow_forwardIn this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike assignment 5, the Student objects are sorted before they are written to the output file. • The program must implement a main class, three student classes (Student, UndergradStudent, GradStudent), and a Comparator class called StudentIDComparator. • The StudentIDComparator class must implement the java.util.Comparator interface, and override the compare() method. Since the Comparator interface is a generic interface, you must specify Student as the type parameter when defining the StudentIDComparator class, i.e., public class StudentIDComparator implements…arrow_forward
- Write a program that asks any number of students to enter their final grade and enables you to store these grades into a grades.txt plain text file. Then it reads the grades from the grades.txt file, and displays the individual grades and their count and average.arrow_forwardWrite a program that reads student data from a file, compute their GPA and writes the results to a different file. 1. The user should have the option to either enter their text file that contains the student grades or use the provided text file that contains theinformation. The data in "indata.txt" should look similar to this, Lara_Croft75 70 91 69 89Chris_Redfield68 88 79 85 94Johnny_Cochran69 98 95 77 80Wanda_Maximoff84 86 98 95 92Luke_Skywalker74 96 80 98 97William_Kurt89 52 99 81 58Samuel_Jackson50 96 50 64 95END_OF_FILE This is supposed to be in python. This is what I have so far. I have no experience I apologise. I am trying to finish the GPA calculating program currently but am having trouble finishing it. grade = input("enter grades")points = 0 if grade is 90-99:total_points = 4.0if grade is 80-89:total_points = 3.0if grade is 70-79:total_points = 2.0if grade is 60-69:total_points = 1.0if grade is 50-59:total_points = 0.0gpa = total_points/len(grade)print(gpa,"is gpa")arrow_forwardCode should be in Python. A photographer is organizing a photo collection about the national parks in the US and would like to annotate the information about each of the photos into a separate set of files. Write a program that reads the name of a text file containing a list of photo file names. The program then reads the photo file names from the text file, replaces the "_photo.jpg" portion of the file names with "_info.txt", and outputs the modified file names. Assume the unchanged portion of the photo file names contains only letters and numbers, and the text file stores one photo file name per line. If the text file is empty, the program produces no output. (Examples in image)arrow_forward
- A photographer is organizing a photo collection about the national parks in the US and would like to annotate the information about each of the photos into a separate set of files. Write a program that reads the name of a text file containing a list of photo file names. The program then reads the photo file names from the text file, replaces the "_photo.jpg" portion of the file names with "_info.txt", and outputs the modified file names. Assume the unchanged portion of the photo file names contains only letters and numbers, and the text file stores one photo file name per line. If the text file is empty, the program produces no output. Ex: If the input of the program is: ParkPhotos.txt and the contents of ParkPhotos.txt are: Acadia2003_photo.jpg AmericanSamoa1989_photo.jpg BlackCanyonoftheGunnison1983_photo.jpg CarlsbadCaverns2010_photo.jpg CraterLake1996_photo.jpg GrandCanyon1996_photo.jpg IndianaDunes1987_photo.jpg LakeClark2009_photo.jpg Redwood1980_photo.jpg…arrow_forwardA photographer is organizing a photo collection about the national parks in the US and would like to annotate the information about each of the photos into a separate set of files. Write a program that reads the name of a text file containing a list of photo file names. The program then reads the photo file names from the text file, replaces the "_photo.jpg" portion of the file names with "_info.txt", and outputs the modified file names. Assume the unchanged portion of the photo file names contains only letters and numbers, and the text file stores one photo file name per line. If the text file is empty, the program produces no output. Ex: If the input of the program is: ParkPhotos.txtand the contents of ParkPhotos.txt are:…arrow_forwardWrite a C++ program using classes that readslines from a file until the end of file. The program should prompt the user for the file name to read from. The program should open the file for reading, and if the filecannot be opened, print the message “File couldn’t be opened”, followed by a space and the filename, and exit. The program should keep track of the number of lines, the number of non-blank lines, the number of words, and the number of integers read from the file.arrow_forward
- Write a program for the following problem. You're given a file that contains a collection of IDs and scores (type int) for an exam in your computer course. You're to compute the average of these scores and assign grades to each student according to the following rule: If a student's score is within 10 points (above or below) of the average, assign a grade of satisfactory. If a student's score is more than 10 points above average, assign a grade of outstanding. If a student's score is more than 10 points below average, assign a grade of unsatisfactory. The output from your program should consist of a labeled three-column list that shows each ID, score, and corresponding grade. As part of the solution, your program should include functions that correspond to the function prototypes that follow. 7/ reads exam scores into array Scores void readStuData (ifstream &rss, int scores[], // IN: data file // OUT: the scores Oum, the TDsarrow_forwardWrite a program for the following problem. You're given a file that contains a collection of IDs and scores (type int) for an exam in your computer course. You're to compute the average of these scores and assign grades to each student according to the following rule: If a student's score is within 10 points (above or below) of the average, assign a grade of satisfactory. If a student's score is more than 10 points above average, assign a grade of outstanding. If a student's score is more than 10 points below average, assign a grade of unsatisfactory. The output from your program should consist of a labeled three-column list that shows each ID, score, and corresponding grade. As part of the solution, your program should include functions that correspond to the function prototypes that follow. // reads exam scores into array scores void readStuData (ifstream &rss, int scores[], // IN: data file // OUT: the scores // OUT: the IDs // OUT: Number of students read // OUT: A flag to indicate…arrow_forwardCreate a program that reads three lines from a text file. Each line should be read one at a time. For each word on a line, the user should be asked to specify how many syllables are in that word. The program should then inform the user if the file contains a valid Hiku using the function below: def isHiku (firstLine, secondLine, thirdLine):if (firstLine == 5 and secondLine == 7 and thirdLine == 5): return True return False print("The file contains a Hiku:", isHiku(5,7,5))Note: You must replace the values in the function with the numbers counted by your program.arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning