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).

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}

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

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

I can't just add up all of the elements because I'm doing Huffman Coding...which means each time I add I need to assign a 0 or 1 to the highest and lowest number.

2007-02-24 16:23:34 · update #1

4 answers

Personally I would recommend doing that one iteratively, it wouldn't put as much of a load on system resources. But, if you're new to programming, this might be easier to do as a recursive algorithm.

If you're doing this in VB, you can use the for....each loop to step thru the array, which might be easiest since the indices don't step at regular intervals.

If not, I would recommend populating the array with whole number indices such as
x(1) = 1, x(2) = 3, x(3) = 6, x(4) = 7

So here's basically how you'd do it recursively:

Function crunch(array x())
if size(x) < 2 then crunch = x(1)
else
{
newArray = x
newArray.deleteMember(smallest) 'you'd have to calculate which element is the smallest
crunch = x(1) + crunch(newArray)
}
End function

That's the basic idea.

2007-02-24 16:25:10 · answer #1 · answered by I Know Nuttin 5 · 0 0

First of all you are starting with an array of 4 positions. we'll call this A. A(1) is 1 A(2) is 3 A(3) is 6 and A(4) is 7. Your program would need the equivalent of the following:
B = 1
Start loop
C = A(1) + A(B+1)
A(1) = C
If B = 4 exit loop.
B = B + 1
end loop
I think you should be able to follow this easily. Any language will have different types of loop and therefore you may have to make minor changes.
Good luck.
end loop

2007-02-24 16:28:16 · answer #2 · answered by HelpingHand 2 · 0 0

ok first off you cant really REMOVE elements from an array, you can only replace their values, or even write them to null. Anyway I wrote a program for one of my programming classes that is somewhat similar to what your talking about. The way I did it was I took 2 temp variables to keep the lowest number and low number in, then I did a sequential search though the array (inside a dynamic for loop) and replaced values that were lower than either of them and swapped the values as necessary., THEN after the loop had reached the sential value set, I did my calculations.

Your looking for an algorithm, not code.

Remember young grasshopper..."Patience, Discipline" = Greatness

I miss World of Warcraft
/sniff

2007-02-24 16:21:56 · answer #3 · answered by D 4 · 0 0

Why can't you just add up all the numbers at once?

2007-02-24 16:21:16 · answer #4 · answered by phrozen_wolf 2 · 1 0

fedest.com, questions and answers