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)+(function(n/10)));
}
}
Will appreciate some help on this,
Thanks,
Ali 8 1392
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.com... 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)+(function(n/10))); } }
Will appreciate some help on this,
Thanks,
Ali
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)+(function(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.
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.com...
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)+(function(n/10))); } }
Will appreciate some help on this,
Thanks,
Ali
Heh.. sounds like one our our homework questions :-)
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.
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)+(function(n/10))); } }
Your code doesn't work because the problem requires two recursions.
"ali" <tj@raha.com> wrote in message
news:et********************************@4ax.com... 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
"Dave Townsend" <da********@comcast.net> wrote in message news:<K7********************@comcast.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
<pu*********@hotmail.com> wrote in message
news:c7**************************@posting.google.c om... 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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... ...
|
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"...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |