c - How to use a function recursively? -
i have program prompts user enter 10 digits. program uses function return sum of divisors of each number entered minus , display until number equal 1 or 0. problem function stops after 45's divisors summed. i'm trying use recursion here because function called 'n' number of times until each 1 equal 0 or 1. makes recursion useful in situations these , how can apply here? there i'm missing way function called? can me?
for example,
if user types: 25 -4 6 45 (then presses enter)
the program should output:
25 1 0
-4 0 0
6 6
45 33 15 9 4 3 1 0 0
6 example of perfect number , repeat, if perfect number occurs should stop summing divisors. when sum equal 0 should print once , stop. -4 out of range should print 0. must greater 1.
#include<stdio.h> #include<stdlib.h> int main(void) { //fields int i=0, j=0, k=0, l=0, num = 0, x = 0, count = 0, total = 0, z = 0; signed int b[11] = {0}; char discard; //prompt message printf( "\n\nplease enter list of numbers: " ); //this while loop scans until enter button pressed while(i < 11 && (scanf("%d%1[^\n]s", &b[i], &discard)) == 2) { ++count; i++; } puts(""); puts(""); //display factors while(k <= count) { x=b[k]; num = sum_divisors(x); printf("%d " , num); k++; puts(""); } }//end of main //function sum divisors int sum_divisors(int a) { int total = 0; int z = 0; printf("%d ", a); if(a < 1) { total = 0; }else { if((a%1 == 0) && !=1) total = total + 1; for(z=2; z<102; z++) { if((a%z == 0) && != z) { total = total + z; }//if }//for }//end if/else statement // printf("%d ", total); return total; }//end function sum_divisors
first, wanted mention few things, first being statement a % 1 == 0
going true positive integer a
, , should not used in conditional, other should try avoid using magic
numbers, such 102
in loop in sum_divisors
; there more natural, , general value used replace 102
, , imagine using such constant cause more grief larger numbers divisors larger 102 not calculated.
that being said, way you've structured sum_divisors
pretty close how should be, accept missing recursive call , few changes need made make recursive.
- since want print sum of divisors, can't see reason have
sum_divisors
return value (accept possibly number of times recurred), return value ofvoid
seems more appropriate. lets rid of
(a%1==0)&&(a!=1)
, change base case to.if( <= 1 ) return;
so function knows when stop
- as said earlier, loop can changed more general, no magic numbers. can replace
102
a-1
in order omit countinga
divisor, instead ofa!=z
- check see if perfect number
- now call recursive call,
sum_divisors( total );
another thing initialise total such total = 1
,so don't need treat a=1
exception base case.
after said , done, what's left
void sum_divisors( int ) { int total = 1; // divisor sum of int z; printf("%d ", a); // print value if( <= 1 ) // base case return; for( z = 2; z < a-1; ++z) // z < a-1 not include in total { if( a%z == 0 ) { total += z; } } if( total == ) // perfect number we're done return; sum_divisors( total ); // call sum_divisors recursively = total }
it's not different had initially
Comments
Post a Comment