c - Multiplication table using recursion without loops -
i've received assignment write recursive function receives 2 integersd user: x , y , prints out multiplication table number x*y. prototyping of function must like: void mult_table (int x, int base, int y) (base gets 1 upon first time function called). example, if x=4 , y=5, output be:
1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 note no loop can used inside function, additional recursive functions can used if needed.
my problems are: 1. how iterate base correctly in lines 2 , greater, because in line 1 simple ++ works, second line need 2. can't think stopping case function, because each time i'm print new line x , y values changed. grateful help, suggestions of other ways try , it.
void mult_table (int x, int base, int y) { int temp; //temp variable hold x vlaue if (base <= y) //as long base less or equal y, number of line printed { printf(" %d", base); //using base +1 incrementation mult_table(x, base+1, y); } else { printf("\n"); //start of new line temp = x; //value of x saved because changed still needed x= x+x*(1/(base-temp-1)); //new x value x+(x*1/original base of line) reach next x value y = y+y*(1/(base-temp-1)); //new y value y+(y*1/original base of line) reach next x value base = base - temp; //base incrimented 1 using calcualtion mult_table(x, base, y); //recursdive call } }
at each call increase base one. have decompose base f1 , f2 such f1 * f2 need print @ step base. give formula, since assignment choose give hint: write in table values of base , expected values of f1 , f2 , have find 2 formulas compute f1 , f2 based on base , y.
e.g. (for x=4 , y=5):
base f1 f1 1 1 1 2 1 2 .. 4 1 5 5 2 1 6 2 2 .. , on hints:
- notice
f2kind of loops around range. think mathematicalmod(%in c) that. - notice
f1grows 1 eachkiterations. think/that.
Comments
Post a Comment