Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

A few C functions I need help with

Name: OP 2012-04-30 4:57

These are pretty trivial, but I am no programmer.


if I do swap( 4, 6 )

where

int swap( int x, int y) {
   x = y;
   y = x;
   return( y );
}

What will it return and why?

Name: Anonymous 2012-04-30 10:02

x will become 6, y will be redundantly assigned to 6, 6 will be returned

what you should do instead is this

void swap(int x, int y){
    int t=x; 
    x=y;
    y=t;
}

preferably written on a single line since it is such a simple function, like this
void swap(int x, int y){int t=x; x=y; y=t;}

Name: OP 2012-04-30 11:00

Thank you for the explanation and I am especially grateful for the improved version.

Since there is int before swap, the returned 6 would be considered integer, correct?

What purpose does the void type serve in the improved (and also working) version of swap?

Now, onto the next one:

int excl( int n ) {
  if ( n == 1 ) return 1;
  return n * excl( n - 1 );
}

I'm pretty sure this is just a function to compute factorial. The problem is what happens, when 0 (or negative number) is on the input. Will it result in an infinite loop? If so, how to prevent this and make it correctly output excl(0) as 1?

Name: Anonymous 2012-04-30 16:27

Since there is int before swap, the returned 6 would be considered integer, correct?
Yea
What purpose does the void type serve in the improved (and also working) version of swap?
It makes it so the function doesn't have to return anything, which resolves a redundancy issue in your original writing of the function which was return ( y ); The purpose of that function was only to manipulate a pair of variables you're passing into the function, not returning any kind of value when the function is used.

>how to prevent this and make it correctly output excl(0) as 1?
You have to fix the logical argument on if ( n == 1 ), since you want all things that are a value less than or equal to 1 to return 1, the logical argument should be   if(n <= 1)

Name: OP 2012-05-01 5:17

Great, thanks. It all makes sense now.

Name: Anonymous 2012-05-13 16:38

or if you know xor gates, do:
int swap(int x, int y) {
x ^= y;
y ^= x;
x ^= y;

return y;
}

or less verbosely,
void swap(int *x, int *y) {
*x ^= (*y ^= (*x ^= *y));
}

Name: Anonymous 2012-05-21 17:41

>>2
>>1
will never work. am I being trolled? 2 will not work because it does strictly nothing outside its own scope. Replacing the function with void swap(int a, int b){} would have the same effect. As for 1, at least you got the correct explanation of why it doesn't work. The correct answer is to void swap(int* a, int* b){ int t = *a; *a = *b; *b = t; }

>>3
This would, indeed, result in an infinite loop. To solve it, change 'return n*excl(n-1)' to 'return (n*excl(n<1?n+1:n-1))'. Then again, the factorial is undefined for values < 0, so the actual correct way to handle it is:

int excl( unsigned int n ) {
  if ( n < 2 ) return 1;
  return n * excl( n - 1 );
}

Don't change these.
Name: Email:
Entire Thread Thread List