Function riddle (C/C++)

Place to talk about all that new hardware and decaying software you have.

Moderator: General Mods

Post Reply
DeFender1031
Rookie
Posts: 18
Joined: Tue Dec 05, 2006 9:50 pm

Function riddle (C/C++)

Post by DeFender1031 »

Who can tell me what this function is designed to do?

Code: Select all

void calculate(unsigned int number, int cow, char bleh, double three, long double eight)
{
  printf("%u\n",number);
  calculate(number+1, cow+5, three+2, eight, bleh-1);
}
Last edited by DeFender1031 on Wed Oct 17, 2007 1:46 am, edited 1 time in total.
byuu

Post by byuu »

You mean other than cause a runtime stack overflow exception? :P

Let's cut through the 'fluff' ...

Code: Select all

void calculate(int x, int y, int z) {
  printf("%2d %2d %2d\n", x, y, z);

//x, y, z -> y, z, x = rotate variables left by 1 (3-step loop)

//x = y + 2
//y = z
//z = x - 1

  calculate(y + 2, z, x - 1);
}
If x were y + 1, I'd think it was a rotation of some sort. Because it's +2, it causes all three values to increment by one every three iterations. This also causes the sum of (x,y,z) to always increment by one each recursive call (eg 2+0-1=1). What the iterations mean in the interim two of three steps though, I'm not sure.

Log of the first few runs:

Code: Select all

 0  0  0= 0
 2  0 -1= 1
 2 -1  1= 2

 1  1  1= 3
 3  1  0= 4
 3  0  2= 5

 2  2  2= 6
 4  2  1= 7
 4  1  3= 8

 3  3  3= 9
 5  3  2=10
 5  2  4=11

 4  4  4=12
 6  4  3=13
 6  3  5=14
funkyass
"God"
Posts: 1128
Joined: Tue Jul 27, 2004 11:24 pm

Post by funkyass »

an expensive way of counting.
Does [Kevin] Smith masturbate with steel wool too?

- Yes, but don’t change the subject.
whicker
Trooper
Posts: 479
Joined: Sat Nov 27, 2004 4:33 am

Post by whicker »

A crappy IIR filter with most of the coefficients (and input value) set to 1?
Nach
ZSNES Developer
ZSNES Developer
Posts: 3904
Joined: Tue Jul 27, 2004 10:54 pm
Location: Solar powered park bench
Contact:

Post by Nach »

Cute, I think you meant to have that first param be unsigned and change it to %u though.
May 9 2007 - NSRT 3.4, now with lots of hashing and even more accurate information! Go download it.
_____________
Insane Coding
DeFender1031
Rookie
Posts: 18
Joined: Tue Dec 05, 2006 9:50 pm

Post by DeFender1031 »

byuu wrote:Blah Blah Blah Code Blah Blah
Seriously, why are you making up your own code instead of telling me what mine does? You removed some key variables, you're printing totally different things than mine is... I'll give you one thing, you DID pull an interesting pattern out of some variables there.
Nach wrote:Cute, I think you meant to have that first param be unsigned and change it to %u though.
Oh shoot... gotta fix that... *Goes back and edits the original post*
odditude
Official tech support dood
Posts: 2118
Joined: Wed Jan 25, 2006 7:57 am

Post by odditude »

Is it safe to assume that 'char' ranges from -128 to 127, unsigned int from 0 to 65535, and int from -32768 to 32767?

If so, life will get a little more interesting after the original value of "bleh" rolls over (assuming there isn't an overflow error instead of a simple rollover).

Regardless, it still seems that all the program will print is the numbers from 0 to 65535, one per line, repeating ad infinitum, starting from whatever the initial value of "number" is.
Why yes, my shift key *IS* broken.
Nach
ZSNES Developer
ZSNES Developer
Posts: 3904
Joined: Tue Jul 27, 2004 10:54 pm
Location: Solar powered park bench
Contact:

Post by Nach »

odditude wrote:Is it safe to assume that 'char' ranges from -128 to 127, unsigned int from 0 to 65535, and int from -32768 to 32767?
No.
May 9 2007 - NSRT 3.4, now with lots of hashing and even more accurate information! Go download it.
_____________
Insane Coding
funkyass
"God"
Posts: 1128
Joined: Tue Jul 27, 2004 11:24 pm

Post by funkyass »

for those that don't know:

unsigned int is 0 -> +4,294,967,295 , int is -2,147,483,648 -> +2,147,483,647 unsigned char is 0-255, signed char is -128-127

this is a C++ function, what the sizes of the data types are is Dependant on the compile target, but for the sake of sanity assume its x86.
Does [Kevin] Smith masturbate with steel wool too?

- Yes, but don’t change the subject.
DeFender1031
Rookie
Posts: 18
Joined: Tue Dec 05, 2006 9:50 pm

Post by DeFender1031 »

odditude wrote:Regardless, it still seems that all the program will print is the numbers from 0 to 65535, one per line, repeating ad infinitum, starting from whatever the initial value of "number" is.
That is incorrect. And would still be incorrect if you had said 0 to 4,294,967,295.
byuu

Post by byuu »

Seriously, why are you making up your own code instead of telling me what mine does? You removed some key variables, you're printing totally different things than mine is... I'll give you one thing, you DID pull an interesting pattern out of some variables there.
1) Because I don't know what your code does.
2) Because you don't ever actually use either cow or number for anything other than a printf statement.
3) Because that's the way I analyze problems.
4) I have to guess what inputs to give the first calculate call, since you never specified start values, hence why we have different outputs. The last three vars should be the same for both of ours. I only changed their names.

But if you're going to get upset over it, I'll move on. I don't know what it does. It looks like gibberish to me with no context. Maybe someone else can solve it, then.
funkyass
"God"
Posts: 1128
Joined: Tue Jul 27, 2004 11:24 pm

Post by funkyass »

assuming the function in question would actually compile...

stuffing a char into a long double might make the compiler question the sanity of the programmer. Putting a double into a char would make the compiler phone the police.
Does [Kevin] Smith masturbate with steel wool too?

- Yes, but don’t change the subject.
Nach
ZSNES Developer
ZSNES Developer
Posts: 3904
Joined: Tue Jul 27, 2004 10:54 pm
Location: Solar powered park bench
Contact:

Post by Nach »

Okay, since people are getting upset over this, I'll give away the answer, and this should have been obvious (at least it was to me when I first saw it).

This function is recursive, and seems to run until it crashes, each time printing out the value of number which increments by one per itteration. The other parameters don't seem to do anything but take up stack space.

So assuming that the first call to the function is with number being equal to 0/1, it counts how many times a recursive function can call itself with 2 ints, a char, a double, and a long double.

You should be safe to have recursion go up to 100,000 times or so on modern PCs.
Last edited by Nach on Wed Oct 17, 2007 8:22 pm, edited 2 times in total.
May 9 2007 - NSRT 3.4, now with lots of hashing and even more accurate information! Go download it.
_____________
Insane Coding
DeFender1031
Rookie
Posts: 18
Joined: Tue Dec 05, 2006 9:50 pm

Post by DeFender1031 »

byuu wrote: 1) Because I don't know what your code does.
2) Because you don't ever actually use either cow or number for anything other than a printf statement.
3) Because that's the way I analyze problems.
4) I have to guess what inputs to give the first calculate call, since you never specified start values, hence why we have different outputs. The last three vars should be the same for both of ours. I only changed their names.

But if you're going to get upset over it, I'll move on. I don't know what it does. It looks like gibberish to me with no context. Maybe someone else can solve it, then.
1) Looking at a fridge won't help you figure out how an internal combustion engine works.
2) Incorrect, every variable there is vital.
3) That's your prerogative
4) true

I'm not upset. I do not get upset. I was just wondering why you think a fridge will help.
funkyass wrote:assuming the function in question would actually compile...

stuffing a char into a long double might make the compiler question the sanity of the programmer. Putting a double into a char would make the compiler phone the police.
No, it compiles fine, no warnings even.
Nach wrote: Okay, since people are getting upset over this, I'll give away the answer, and this should have been obvious (at least it was to me when I first saw it).

This function is recursive, and seems to run until it crashes, each time printing out the value of number which increments by one per itteration. The other parameters don't seem to do anything but take up stack space.

So assuming that the first call to the function is with number being equal to 0/1, it counts how many times a recursive function can call itself with 2 ints, a char, a double, and a long double.

You should be safe to have recursion go up to 100,000 times or so on modern PCs.
correct. It's to test how much stack space there is. You win the prize, i WON'T come over to your house and kill you. Thank you for playing.
Last edited by DeFender1031 on Wed Oct 17, 2007 8:20 pm, edited 1 time in total.
Deathlike2
ZSNES Developer
ZSNES Developer
Posts: 6747
Joined: Tue Dec 28, 2004 6:47 am

Post by Deathlike2 »

Bah to infinite recursion. Could've seen that coming w/o having really guessed the output/behavior of the function itself.
Continuing [url=http://slickproductions.org/forum/index.php?board=13.0]FF4[/url] Research...
Nightcrawler
Romhacking God
Posts: 922
Joined: Wed Jul 28, 2004 11:27 pm
Contact:

Post by Nightcrawler »

Lame riddle. The answer is a crashing program...
[url=http://transcorp.romhacking.net]TransCorp[/url] - Home of the Dual Orb 2, Cho Mahou Tairyku Wozz, and Emerald Dragon SFC/SNES translations.
[url=http://www.romhacking.net]ROMhacking.net[/url] - The central hub of the ROM hacking community.
byuu

Post by byuu »

correct. It's to test how much stack space there is.
Easy enough to do that without the need for all the variables. Then you can extrapolate by testing sizeof() on each type you want. I did this when choosing recommended defaults for thread heap sizes.

For 32-bit platforms and IIRC, DJGPP gives 512KB, Visual C++ and MinGW give 1MB; unless of course you override the default stack sizes.

I used something like:

Code: Select all

void heapsize() {
static int size = 0;
  size += sizeof( void (*)() );
  printf("heap ~= %d bytes\n", size);
  heapsize();
}
If I'm still missing what you're doing, then I give up.
1) Looking at a fridge won't help you figure out how an internal combustion engine works.
Analogies aside, I think I typically do quite well reverse engineering stuff. Of course, I can't even compare to Nach, blargg or anomie. Which is actually kind of nice, I like having others to seek advice from and aspire to.

But who knows, I'm sure my technique could use a lot more work. And sorry, your first reply came off like you were perturbed by my attempt.
Lame riddle. The answer is a crashing program...
Bah, seconded. I was hoping it would hold a special mathematic property or something.
grinvader
ZSNES Shake Shake Prinny
Posts: 5632
Joined: Wed Jul 28, 2004 4:15 pm
Location: PAL50, dood !

Post by grinvader »

byuu wrote:I was hoping it would hold a special mathematic property or something.

Code: Select all

typedef enum { knf, sw1, sw2, ax1, ax2, ham, spr, stf, fll, bow, glv, NA} wname;

struct
{
  uint8_t inc[7];
  bool used;
} weap[11];

static void next_step(uint8_t table[11], const wname last)
{
  wname lnz = last;
  uint8_t tmp;

  while (!table[lnz] || !weap[lnz].used) { lnz--; }

  if (lnz == last)
  {
    tmp = table[lnz];
    table[lnz] = 0;

    do { lnz--; } while (!table[lnz] || !weap[lnz].used);
  }
  else { tmp = 0; }

  table[lnz]--;

  do { lnz++; } while (!weap[lnz].used);
  table[lnz] = tmp+1;
}
Have fun.
皆黙って俺について来い!!

Code: Select all

<jmr> bsnes has the most accurate wiki page but it takes forever to load (or something)
Pantheon: Gideon Zhi | CaitSith2 | Nach | kode54
Post Reply