473,666 Members | 2,539 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Root valueProblem - Help

ali
Hi,

I'm trying to work on a recursive function that will give me root
valuefor a given number.

What i mean by root value is, if given 13, the answer is 1+3 = 4. If
given 65, the answer is 2, i.e, 6+5=11, and 1+1=2. The final answer is
always less than 10.

I've been able to work on the code, but i can get it to work for
values less than 10, example: if use 13, it gives 4. If i give 34, it
gives 7. But if i give 55, it give me 10 as the answer, instead of 1.

Here is the code:

int function(int n)
{
if ((n<9))
{
return n;
}
else
{
return ((n%10)+(functi on(n/10)));
}
}

Will appreciate some help on this,

Thanks,

Ali
Jul 22 '05 #1
8 1407

You need to recursively call your root function on the partial result if
the result is >= 10

int root( int n )
{
if (n < 9) return n;

int sum = 0;
for ( ;n ; n = n/10 )
{
sum += n % 10;

}

// this will eventually yield a value 1--9
return root(sum);
}
"ali" <tj@raha.com> wrote in message
news:et******** *************** *********@4ax.c om...
Hi,

I'm trying to work on a recursive function that will give me root
valuefor a given number.

What i mean by root value is, if given 13, the answer is 1+3 = 4. If
given 65, the answer is 2, i.e, 6+5=11, and 1+1=2. The final answer is
always less than 10.

I've been able to work on the code, but i can get it to work for
values less than 10, example: if use 13, it gives 4. If i give 34, it
gives 7. But if i give 55, it give me 10 as the answer, instead of 1.

Here is the code:

int function(int n)
{
if ((n<9))
{
return n;
}
else
{
return ((n%10)+(functi on(n/10)));
}
}

Will appreciate some help on this,

Thanks,

Ali

Jul 22 '05 #2
ali wrote:
What i mean by root value is, if given 13, the answer is 1+3 = 4. If
given 65, the answer is 2, i.e, 6+5=11, and 1+1=2. The final answer is
always less than 10.

I've been able to work on the code, but i can get it to work for
values less than 10, example: if use 13, it gives 4. If i give 34, it
gives 7. But if i give 55, it give me 10 as the answer, instead of 1.

Here is the code:

int function(int n)
{
if ((n<9))
{
return n;
}
else
{
return ((n%10)+(functi on(n/10)));
}
}

Will appreciate some help on this,


You are defining the "root value" in a recursive fashion. However, your
code is not actually implementing a recursive definition (except insofar
as it recurses to do the first summation).

A simple manual trace would indicate this. 55%10 is 5. function(55/10)
is function(5) is 5. 5+5 is 10, and there you have what you say you
get.

In order to get 1, you need to call function() again on the summed
result, by replacing the last line with this:

return function((n%10) +(function(n/10)));

- Brooks

--
The "bmoses-nospam" address is valid; no unmunging needed.
Jul 22 '05 #3
Dave Townsend wrote:
You need to recursively call your root function on the partial result if
the result is >= 10

int root( int n )
{
if (n < 9) return n;

int sum = 0;
for ( ;n ; n = n/10 )
{
sum += n % 10;

}

// this will eventually yield a value 1--9
return root(sum);
}
"ali" <tj@raha.com> wrote in message
news:et******** *************** *********@4ax.c om...
Hi,

I'm trying to work on a recursive function that will give me root
valuefor a given number.

What i mean by root value is, if given 13, the answer is 1+3 = 4. If
given 65, the answer is 2, i.e, 6+5=11, and 1+1=2. The final answer is
always less than 10.

I've been able to work on the code, but i can get it to work for
values less than 10, example: if use 13, it gives 4. If i give 34, it
gives 7. But if i give 55, it give me 10 as the answer, instead of 1.

Here is the code:

int function(int n)
{
if ((n<9))
{
return n;
}
else
{
return ((n%10)+(functi on(n/10)));
}
}

Will appreciate some help on this,

Thanks,

Ali



Heh.. sounds like one our our homework questions :-)
Jul 22 '05 #4
AlanP wrote:
[big snip]
Heh.. sounds like one our our homework questions :-)


Yeah. I thought about that about 15 seconds after sending my reply -- I
shall be rather peeved if I find out that I've been doing Ali's homework
for him. Although at least he had most of the solution already done,
rather than asking for us to solve it for him from scratch.

- Brooks
--
The "bmoses-nospam" address is valid; no unmunging needed.
Jul 22 '05 #5
ali <tj@raha.com> wrote:
I'm trying to work on a recursive function that will give me root
valuefor a given number.

What i mean by root value is, if given 13, the answer is 1+3 = 4. If
given 65, the answer is 2, i.e, 6+5=11, and 1+1=2. The final answer is
always less than 10.
Are you wanting to make it a recursive function because of a homework
requirement? If so, then OK; but if not, then I would say don't make
this a recursive function.

The amount of memory this function takes up if recursive will be a
function of the number of times it must recurse. A non-recursive version
would have a constant memory requirement and probably execute faster.

I also personally believe that non-recursive functions are easer to
understand...

unsigned root( unsigned n )
{
while ( n > 9 ) {
unsigned i = 0;
while ( n ) {
i += n % 10;
n /= 10;
}
n = i;
}
return n;
}

vs:

unsigned sub_root( unsigned n, unsigned i )
{
if ( n )
i = sub_root( n / 10, i + n % 10 );
return i;
}

unsigned root( unsigned n )
{
if ( n > 9 )
n = root( sub_root( n, 0 ) );
return n;
}

I think it is also very instructive to compare and contrast the above
two methods of solving the problem...

I've been able to work on the code, but i can get it to work for
values less than 10, example: if use 13, it gives 4. If i give 34, it
gives 7. But if i give 55, it give me 10 as the answer, instead of 1.

Here is the code:

int function(int n)
{
if ((n<9))
{
return n;
}
else
{
return ((n%10)+(functi on(n/10)));
}
}


Your code doesn't work because the problem requires two recursions.
Jul 22 '05 #6

"ali" <tj@raha.com> wrote in message
news:et******** *************** *********@4ax.c om...
Hi,

I'm trying to work on a recursive function that will give me root
valuefor a given number.

What i mean by root value is, if given 13, the answer is 1+3 = 4. If
given 65, the answer is 2, i.e, 6+5=11, and 1+1=2. The final answer is
always less than 10.

Why use recursion at all? It's just like the old "casting out nines" method
we learned in elementary school for checking sums. All you need is the mod
(%) function, except that you want to return 9 where the modulus is 0
(except for zero itself, which I'm guessing should return zero). How about
this:

unsigned int GetRoot( unsigned int val )
{
if (val == 0)
return 0; // handle special case of zero
else
{
root = val % 9;
if (root == 0)
return 9; // handle special case of zero modulus
else
return root; // all other cases
}

-Howard
Jul 22 '05 #7
"Dave Townsend" <da********@com cast.net> wrote in message news:<K7******* *************@c omcast.com>...
You need to recursively call your root function on the partial result if
the result is >= 10

int root( int n )
{
if (n < 9) return n;

int sum = 0;
for ( ;n ; n = n/10 )
{
sum += n % 10;

}

// this will eventually yield a value 1--9
return root(sum);
}


Ok, maybe I'm going blind. But what happens if you call this
with a value for n of 9? I'm thinking you learn the meaning
of the word "recursive. " In other words, shouldn't the test
be n < 10 rather than n < 9?
Socks
Jul 22 '05 #8

<pu*********@ho tmail.com> wrote in message
news:c7******** *************** ***@posting.goo gle.com...
Ok, maybe I'm going blind. But what happens if you call this
with a value for n of 9? I'm thinking you learn the meaning
of the word "recursive. " In other words, shouldn't the test
be n < 10 rather than n < 9?
Socks


It was probably a typo . . . calm down. You are right, it should be n < 10
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
8686
by: cplusplus | last post by:
Hello, I have newbie question. I'm stuck on this current assignment. Write a program that prompts the user for two integer values, passes the values to a function where they are multiplied together and the square root of the product is returned and displayed for the user. The function should return a double. Hint: If you multiply an integer by 1.0 the result will be a double. For example:
0
1873
by: ./Rob & | last post by:
Hi gang: I'm experiencing a problem with MySQL -- I updated MySQL from version 4.1.0 to 4.1.10 and now when I login as root it doesn't show all the databases I should have access to, nor it doesn't recognize me being logged in as root (via CURRENT_USER().) Here it is, line-by-line. Inline comments are denoted by '//'
5
4300
by: MLH | last post by:
I'm supposed to set a password for the MySQL root user. The output of mysql_install_db instructed me to run the following commands... /usr/bin/mysqladmin -u root -h appserver password mynwewpasswd I did. It did not work. Here's the error: /usr/bin/mysqladmin: connect to server at 'appserver' failed error: 'Host 'appserver.crci.com' is not allowed to connect to this MySQL server' Another command I'm supposed to run also resulted in an...
13
12737
by: Kishor | last post by:
Hi Friends Please help me to write a C program to find the 5th (fifth) root of a given number. Ex:(1) Input : 32 Output : 5th root of 32 is 2 Ex:(1) Input : 243 Output : 5th root of 243 is 3 Click here : www.c4swimmers.esmartguy.com to Test Your C Programming Strengths.
19
10208
by: Steve Franks | last post by:
I am using VS.NET 2005 beta 2. When I run my project locally using the default ASP.NET Development Web Server it runs using a root address like this: http://localhost:11243/testsite/ However when I deploy to a remote test server running real IIS, the real root of my application becomes: http://localhost/ What I want to do is have it so that on my local machine the asp.net dev
3
1700
by: Nalaka | last post by:
Hi, I have an asp.net web application (www.myWebSite), and a subweb application (www.myWebSite/subSite). How do I set it so that, subweb application (www.myWebSite/subSite) be the root application..... so that, when a user types www.myWebSite/subSite, it actualy, shows pages off subweb.
9
6640
by: MR | last post by:
I get the following Exception "The data at the root level is invalid. Line 1, position 642" whenever I try to deserialize an incoming SOAP message. The incoming message is formed well and its length is 642 bytes ( I have appended it to the end of this message). I suspect that the reason may have something to do with an incorrect declaration of which class to de-serialize to. In the attached code I substituted @@@@@@@ in the code below with...
7
161301
by: rajbala.3399 | last post by:
Hi , I want to download sql in my linux system........... # rpm -ivh MySQL-server-5.0.24a-0.glibc23.i386.rpm MySQL-cl ient-5.0.24a-0.glibc23.i386.rpm Preparing... ########################################### package MySQL-client-5.0.24a-0.glibc23 is already installed package MySQL-server-5.0.24a-0.glibc23 is already installed
2
2514
by: ershad | last post by:
Hello, I want to add a namespaces to the root element in a XML-message using the XmlTextWriter class: My XML-stream looks like this: <?xml version="1.0" encoding="us-ascii" standalone="yes"?> <root> <session name="All about XML"> <slides>
0
8454
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8561
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8645
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7389
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4200
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2776
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.