![]() |
| ||
| powers of two, recursion. int ans, guess, random, type, counter, num, i; and i had to take that and turn it into a recursive function. IDK what recursion is other than "it calls itself" or something... anyway im lost on this. please help! |
| ||
| Re: powers of two, recursion. here is the recursive code for calculating power of 2; int power(int num) { if(num==0) return 1; if(num==1) return 2; return 2*power(num-1); } well after including this function in ur prog, u dont need to check whether the input value is 0 or 1. but check for -ve entry.. here is ur modified switch statements ... case 1 : /*powers of 2 calculator*/ printf("please enter a positive integer:\n"); fgets(numb, sizeof(numb), stdin); sscanf(numb, "%d", &num); //ans=2; if(num<0) printf("incorrect input, please input a positive integer:"); else{ ans=power(num); printf("The answer is %d\n", ans);} break; |
| All times are GMT -4. The time now is 5:24 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC