Mult and Add loop function problem

I'm having a problem with the addition function. I used product as the accumulator, I'm not sure if I should have. I cant figure how to configure the for loop hope you can help
Write a function that accepts two input parameters, computes their product(the result of multiplication),and
returns the result.You are NOT allowed to use the multiplication operator(*)for performing multiplication..
You must implement this as repeated addition in a loop, building up the answer in an accumulator variable.
..with one small exception: you ARE allowed to use it for computing the absolute value of a number
by multiplying it by−1 when the value is negative. You can do all of the addition by
calling the addition function that you created in the first problem.

[code]



#include<iostream>
using namespace std;


double addition(double , double );


int main()
{
double num1; //to store first number
double num2; //to store second number
double product = 0; //to store multiplication

//read numbers
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;

//call function
product = addition(num1, num2);

//print multiplication
cout << "The product of " << num1 << " and " << num2 << " is: " << product << endl;

system("pause");
return 0;
}

//function definition
double addition(double a, double b)
{
//function definition
double addition(double a, double b)

this wont return two negative numbers or two decimal like 3.7 & 4.3
{
double result;
result = 0;
if (b == 1)
result = a;
else
result = a + addition(a, b - 1);
return(result);
}


}

Last edited on
int product( int lhs, int rhs )
  int multiplier = absolute value of lhs
  int result = 0
  add rhs to result multiplier times
  if lhs is negative, then change sign of result
  return result
c++ has an absolute value function. don't try to outsmart it as the problem statement suggests doing.

multiplication can be optimized somewhat. I don't know the best algorithm, but consider

5*10.
you can say 5+5+5+5... 10 times.
or you can compute x = (5+5) once and use that to get y = (5+5)+(5+5) ... and z = 2y and do less total additions, lg(n) ish approach? I am not suggesting this, just observing. It looks recursive... and you need to handle odd numbers with 1 extra addition.

I believe that there is a trick to do it in base 8 or base 16 or base 2 more efficiently as well.
I would have to think about it (or google at it)... I remember creating a multiply circuit in circuits class but that was a very long time ago. Something about a half-adder.
Last edited on
keskiverto I don't understand what this all means what's lhs and rhs ?where does this fit in ?
ok I got this
{
double result;
result = 0;
if (b == 1)
result = a;
else
result = a + addition(a, b - 1);
return(result);
}
but it wont return two negative numbers or two decimal like 3.7 & 4.3
Last edited on
you can't add doubles to multiply them, because fractional part.

10*3.7 is 10 +10+10 + 7 ... how do you get that 7? You can get it using multiplication or division, but that is sort of cheating (?). You can convert them into fractions, but that is not really in the scope of the problem and its aggravating with irrationals etc -- you can't easily do 2 * pi with fractions.

you can do 37*43 and divide by 100, though. This is an example; what it means is managing the decimal point yourself (basically ignore the decimal and put it back after), and its a little crude, but it would "work" for simple stuff anyway. 2*pi .. is still going to be a bit of a pain.

rhs/lhs are just right hand side / left hand side as often stated by math people when talking about equations.

what is the purpose of this exercise, anyway? Its unclear how robust of an answer you need; I am giving you very simple approaches because it 'feels like' you are in the very beginner stages?


Last edited on
jonnin this works I'm just looking for it to work correctly for both inputs being positives, zeroes, or ones I would like it to work correctly for negative numbers in either input position. but negative numbers only work in the first number entered like -9,9 not 9,-9 and zeroes like 0,9 not 9,0

//function definition

double addition(double a, double b)
{
double result;
result = 0;
if (b == 1)
(result = a);
else
result = a + addition(a, (b - 1));
return(result);
}
Last edited on
1
2
3
4
5
6
7
int result = 0;
// add rhs to result multiplier times
for ( int counter=0; counter<multiplier; ++counter ) {
  result = result + rhs;
}
// is equivalent to
result = multiplier * rhs;

You, obviously, won't use + or *.
thanks keskiverto how can I get this to work correctly for negative numbers in either input position ?
Last edited on
If you do look at my earlier post, you should notice that multiplier is the absolute value of lhs.
In other words, my loop computes abs(lhs) * rhs.
IF lhs is not negative
THEN abs(lhs) * rhs == lhs * rhs
ELSE -1 * abs(lhs) * rhs == lhs * rhs

That is math.
Topic archived. No new replies allowed.