c++function

Reading input data, manipulating it, and printing it, using only functions


Yesterday I came across a question on the Spanish site, which I found interesting for the challenge it proposes (it has no practical relevance), so I took the liberty of reposting it here:

Make a program in C++ that asks for 10 numbers, displays them, shows the sum of these and also the average.

The restrictions are:

Not to use variables of any type
Not to use any data structure
Not to use loops and conditionals
ONLY FUNCTIONS MUST BE USED

Parameters can be used.

The original post is here

This is as close as I've come....

#include <functional>
#include <iostream>
#include <string>

using namespace std;

int getSum( int suma ) {
   cin >> suma;
   return suma;
}

void message( int sum, int count, int limit ) {
    cout << "Sum = " << sum << endl;
    cout << "average = " << sum / limit;
}

void calculate( int sum, int count, int limit ) {
   sum = sum + getSum(sum ); 

     // we create a boolean that tells us if we are inside the iteration values 
     // of the iteration values  
     // bool isGreater = limit > count;
   
     // this line gives us an integer, which will serve as index 
     // to our array of functions
     // int value = isGreater - true + 1;

     // create the array of functions
   function< void( int, int, int )> array[ 2 ] {
        message,
        calculate
   };
   
     // we execute the corresponding one, 
     // "limit > count - true + 1" replaces “isGreater - true + 1”
   array[ limit > count - true + 1 ]( sum, ++count, limit );
}

int main() {
   int amount = 3;
   std::cout << "Enter " << amount << " values: ";
   calculate( 0, 1, amount );    
   return 0;
}

Explanation: In the operation sum + getSum(), this the trick, since by the way of working of the language, sum maintains the original value, although getSum() modifies it, and here we discover that there is a small trap... yes... we use a variable, the one that getSum() creates, only that it is created “on the fly”, therefore it does not appear in the code.

As long as limit is greater than quantity, value will be “1” (remember that false is “0” and true is “1”), so recursion will be done by increasing the value of quantity until it equals limit, then value will be “0” and message is called which does its job, and execution is finished.

The only thing left to do is to eliminate the array of functions, that's as close as I've come....


Solution

  • You might move local variable as parameter as you do for other variables. You just have to adjust second function with lambda:

    void calculate(int sum,
                   int count,
                   int limit,
                   std::function<void(int, int, int)> (&&funcs)[2] = {nullptr, nullptr})
    {
        funcs[0] = &message;
        funcs[1] = [&](int sum, int count, int limit) {
            sum = sum + getSum(sum );
            funcs[ limit > count - true + 1 ]( sum, count + 1, limit );
        };
        funcs[1](sum, count, limit);
    }
    

    Demo