12 Replies - 4328 Views - Last Post: 19 March 2012 - 06:00 PM Rate Topic: -----

#1 Javacodes  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 07-March 12

how to add two one-dimensional arrays in java?

Posted 07 March 2012 - 06:48 PM

how to add two one-dimensional arrays in java?
for (int i=0; i < arr.length; i++)
sum=arr[i] + arr1[i]; 


i used that codes. but when i enter three elements 9 9 9 then 9 9 9 i got 18 18 18.
how can i add them? The answer must 1 9 9 8. not 18 18 18. T_T

This post has been edited by Atli: 07 March 2012 - 07:11 PM
Reason for edit:: Added [code] tags.

Is This A Good Question/Topic? 0
  • +

Replies To: how to add two one-dimensional arrays in java?

#2 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: how to add two one-dimensional arrays in java?

Posted 07 March 2012 - 07:03 PM

So, each element is a digit, right? If you had the value 99, what would the first 9 represent? I'll give you a hint: The second nine represents: (9 * 10^0), where 10^0 is 10 to the 0th power, or just 1. It might help you to visualize it as 90 + 9.
Was This Post Helpful? 1
  • +
  • -

#3 Javacodes  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 07-March 12

Re: how to add two one-dimensional arrays in java?

Posted 07 March 2012 - 07:05 PM

View PostSheph, on 07 March 2012 - 07:03 PM, said:

So, each element is a digit, right? If you had the value 99, what would the first 9 represent? I'll give you a hint: The second nine represents: (9 * 10^0), where 10^0 is 10 to the 0th power, or just 1. It might help you to visualize it as 90 + 9.

Yes. What's the ^ represent for?
Was This Post Helpful? 0
  • +
  • -

#4 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: how to add two one-dimensional arrays in java?

Posted 07 March 2012 - 07:09 PM

I was using it here to mean the power function, as I can't do a superscript. 10^1 = 10 raised to the 1st power. In the forum text only... in Java, ^ will not produce the same results.

This post has been edited by Sheph: 07 March 2012 - 07:09 PM

Was This Post Helpful? 0
  • +
  • -

#5 Javacodes  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 07-March 12

Re: how to add two one-dimensional arrays in java?

Posted 07 March 2012 - 07:16 PM

View PostSheph, on 07 March 2012 - 07:09 PM, said:

I was using it here to mean the power function, as I can't do a superscript. 10^1 = 10 raised to the 1st power. In the forum text only... in Java, ^ will not produce the same results.


Ahh. Thank you Sir. How many for loops i'll use?
Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3710
  • View blog
  • Posts: 5,958
  • Joined: 08-June 10

Re: how to add two one-dimensional arrays in java?

Posted 07 March 2012 - 07:21 PM

View PostSheph, on 08 March 2012 - 02:09 AM, said:

as I can't do a superscript.

Yes you can: 9 * 100

Just use the Posted Image button, or do: 9 * 10[​sup]0[​/sup]
Was This Post Helpful? 1
  • +
  • -

#7 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: how to add two one-dimensional arrays in java?

Posted 07 March 2012 - 07:22 PM

That depends on what algorithm you decide to use. I asked you to consider what the digits mean, and how to get the value for a single digit. Have you done that yet? Why don't you write a method that transforms just 1 array into an integer that you can do something with? For example an array like {2, 4, 6} would become 246. (200 + 40 + 6) From there it would be easy as cake:

int sum = myMethod(arr) + myMethod(arr1);

Edit: @Atli Thank you, I did not know that. How foolish of me to overlook that. It is right there! :)

This post has been edited by Sheph: 07 March 2012 - 07:24 PM

Was This Post Helpful? 3
  • +
  • -

#8 Javacodes  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 07-March 12

Re: how to add two one-dimensional arrays in java?

Posted 07 March 2012 - 07:24 PM

View PostSheph, on 07 March 2012 - 07:22 PM, said:

That depends on what algorithm you decide to use. I asked you to consider what the digits mean, and how to get the value for a single digit. Have you done that yet? Why don't you write a method that transforms just 1 array into an integer that you can do something with? For example an array like {2, 4, 6} would become 246. (200 + 40 + 6) From there it would be easy as cake:

int sum = myMethod(arr) + myMethod(arr1);

Thank you so much Sir. I'll try to do it. Thank you for the time. :)
Was This Post Helpful? 0
  • +
  • -

#9 Javacodes  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 07-March 12

Re: how to add two one-dimensional arrays in java?

Posted 12 March 2012 - 06:21 PM

View PostJavacodes, on 07 March 2012 - 07:24 PM, said:

View PostSheph, on 07 March 2012 - 07:22 PM, said:

That depends on what algorithm you decide to use. I asked you to consider what the digits mean, and how to get the value for a single digit. Have you done that yet? Why don't you write a method that transforms just 1 array into an integer that you can do something with? For example an array like {2, 4, 6} would become 246. (200 + 40 + 6) From there it would be easy as cake:

int sum = myMethod(arr) + myMethod(arr1);

Thank you so much Sir. I'll try to do it. Thank you for the time. :)


Sir. I had a problem with my output. I got the answer. By using this code.

for( int x = 2; x < array.length; ++x) {
total = arr1[x-2] * 100 + arr1[x-1] * 10 + arr1[x] * 1;
System.out.println(" " + total + " ")
}
and the output is 1988.
i need 1 9 8 8.
How can i put space with each digit?
Was This Post Helpful? 0
  • +
  • -

#10 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: how to add two one-dimensional arrays in java?

Posted 13 March 2012 - 02:20 AM

How about another method, to take a String, and modify it to put a space between each char?
String stringWithSpaces(String str) { ... }

// Do you know how to use Integer.toString?
int myInt = 1998;
String myString = Integer.toString( myInt );
System.out.println( myString );

Was This Post Helpful? 0
  • +
  • -

#11 Javacodes  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 07-March 12

Re: how to add two one-dimensional arrays in java?

Posted 13 March 2012 - 05:05 PM

yeah. i know how to string it. But the challenge is not to use string. Is there any problem to my codes?
Was This Post Helpful? 0
  • +
  • -

#12 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: how to add two one-dimensional arrays in java?

Posted 13 March 2012 - 06:53 PM

What you have to realize is that you have data that is an integer. That data shouldn't change from the way you display it. Today you might want to show it with spaces in between the characters. Tomorrow you might want to subtract one from it and show it. The second one would be very hard to do if you inherently had spaces IN your data. The best way here is to transform your data (the int) into a representable form, and modify the representation accordingly, instead of modifying the int, which you can't do in this case.
Was This Post Helpful? 1
  • +
  • -

#13 Javacodes  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 07-March 12

Re: how to add two one-dimensional arrays in java?

Posted 19 March 2012 - 06:00 PM

View PostSheph, on 13 March 2012 - 06:53 PM, said:

What you have to realize is that you have data that is an integer. That data shouldn't change from the way you display it. Today you might want to show it with spaces in between the characters. Tomorrow you might want to subtract one from it and show it. The second one would be very hard to do if you inherently had spaces IN your data. The best way here is to transform your data (the int) into a representable form, and modify the representation accordingly, instead of modifying the int, which you can't do in this case.


Uhmm. I dont get it. i cant use string.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1