How to Multiply Two Positive Integers Using Recursion | Comp Sci Questions

  • Thread starter Thread starter courtrigrad
  • Start date Start date
  • Tags Tags
    comp sci
AI Thread Summary
A recursive method to multiply two positive integers using repeated additions can be defined as a*b = a + (a * (b-1)). The provided code has errors, particularly in the recursive call where it incorrectly adds the arguments. The correct implementation should ensure that the recursive function is called with two parameters. Additionally, for the hello method, the output formatting needs correction; it should use concatenation instead of a comma. Overall, the discussion highlights the importance of proper syntax and logic in recursive function implementation.
courtrigrad
Messages
1,236
Reaction score
2
Hello all

If we want to write a recursive method that multiplies two positive integers using repeated additions, we know that: a\times b = a + (a\times(b-1)) Would this be correct:

Code:
// pre: a and b are positive
// post: returning the product of a and b.
public int mult(int a, int b )

a*b = a +(a * (b-1))
if( b ==1)
return(a);
else
  return mult(a + a*(b-1)))


Also if you had:

Code:
public void hello(int n )
{
   System.out.println("hello n = ",n);
   if(n > 1)
      hello(n-1);
   System.out.println("goodbye n = ", n);

How would you find the output when a 3 is passed? I got a bumch of hellos and goodbyes, but I got it wrong


Thanks :smile:
 
Physics news on Phys.org
are these right?
 
courtrigrad said:
Code:
// pre: a and b are positive
// post: returning the product of a and b.
public int mult(int a, int b )

a*b = a +(a * (b-1))

The above line is unnecessary.

courtrigrad said:
Code:
if( b ==1)
return(a);
else
  return mult(a + a*(b-1)))

Your second return line here is wrong. You're calling mult which should have two arguments...
 
courtrigrad said:
Also if you had:

Code:
public void hello(int n )
{
   System.out.println("hello n = ",n);
   if(n > 1)
      hello(n-1);
   System.out.println("goodbye n = ", n);

How would you find the output when a 3 is passed? I got a bumch of hellos and goodbyes, but I got it wrong


Thanks :smile:

I think it should be:
System.out.println("hello n="+n);

Instead of a comma you need a "+".
 
I multiplied the values first without the error limit. Got 19.38. rounded it off to 2 significant figures since the given data has 2 significant figures. So = 19. For error I used the above formula. It comes out about 1.48. Now my question is. Should I write the answer as 19±1.5 (rounding 1.48 to 2 significant figures) OR should I write it as 19±1. So in short, should the error have same number of significant figures as the mean value or should it have the same number of decimal places as...
Thread 'A cylinder connected to a hanging mass'
Let's declare that for the cylinder, mass = M = 10 kg Radius = R = 4 m For the wall and the floor, Friction coeff = ##\mu## = 0.5 For the hanging mass, mass = m = 11 kg First, we divide the force according to their respective plane (x and y thing, correct me if I'm wrong) and according to which, cylinder or the hanging mass, they're working on. Force on the hanging mass $$mg - T = ma$$ Force(Cylinder) on y $$N_f + f_w - Mg = 0$$ Force(Cylinder) on x $$T + f_f - N_w = Ma$$ There's also...

Similar threads

Replies
7
Views
2K
Replies
12
Views
2K
Replies
11
Views
1K
Replies
1
Views
2K
Replies
17
Views
3K
Replies
5
Views
3K
Replies
7
Views
3K
Back
Top