Obfuscated code

Hey forum.
I was amazed by the art of obfuscating just a little while ago. After countless attempts to change loops into recursive calls and things like these, I gave up. So here I am, asking how to obfuscate the following simple program (the function, only) using the steps shown here:
http://en.wikipedia.org/wiki/Obfuscated_code#Step_by_step

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
double averageoflist(int* numberlist, unsigned amount)
{
    double average;
    for(int i = 0; i<amount; i++)
    {
        average += numberlist[i];
    }
    return (average / amount);
}

int main()
{
    int* list = new int[5];
    list[0] = 2;
    list[1] = 51;
    list[2] = 103;
    list[3] = -89;
    list[4] = -13;
    std::cout << averageoflist(list,5);
    delete [] list;
    return 0;
}


Thanks in advance.
Last edited on
I don't know about obfuscating it (I was never a fan), but I can tell you that your are stepping out of bounds of your 'list' array (you're only allocating 4 elements when you should be allocating 5)
Woops, my mistake, I'll just change that little 4. :P
You could replace the average algorithm with something like this:

1
2
3
4
5
6
7
8
double averageoflist( int* nlist, unsigned amount ) {
    int whole = 0, sum = 0;

    for( int j = 0; j < amount; ++j )
         while( ( nlist[ j ] > amount && ++whole && nlist[ j ] -= amount ) || ( ++sum && --nlist[ j ] ) );

    return whole + (double)sum / amount;
}


Next, you could replace the array of ints by a 2-D array of chars, one char
per digit, then perform the additions by hand.

(EDIT: above code not tested)

Last edited on
This code has been tested and works for all non-negative numbers.

1
2
3
4
5
6
7
8
double averageoflist( int* nlist, unsigned amount ) {
    int whole = 0, sum = 0;

    for( int j = 0; j < amount; ++j )
         while( ( ( nlist[ j ] > amount ) && ++whole && ( nlist[ j ] -= amount ) ) || ( ++sum && --nlist[ j ] ) );

    return whole + (double)sum / amount;
}
Topic archived. No new replies allowed.