Hi, all

I am trying to write a code for root test. I'm trying to do it recursively. For instance, If n = 25 and r = 2 then.. the domain is from 1 to 25 first it will add first number and last number which is 26 and divide by 2 is equal to 13 and it will go on like that...but i'm having problem how to write the code. Could someone help me about that?

Explain your needs properly and paste in the code which you have tried.

Take a paper, take a pen.
Scribble something until it makes sense.
Posting without trying is not wise,
It just show your ignorance.

Although we are here to solve your queries,
We, can't work until we have some berries.
Those berries are your tries,
So, don't post before attempting, it makes us cry.

One more thing I tell you my friend,
There is a page by Eric S Raymond.
Which you should read, here we pray,
and that page is "How to ask question the Smart way"
(Applause)
Thank You. Thank You.

global_procedure Root (
	alters Natural_Power_2& n,
	preserves Integer r
    );
    /*!
	requires
	    r > 0
	ensures
	    n^(r) <= #n < (n+1)^(r)
    !*/

//--------------------------------------------------------------

procedure_body Root (
	alters Natural_Power_2& n,
	preserves Integer r
    )
{
    
  // need to fill
   

}

there is the code i have so far.... i need to fill procedure body... any idea?

Aslan,
We would be happy if you can clearly give us full description of your problem.
Some clarifications sought:
Can r be 3? In that case how we need to proceed with the computation?

For n=25 r=2,
(1 + 25)/2 = 13
(2 + 24)/2 = 14
(3 + 23)/2 = 13
.....
....
(13 + 13)/2 = 13
Is the iterations mentioned above is correct?

What do we need to do after every iteration?
Do we add the resultant?

global_procedure Root (
	alters Natural_Power_2& n,
	preserves Integer r
    );
    /*!
	requires
	    r > 0
	ensures
	    n^(r) <= #n < (n+1)^(r)
    !*/

//--------------------------------------------------------------

procedure_body Root (
	alters Natural_Power_2& n,
	preserves Integer r
    )
{
    
  // need to fill
   

}

there is the code i have so far.... i need to fill procedure body... any idea?

global_procedure Root (
	alters Natural_Power_2& n,
	preserves Integer r
    );
    /*!
	requires
	    r > 0
	ensures
	    n^(r) <= #n < (n+1)^(r)
    !*/

//--------------------------------------------------------------

procedure_body Root (
	alters Natural_Power_2& n,
	preserves Integer r
    )
{
    
  // need to fill
   

}

there is the code i have so far.... i need to fill procedure body... any idea?

What you have posted is a pseudocode with two C function declarations and we asked for the code which you have tried ok. Don't do the cut and paste job here.We helping you here are not dumb to think above thing as a code.So if you want answers and help give an understandable description of your question and the code which you have tried...

object Natural_Power_2 too_high, too_low, n_temp, n2, n3, guess, temp_low;
    	self.Copy_To(too_high);
    	temp_low.Increment();
    	too_high.Increment();
    	n2.Convert_From_Integer(2);
 
    	while(temp_low.Compare(too_high) != 0)
    	{
			// create guess = ((high-low)/2)+ low
			n_temp.Clear();
			n_temp.Add(too_low);
			n_temp.Add(too_high);
			n_temp.Divide(n2, n3);
			n_temp.Copy_To(guess);
			n_temp.Power(r);
 
			// do compaisons to change the low and high
			// compare guess to self, if to high set new high
			if(n_temp.Compare(self) > 0)
			{
	    		guess.Copy_To(too_high);
			}
       		// compare guess to self, if to low set new low
			else
			{
	    		guess.Copy_To(too_low);
	    		too_low.Copy_To(temp_low);
	    		temp_low.Increment();
			}
    	}
 
    	//set value to too_low to self
    	too_low.Copy_To(self); 
    }

Theres my code but it doesnt work for any r it just works when r=2 any ideas

mail me i'll try to help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.