Problem Solving Issue!

Hey first post here, ive lurked for information here before in regards to this problem but to no avail. I also asked a good friend/ really talented programmer to help me and he pointed me in the right direction but even he was at a loss for words as to what my instructor wanted me to do (my instructor is genuinely terrible). I'm not asking you to write the code for me, but it'd be really great if anyone had any additional information.
I know the readme at the beginning said no homework, but THIS IS NOT YOUR AVERAGE HOMEWORK so i would really appreciate some advice on what to do here:

"Produce a package where all the arithmetic functions are written in a non-recursive fashion, that is, iteratively using while loops. A non-recursive version of addition called plus is shown below."

int plus(int x, int y)
{
while ( y != 0 )
{ x = add1(x); y = sub1(y); }
return x;
}



From what my friend told me, it seems as though this is less of a programming problem as it is a problem solving/ logic problem. In his words, it seems like my instructor wants us to define the actual arithmetic used. Basically, we are supposed to define the functions of addition, subtraction, etc. He helped me out and wrote this for me:

int plus1(int x, int y) {
while(y != 0){
x++;
y--;
}
return x;
}

and it helped me grasp what my instructor might be talking about, but im still confused.
There are formulas attached to the document that we are supposed to code as well, for instance: x + (y / z) % x.


So from my understanding, my professor wants us to define the addition, subtraction, multiplication, division, and modulus functions above main (even though these operants are already built in the framework????), and then somehow incorporate assigned formulas in the main function. Also, i have already programmed each of these functions in a previous assignment without having to use any type of loops and it took like ten minutes, whereas this has taken me a week to even have any sort of idea what to do.
I wrote this small section of code for the previous assignment, first (but same) formula:
E = userNumY / userNumZ;
E = E % userNumX;
E = E + userNumX;


Again, im sorry if im breaking the rules of the forum here posting homework, but im in genuine need of help and my instructor is just not delivering, his lectures have literally not even brushed on the topic of programming. Ive watched tons of videos and i cannot find anything that even comes close to explaining what im supposed to be doing here.
Last edited on
I'm not sure why all of this matters so much, but if you want iteration, the sample is okay as far as it goes.

However, the add1() and sub1() functions each only send one parameter - so I think it should be something like:

1
2
function add1(number)
return number +1


1
2
function sub1(number)
return number -1


you should be able to take those two snippets of pseudocode, rewrite them in c style, and build on them to make all of the required arithmetic functions work.

for example

1
2
3
4
5
6
7
function subtract( x, y)

do while (y > 0)
     x = sub1(x)
     y = sub1(y)
end do
return x


1
2
3
4
5
6
7
8
9
function multiply(x,y)

z = 0

do while (y > 0)
      x = plus(z,x)
      y = sub1(y)
end do
return z


1
2
3
4
5
6
7
8
9
10
function divide(x,y)

z = 0

do while (x > y)
      x = subtract(x,y)
      z = add1(z)
end do
// x is remainder or (x % y)
return z



I think maybe his purpose is to demonstrate that all math problems can be reduced to adding or subtracting in the correct manner.
Last edited on
Topic archived. No new replies allowed.