4 Replies - 265 Views - Last Post: 06 June 2012 - 05:38 PM Rate Topic: -----

#1 mahkoe  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 12-March 11

Can anyone make heads or tails of this code (in C)

Posted 06 June 2012 - 05:11 PM

I'm familiar with c++, and can usually understand C code. But, I absolutely need to understand this bit of code here (to decompose a number into the sum of four squares, this is the original page I got it from) and it's like having my eyes slowly gouged out with a cheese grater. I can't understand a word. I've been trying to figure it out for hours, but once again I learned c++ and I'm not 100% aware of all the details and technicalities of c (or even c++, for that matter). Below, I have included the same code, but a little easier on the everything. Can anyone at least give me a brief description (in words or in c++ code) of what's going on? (it doesn't have to be very detailed, just enough so I can get a basic idea)

n;
t;

f (l, a, s)
{
return (t=s+a*a) < n ? //specifically, what does this do?
	l && f (l - 1, a, t) ? //...and this?
		printf (" %d",a) 
		: f(l,a+1,s)
		 
	: l | t - n ?
		0 
		: printf("%d: %d",n,a);
}

g(x)
{
f(x,1,0) || (x - 3 && g (x + 1) ); //...and this?
}

main()
{
	
	for( ; ++n <= 300 ; puts("") )
	g(0);
	
}



Thanks very much

Is This A Good Question/Topic? 0
  • +

Replies To: Can anyone make heads or tails of this code (in C)

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7576
  • View blog
  • Posts: 12,732
  • Joined: 19-March 11

Re: Can anyone make heads or tails of this code (in C)

Posted 06 June 2012 - 05:26 PM

return (t=s+a*a) < n ? //specifically, what does this do?


p?q:r is the ternary operator. It returns q if p is true, r if p is false. So this returns one of these expressions:

	l && f (l - 1, a, t) ? //...and this?
		printf (" %d",a) 
		: f(l,a+1,s)



or this

		 
	: l | t - n ?
		0 
		: printf("%d: %d",n,a);


Notice that both subexpressions include similar examples of the ternary.



	l && f (l - 1, a, t) ? //...and this?
		printf (" %d",a) 
		: f(l,a+1,s)



prints a if the expression
l && f (l - 1, a, t) 



evaluates to true,
or else it does a recursive call:
f(l,a+1,s)



g(x)
{
   f(x,1,0) || (x - 3 && g (x + 1) ); //...and this?
}



This calls f(x, 1, 0). If this call does not return true, then it evaluates the expression (x - 3 && g (x + 1) ). That is, if x - 3 is zero, it stops there (lazy evaluation). If x-3 is a non-zero value, then we have to evaluate the other side of the && which is a recursive call.

I think I've got that mostly right. I'm sure I'll be corrected if I've missed anything, though!

This post has been edited by jon.kiparsky: 06 June 2012 - 05:30 PM

Was This Post Helpful? 0
  • +
  • -

#3 mahkoe  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 12-March 11

Re: Can anyone make heads or tails of this code (in C)

Posted 06 June 2012 - 05:31 PM

Ohhhh I see it now. Thank you so much! I had no idea what the turnary operator was. This makes perfect sense now.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,724
  • Joined: 05-May 12

Re: Can anyone make heads or tails of this code (in C)

Posted 06 June 2012 - 05:36 PM

Line 6: Do you remember your ternary operator in C/C++?

Also on line 6: Do you remember the classic character counter implementation?
int len;
char c;
while((c = getchar()) != EOF)
    len++;



As for lines 7 and 18: Do you remember C/C++ Boolean short circuiting rules?
bool AlwaysTrue() {
    cout << "true";
}

bool AlwaysFalse() {
    cout << "false";
}

// What is your expected out put for the following?
AlwaysTrue() || AlwaysFalse();
AlwaysTrue() && AlwaysFalse();
AlwaysFalse() || AlwaysTrue();
AlwaysFalse() && AlwaysTrue();


Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7576
  • View blog
  • Posts: 12,732
  • Joined: 19-March 11

Re: Can anyone make heads or tails of this code (in C)

Posted 06 June 2012 - 05:38 PM

View Postmahkoe, on 06 June 2012 - 07:31 PM, said:

Ohhhh I see it now. Thank you so much! I had no idea what the turnary operator was. This makes perfect sense now.



Glad to help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1