English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

How would I add and subtract elements in an array? I want to add two elements that are in an array, take those elements out of the array, and add in the sum of those two elements into the array (so I take out 2 things from the array and add in one new one).

I've already tried google and the java sun forums to no avail. Any help would be greatly appreciated.

2007-02-24 16:03:55 · 4 answers · asked by Galbadian 2 in Computers & Internet Programming & Design

Thanks BK. I already tried that. Here's a little example. Let's say we have the array { 1, 3, 6, 7}. I want to sum up the two lowest (1 and 3) , delete the two lowest from the array (1 and 3), and then put that sum back into the array. So in this case I'd end up with {4,6,7}. If I keep going I'll get {10,7}, then {17}

2007-02-24 16:15:59 · update #1

4 answers

If you are doing this with a sorted array (low to high) it is easy....

1.) determine the number of elements in the array as you are going to loop through each element. Using a For/next where idx = 1 to maxIndex (Notice I am not starting at 0)

2.) read the value in the 0 element (lowest value) into a variable
X = ay(0) do this before entering the loop

3.) Start your for next loop. As you loop through each element (1 to max)you are going move its value into the element just below it

ay (idx-1) = ay(idx)

But as you do this movement you add the value of X which before entering the loop was set to ay(0)

ay(idx-1) = ay(idx) + X

since we only want to combine values in the first two elements
you only need to add the value stored in X just on the first add.

you can either set X = 0 after the first assignment OR use an if statement to perform the addition just once when the idx = 1

4.) after exiting the loop you will need to redim the array since everything has been copied down one position. You will have a duplicate of the last element

so redim to max-1

2007-02-25 01:46:23 · answer #1 · answered by MarkG 7 · 0 0

initially you're beginning with an array of four positions. we are going to call this A. A(a million) is a million A(2) is 3 A(3) is 6 and A(4) is 7. Your software might choose the equivalent of here: B = a million initiate loop C = A(a million) + A(B+a million) A(a million) = C If B = 4 go out loop. B = B + a million end loop i think of you're able to have the means to stick to this actual. Any language could have categories of loop and as a result you're able to could make minor differences. solid luck. end loop

2016-11-25 21:50:13 · answer #2 · answered by schaner 4 · 0 0

it's just simple.
e.g.
a[0] = 100
a[1] = 200

int sum = a[0]+a[1]; //to add two elements and store in a variable

a[0] = sum; // now, a[0] has the sum
a[1] = 0; // now, a[1] has nothing.

hope u r looking for a solution something like this....

2007-02-24 16:10:25 · answer #3 · answered by bk 2 · 0 0

It depends on the programming language you are using.

2007-02-24 16:10:19 · answer #4 · answered by Anonymous · 0 0

fedest.com, questions and answers