• Post Reply Bookmark Topic Watch Topic
  • New Topic

subtract arrays  RSS feed

 
shawin azize
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two array each is one dimension,


A=[0.5, 0.2];
B=[0.1,0.07];

i would like to produce an array in java
which is result of subtracting array A from B.
c = [0.4 , 0.13];
how can we do that in java ?

the code which i used is:



 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi shawin, welcome to the Ranch.

Have you tried a for loop? Give it a try and post your code.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your first step is to explain how you'd do it in English (or whatever is your native language). Pretend you are talking to a young child. Give them step by step instructions on what they should do to get the result you want.

Once you can explain it like that, programming is much easier. You can then see the pieces you'd need to code. So do that, then post what you have here and we can continue the discussion.
 
Jimm Jones
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shawin,

The declaration

needs to be before the for loop not inside it. Instead of null, initialize the array, like C = new double[A.length]
The println statement should similarly be outside the loop.

You might also want to follow the convention that any names
that you introduce that are not reference types or constant variables
start with a lowercase character. That way people reading your code
will have an easier time following it.
 
praveen roy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Initialize the array before the loop :
double [] C = new double[2];
for ( int i=0; i<A.length;i ++){
// receiving error when iam putting the subtract result in array


C[i] = A[i]-B[i];

System.out.println(Arrays.toString(C));

}

This should work. This is not the best practice but it would suffice your requirement right now.
 
Carey Brown
Bartender
Posts: 2998
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not:

Use this:

 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi praveen, welcome to the Ranch.

Be sure to surround your code with code tags when you post code. It makes reading it much easier.
 
Liutauras Vilda
Marshal
Posts: 4650
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a short remark - when I read your subject "subtract arrays" I got a wrong idea what is your task about. Somehow I thought you need to find the difference between the two arrays in terms of elements which are in.

But it appears you need to subtract arrays elements one from another.

Be careful when choosing subject description or actual problem's description. It is important to get it right, otherwise you could get the advices which are not the ones you were expecting
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!