6 Replies - 406 Views - Last Post: 03 October 2008 - 09:10 AM Rate Topic: -----

#1 khestoi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-September 08

need help here

Posted 03 October 2008 - 08:12 AM

I just dont get this code.
Mind if you can help me guys explain this thing?


#include <iostream.h>

int n(int x) {
		return ( x ? (x > 0 ? n(x-1) * x: 0) : 1 );
}

int main() {
		int number;

		cout << "Enter an integer: ";
		cin >> number;

		cout << "n(" << number << ") = " << n(number) << endl;

		return 0;
}



I know that the output is the factorial of the number but i cant understand the code.

This post has been edited by khestoi: 03 October 2008 - 08:15 AM


Is This A Good Question/Topic? 0
  • +

Replies To: need help here

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: need help here

Posted 03 October 2008 - 08:15 AM

It is trying to check whether the entered string is palindrome. [I haven't tested it, so I don't know whether the logic works :). But I think it may work.]
Was This Post Helpful? 0
  • +
  • -

#3 khestoi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-September 08

Re: need help here

Posted 03 October 2008 - 08:17 AM

View PostAmitTheInfinity, on 3 Oct, 2008 - 08:15 AM, said:

It is trying to check whether the entered string is palindrome. [I haven't tested it, so I don't know whether the logic works :). But I think it may work.]


sorry got the wrong code
Was This Post Helpful? 0
  • +
  • -

#4 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: need help here

Posted 03 October 2008 - 08:28 AM

View Postkhestoi, on 3 Oct, 2008 - 08:47 PM, said:

View PostAmitTheInfinity, on 3 Oct, 2008 - 08:15 AM, said:

It is trying to check whether the entered string is palindrome. [I haven't tested it, so I don't know whether the logic works :). But I think it may work.]


sorry got the wrong code


:o I can't beleive you did that. :D never mind.

The new code you posted is a recursive function to calculate factorial of a number. It also uses a ternary conditional operator in it.
x ? (x > 0 ? n(x-1) * x: 0) : 1 ); means

if x is not zero then
----if x is greater than zero then
--------result = call same function with n-1
--------return (result * x)
----else
--------return 0
else
----return 1

I hope this will help you. :)
Was This Post Helpful? 0
  • +
  • -

#5 khestoi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-September 08

Re: need help here

Posted 03 October 2008 - 08:34 AM

Thanks!
Ur d man!
The loop is quite tricky.
Im not used to those kind of codes.
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,594
  • Joined: 16-October 07

Re: need help here

Posted 03 October 2008 - 08:48 AM

View Postkhestoi, on 3 Oct, 2008 - 11:12 AM, said:

int n(int x) {
	return ( x ? (x > 0 ? n(x-1) * x: 0) : 1 );
}



This is one of those cases where someone got way too happy with short syntax and murdered clarity.

To understand it, let's break it off into "if then" constructs:
int n(int x) {
	if (x) {
		if (x > 0) {
			return n(x-1) * x;
		} else {
			return 0;
		}
	} else {
		return 1;
	}
}



The "if (x)" is relying on an implicit conversion where 0 is false and everything else is true. Being explicit gives a little more clarity:
	if (x!=0) {
		if (x > 0) {
			return n(x-1) * x;
		} else {
			return 0;
		}
	} else {
		return 1;
	}



The logic is a little wonky, let's see if you we can break it down a little:
	if (x==0) {
		return 1;
	} else if (x > 0) {
		return n(x-1) * x;
	} else {
		return 0;
	}



There's actually no time that's you'd ever want to return a 0, so it's bugged. It will get there if the number is <0. Let's make it safe:
int n(int x) {
	if (x<2) { 
		return 1;
	} else {
		return n(x-1) * x;
	}
}



Now it's safe and a little easier to follow. I like this form a little better:
int n(int x) {
	if (x<2) { return 1; }
	return n(x-1) * x;
}



If you want to take it back to the inline:

int n(int x) { return (x<2) ? 1 : (n(x-1) * x); }



As to what it's doing? It's doing recursion. We had fairly exhaustive explorations of that on this site, if you care to search.

Hope this helps.

Edit: Darn, AmitTheInfinity beat me to it. I'm leaving it anyway, took me a while to type. :P

This post has been edited by baavgai: 03 October 2008 - 08:50 AM

Was This Post Helpful? 0
  • +
  • -

#7 khestoi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-September 08

Re: need help here

Posted 03 October 2008 - 09:10 AM

Thanks for a much clearer explanation!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1