Alternatively, if you want to return something you could create a struct/class which contains the data you want to return.
#include <iostream>
#include <string>
#include <fstream>
struct names {
std::string first_name;
std::string last_name;
};
names read_from_file( std::ifstream &in ) {
names i_names;
in >> i_names.first_name >> i_names.last_name;
return i_names;
}
Probably isn't the best example of how to do it but gives you an idea anyhow.
twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
c++98 has the convenient std::pair<> .
c++09 also has std::tuple<> .
#include <utility>
#include <algorithm>
#include <tuple>
std::pair<int,int> min_n_max( const int* arr, std::size_t N )
{
// invariant: N > 0
int low = arr[0] ;
int high = arr[0] ;
for( std::size_t i=1 ; i<N ; ++i )
{
low = std::min( low, arr[i] ) ;
high = std::max( high, arr[i] ) ;
}
return std::make_pair(low,high) ;
}
std::tuple<int,int,double> min_max_n_avg( const int* arr, std::size_t N )
{
// invariant: N > 0
int low = arr[0] ;
int high = arr[0] ;
int sum = arr[0] ;
for( std::size_t i=1 ; i<N ; ++i )
{
low = std::min( low, arr[i] ) ;
high = std::max( high, arr[i] ) ;
sum += arr[i] ;
}
return std::make_tuple( low, high, sum/double(N) ) ;
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
> .. I have a large quantity of values...
if the values are in a file, pass the file (istream) to the minmax function. in the function, read the values one by one and check for min/max.
if the values are programmatically generated, pass the generator function (or function object) to the minmax function.
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287