Declare two variables for max and next Read first number Assign value of the fir
ID: 3533615 • Letter: D
Question
Declare two variables for max and next
Read first number
Assign value of the first number to max
Use a while-loop to read all other numbers
Compare each number with max; if new number is larger than max then assign value of
the new number to max
Write the largest number to the screen
Modify your code to find both smallest and largest numbers
The output of your program on screen should be similar to this:
The maximum number is 1000
The minimum number is 1
This is the link of the data.dat file..
http://www.sendspace.com/file/yf88rp
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int next, max, min;
ifstream inStream;
inStream.open( "Data.dat");
if(!inStream)
{cout<<"invalid file"; return 0;}
inStream>>next;
max = next;
min = next;
while(!inStream.eof()){
inStream>>next;
if(next < min)
min = next;
if(next > max)
max = next;
}
cout << "The maximum number is " << max<<" "; //dont use endl since its deprecated
cout << "The minimum number is " << min << " ";
inStream.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.