Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello everyone! I am very confused with how to do this programming assignment fo

ID: 3777728 • Letter: H

Question

Hello everyone! I am very confused with how to do this programming assignment for computer science. If anyone can help me I would greatly appreciate it! The assignment is described below:

"Write a program that reads a stream of integers from a file and prints to the screen the range of integers in the file (i.e. [lowest, highest]). You should first prompt the user to provide the file name. You should then read all the integers from the file, keeping track of the lowest and highest values seen in the entire file, and only print out the range of values after the entire file has been read. Importantly, you can make no assumptions about the contents of the file. If your program cannot read the file, opens an empty file, or encounters a non-integer while reading the file, your program should output that the file is invalid."

Thank you very much in advance everyone!

Explanation / Answer

#include <bits/stdc++.h>
#include <fstream>
using namespace std;
int main(int argc, char * argv[])
{
   cout<<"Enter file name ";
  
char filename[100];

gets(filename);
fstream myfile(filename, std::ios_base::in);

int a;
int max=INT_MIN;
int min=INT_MAX;

if(myfile==NULL)
{
   cout<<"Cant open file ";
   return 0;
}
else
{
   while (myfile >> a)
{
if(a>max)
   max=a;
if(a<min)
   min =a;
}

cout<<"Range is ["<<min<<","<<max<<"]";
}
  

return 0;
}

==================================================

akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Enter file name
input.txt
Range is [1,6]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote