They are of the same type. Just make them the elements of a std::vector<double> and return that.
The latter half of your code could also be massively reduced:
#include <iostream>
#include <tuple>
auto f( int x, int y, int z )
{
return std::make_tuple( x + 1, y + 2, z + 3 );
}
int main()
{
auto [a, b, c] = f( 0, 0, 0 );
std::cout << a << "\t" << b << "\t" << c << "\n";
std::tie(a, b, c) = f( a, b, c );
std::cout << a << "\t" << b << "\t" << c << "\n";
}
you can only ever return one thing via the return statement. That one thing can be a container of many things, including:
<tuple>
struct or class
pointer (static array is similar)
vector, string, map, pair, and the other containers
and anything else you can think of that wraps up many items behind one variable.
alternately, pass by reference will let you return many things:
void foo( int &i, double &d) //i and d can be modified to return new value inside foo. I do not care for this approach for many reasons, but many, many functions out there use this and its valid.
Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.
You can use the preview button at the bottom to see how it looks.
It would probably be better if q_table[][], optimal _action and exploration_rate weren't global variables either.
You don't appear to be doing anything with max.
1 2 3 4 5 6 7 8
float max = q_table[newstate][0];
int optimal_action = 0;
for ( int i = 1; i<4; i++ ) {
if ( q_table[newstate][i] > max ) {
max = q_table[newstate][i];
optimal_action = i;
}
}
The max is used in the condition. However, one can rewrite that without the variable:
1 2 3 4 5 6
int optimal_action = 0;
for ( int i = 1; i<4; i++ ) {
if ( q_table[newstate][i] > q_table[newstate][optimal_action] ) {
optimal_action = i;
}
}
One can go one step further and use library algorithms:
1 2
auto max = std::max_element( q_table[newstate], q_table[newstate] + 4 );
int optimal_action = std::distance( q_table[newstate], max );
Whether the q_table[][] is global or not, and whatever the return type is, what will probably happen with this call: auto result = probabilities( 999999999 );
In other words, should we really trust the user to give a valid index to the array?