# Subtract arrays

mike cool

Greenhorn

Posts: 24

posted 11 years ago

hi all

i have created 2 int array both have the same size of 15... i don't have to fill it all in ...

so it can be half of the array filt in with number and the rest will be zero's by default...

so each element can have only one digit

example:

int[] arr1 = {1,2,3,4,6,8,5}

int[] arr2 ={1,0,0,2,3,2}

int[] subtract = new int[15];

now i want to subtract arr2 from arr1 and add the resault in subtract array

this array can also hold one digit only in each of its elements.

the size of this new array can be 15 and more...

what i did is i pushed all elements value of arr1 and arr2 to the end of the arrays, and i start add the result also at the end of subtract array

so the will look like this :

arr1 = {0,0,0,0,0,0,0,0,1,2,3,4,6,8,5}

arr2 = {0,0,0,0,0,0,0,0,0,1,0,0,2,3,2}

it works fine but i have 2 problems

1- it works fine only if the result will be (+value) value greater than or equal 0sorry i don't know the english name of this .. that mean it works when arr1 - arr2 >=0

i couldn't find a way to give the result if result value less than zero ,,

2-as you see i push all number to the end of the array how can i

remove the zero's from the left side of first number when i print them ??

here is the method i used to subtract

i have created 2 int array both have the same size of 15... i don't have to fill it all in ...

so it can be half of the array filt in with number and the rest will be zero's by default...

so each element can have only one digit

example:

int[] arr1 = {1,2,3,4,6,8,5}

int[] arr2 ={1,0,0,2,3,2}

int[] subtract = new int[15];

now i want to subtract arr2 from arr1 and add the resault in subtract array

this array can also hold one digit only in each of its elements.

the size of this new array can be 15 and more...

what i did is i pushed all elements value of arr1 and arr2 to the end of the arrays, and i start add the result also at the end of subtract array

so the will look like this :

arr1 = {0,0,0,0,0,0,0,0,1,2,3,4,6,8,5}

arr2 = {0,0,0,0,0,0,0,0,0,1,0,0,2,3,2}

it works fine but i have 2 problems

1- it works fine only if the result will be (+value) value greater than or equal 0sorry i don't know the english name of this .. that mean it works when arr1 - arr2 >=0

i couldn't find a way to give the result if result value less than zero ,,

2-as you see i push all number to the end of the array how can i

remove the zero's from the left side of first number when i print them ??

here is the method i used to subtract

Layne Lund

Ranch Hand

Posts: 3061

posted 11 years ago

1) a number greater than zero is called "positive".

2) You need to decide how to represent negative numbers. How will you tell that the elements of the array stand for a negative number? Unless your teacher told you specifically, you have to decide this yourself.

3) Once you decide how to represent a negative number, you may want to go back and revisit your addition method so that it can add both positive and negative numbers. If you do this, you can reuse your add method without rewriting basically the same code. You do this by changing the second argument to a negative number then calling the add method.

4) Did you figure out how to print the results of your add method without the zeros? I suggest you make a method that takes an int array and prints it out. How do you go about printing each element and ignoring it if it is a zero? (Hint: try to think of what control structures you need to use to do this.) I'm sorry, but I don't think it will be helpful to tell you exactly how to do this. It will be much better if you can figure it out on your own. Perhaps you can start by writing the steps you need to do this in your own native language. If you do this, you don't have to worry about Java syntax. You can just explain how to do it in a way that you can understand without Java getting in the way.

I hope this helps.

Layne

2) You need to decide how to represent negative numbers. How will you tell that the elements of the array stand for a negative number? Unless your teacher told you specifically, you have to decide this yourself.

3) Once you decide how to represent a negative number, you may want to go back and revisit your addition method so that it can add both positive and negative numbers. If you do this, you can reuse your add method without rewriting basically the same code. You do this by changing the second argument to a negative number then calling the add method.

4) Did you figure out how to print the results of your add method without the zeros? I suggest you make a method that takes an int array and prints it out. How do you go about printing each element and ignoring it if it is a zero? (Hint: try to think of what control structures you need to use to do this.) I'm sorry, but I don't think it will be helpful to tell you exactly how to do this. It will be much better if you can figure it out on your own. Perhaps you can start by writing the steps you need to do this in your own native language. If you do this, you don't have to worry about Java syntax. You can just explain how to do it in a way that you can understand without Java getting in the way.

I hope this helps.

Layne

mike cool

Greenhorn

Posts: 24

Jay Albano

Greenhorn

Posts: 21

posted 11 years ago

For printing only nonZero integers, as Layne said, you may use control structures/statements to do it... (additional HINT: you may use selection or iteration statements - depends on the scenario).

Still, Layne's advice is very much effective. Maybe something's wrong with your algorithm/translation of idea or whatever you might call it. A little review might help.

I hopwe this helps. GOD bless!

Still, Layne's advice is very much effective. Maybe something's wrong with your algorithm/translation of idea or whatever you might call it. A little review might help.

I hopwe this helps. GOD bless!

Trust in the Lord with all thine heart and lean not unto thine own understanding, <br />in all thy ways acknowledge Him and He shall direct thy path.<br /> <br />blue jay�