Solved

function is not correct...need help!

Posted on 2000-03-01
4
171 Views
Last Modified: 2010-04-15
The functions defined below. a call to the function factors(n), where n is a positive integer, is suppose to print all factors of n. however, the code is not correct. the function call factors(30);
should result in the output:
   1 2 3 5 6 10 15 30

(a) what is the actual output of the call factors(30);
(b) what modification(s) should be made to the code so it correctly     produces the factors of any given positive integer number?

    void facts(int val,int div)
           {
           if(div= =val)
              printf("%d \n",div);
           else
           {
             if (val % div ==0)
               {
                printf("%d \n",div);
                facts(val,div+1);
                }
           }
          }/*end of facts*/

    void factors(int val)
         {
          facts(val,1);
         }/*end of factors*/

thanks in advance!
0
Comment
Question by:beachbumm
  • 3
4 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 25 total points
ID: 2573056
Hi beachbumm,

problem is that you only call facts() recursive in facts() if ( val%div == 0), so i.e. if val=30 and div=4 the function terminates without calling facts() again.

This function for example does what you want to:

void facts(int val,int div)
{
 while ( val % div !=0 && div < val )
  div++;

 printf("%d ",div);

 if ( div < val )
  facts( val, div+1 );
}/*end of facts*/

void factors(int val)
{
 facts(val,1);
 printf( "\n" );
}/*end of factors*/

hope that helps,

ZOPPO
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 2573150
Zoppo is correct. Here's a version that follows your original code base:

void facts(int val,int div)
{
  if (div == val)
    printf("%d \n", div);
  else
    {
    if (val % div == 0)
      printf("%d \n", div);

    facts(val, div+1);
    }
}

void factors(int val)
{
  facts(val, 1);
}
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 2573206
SteveGTR's code is ok too, but a significant difference to mine I think is the recursion depth. My function's recursion depth depends on the 'hits' (how often val%dir==0), SteveGTR function's recursion depth is val-div.

so, i.e. factors( 10000 ) will give following:

1 2 4 5 8 10 16 20 25 40 50 80 100 125 200 250 400 500 625 1000 1250 2000 2500 5000 10000
My function: 25 calls
SteveGTR's function: 10000 calls

ZOPPO
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 2575634
BTW, why do you want this to be recursive at all? It's quit simple to do this none-recursive like this:

int facts(int val,int div)
{
 while ( div < val )
 {
  while ( val % div !=0 )
   div++;
  printf("%d ",div);
  div++;
 }
}/*end of facts*/

ZOPPO
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Read file descriptor 3 in C 14 104
How to set environment variables in C 2 78
Parsing XML instructions in Java/any other language.... 3 104
An API detour question 7 79
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now