An array without a size

The data file contains a sequence of numbers of up to 10 elements.
Find the minimum and maximum of the sequence.
Note that the array size is not specified!

example :

sequence : 5 2 3 4
answer : min = 2 ; max = 5

another example :
sequence : 5 2 6 8 4 2 1 5
answer : min = 1 ; max = 8

Any ideas?
You don't need an array. Just stop when you run out of data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

int main()
{
// ifstream in( "data.txt" );
   istringstream in( "5 2 6 8 4 2 1 5" );

   int mn, mx, value;
   if ( !( in >> value ) ) { cerr << "No data\n";   return 1; }
   mx = mn = value;
   while ( in >> value )
   {
      if ( value < mn ) mn = value;
      if ( value > mx ) mx = value;
   }
   cout << "Minimum: " << mn << "       Maximum: " << mx << '\n';
}
Minimum: 1       Maximum: 8
Last edited on
Thanks!
Topic archived. No new replies allowed.