• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

subtract arrays

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:



 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not:

Use this:

 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic