# Urgent Help for homework - sum and difference

• 03-28-2011
Fatal1ty
Urgent Help for homework - sum and difference
I am not good at programming, my teacher gave me to write a program and i have to finish it in 2 days. Here is the program which i have to write

Quote:

Write a program to determine the sum Z=X+Y and the difference Z=X-Y, where X, Y are n-digit integer decimal numbers and n<=100.

When I asked how to do that he explained me and he helped me little. Here is what he wrote. Ofcourse they are only clues about the way.

Z=X+Y
cost nmax=100
int X[nmax]
for(int i=0; i<nmax; i++)
x[i]= rand()%10;
z[i] = X[i]+ Y[i] + c;

Can anyone help me about that program?

Thanks
• 03-28-2011
stahta01
I suggest you try writing a program with what you know.
And, then compile it, link it, and run it.

When, you get the program written post it here and ask for more help.
Note: I do not expect your program to do much; but, your current stuff has typos and would not even compile.
Note: My instructor would want "nmax" to be all upper case "NMAX".

A few good test cases are (for 4 digit numbers 0-3 index in int arrays)
0,0,0,0 + 0,0,0,1
9,9,9,9 + 0,0,0,1
1,0,0,0 - 0,0,0,1

Hint: You will need to add the ones column first just like you learned addition as a child.

Edit: I am a C programmer learning C++; so if you are supposed to use classes, I have little I can do to help.

The line below implies classes/OOP.
Code:

`Z=X+Y`
Tim S.
• 03-28-2011
smokeyangel
Interesting exercise!

Quote:

Originally Posted by Fatal1ty
Code:

```Z=X+Y cost nmax=100 int X[nmax] for(int i=0; i<nmax; i++) x[i]= rand()%10; z[i] = X[i]+ Y[i] + c;```

This looks reasonable... the intention then is that you store each decimal digit in an int in an array. Fair enough. I don't think using rand()%10 is a good way to test initially -- I think the suggested test cases in stahta01's post are a far better idea - start with something small and manageable that you know the answer to!

The task description says integers, so this must include support for negative numbers too, right? I'm not sure of the best way to do that. Maybe have a 101st element that stores the sign.

So you should also try some tests like:
1,0,0,0 - 9,9,9,9
1,0,0,0 + (-9,9,9,9) // should be same
-1,0,0,0 - (-1,0,0,0)

The Z=X+Y syntax does suggest that you need to put this all in a class and implement operator+. If you're not all that comfortable with operator overloading, implement in C style functions first to get the bit-fiddling right. Unless you do something very bizarre, you should just be able to drop your implementation into an overloaded operator+ later.
• 03-28-2011
stahta01
A design decision that needs to be made is does element 0 hold the most significant or least significant digit. I would go with the least significant digit will be in array element zero.

Tim S.