GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Jun-2008, 20:45
gagless12's Avatar
gagless12 gagless12 is offline
New Member
 
Join Date: Apr 2008
Posts: 15
gagless12 is on a distinguished road

Trinary operator


I was looking at the following function definition and I'm not understanding how it works exactly.

CPP / C++ / C Code:
int F( int X )
{
	return (X<=0) ? 3 : F(X/2)+F(X-3);
}

I tested it out and put 0 and lower values into and got three, fine. But when I put in values greater than zero, like 2, it returned 9. 3 also returned 9. 4 returns 15.
  #2  
Old 04-Jun-2008, 21:31
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Trinary operator


Quote:
Originally Posted by gagless12
I tested it out and put 0 and lower values into and got three, fine. But when I put in values greater than zero, like 2, it returned 9. 3 also returned 9. 4 returns 14.
First of all, it is called the ternary operator.

Function F() as defined above is equivalent to the following:
CPP / C++ / C Code:
int f(int i) {
    int v;

    if (i <= 0)
        v = 3;
    else
        v = f(i / 2) + f(i - 3);

    return v;
}
Both forms are recursive -- the return value is determined by calling the same function again with differing arguments.

The sequence generated is:
Code:
0 3 1 6 2 9 3 9 4 15 5 18 6 18 7 24 8 33 9 33 10 42 11 51 12 51 13 60 14 75 15 75 16 93 17 108 18 108 19 126
The sequence is modestly interesting by virtue of its recurring plateaus, & you may be able to find a relation between the number of repeated plateau values, the value 3, & the fact that division by 2 is being done with integer division.

Otherwise, I do not recognize this sequence as being anything historically notable; it's just a sequence.
  #3  
Old 04-Jun-2008, 23:34
gagless12's Avatar
gagless12 gagless12 is offline
New Member
 
Join Date: Apr 2008
Posts: 15
gagless12 is on a distinguished road

Re: Trinary operator


Great. Thanks for the help.
  #4  
Old 05-Jun-2008, 00:45
gagless12's Avatar
gagless12 gagless12 is offline
New Member
 
Join Date: Apr 2008
Posts: 15
gagless12 is on a distinguished road

Re: Trinary operator


Have another question.

I'm trying to write a recursive function that takes one parameter ( an interger ) and returns how many of the digits are 3, recursively.

So if the parameter is 31337 the function returns 3, if its 0152, it returns 0.

I have the simple parts down, if x = 0 return 0 and if x = 3 return 1, but I can't figure out how to do the part where the function calls itself. Would I need to use an array? Or can it be done without one.
  #5  
Old 05-Jun-2008, 00:52
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Trinary operator


Quote:
Originally Posted by gagless12
Would I need to use an array?
To store the answer? I haven't worked out the solution yet, but no. You simply need the count.
  #6  
Old 06-Jun-2008, 08:42
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 696
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Trinary operator


Here is how I would do it:

CPP / C++ / C Code:
void NumDigits( int i )
{
  // If the absolute value of i is zero, return 0
  // If the absolute value of i is less than 10, return 1
  // return 1 + NumDigits(i/10)

}
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Concept of new operator vicky_brsh C++ Forum 3 20-Apr-2008 21:27
Write a C++ program involving operator overloading jjrsbigo C++ Forum 2 13-Dec-2006 07:47
infix to postfix Kacyndra C++ Forum 2 15-Aug-2005 11:44
C++ PhoneBook marita C++ Forum 46 12-Jun-2005 13:10
Operator overloading c++ clander C++ Forum 4 25-Apr-2005 10:21

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:36.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.