× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



John McKay wrote:
As far as I can see, the code in the answer does not add the values together.
...
C specs
sorta acc;
for x = 1 to 3;
y = *zeros;
z = x + 1;
y = %lookup(acc(x): acc: z);
if y > *zeros;
sum$(x) += sum$(y);
sum$(y) = *zeros;
acc(y) = *blanks;
endif;
endfor;


John, I think your solution would work for the exact data that David showed, but I don't think it would work if there were more than two values the same. Say the acc values were A B B B, then it would add sum(3) to sum(2), but it should also add sum(4) to sum(2).

But also, I don't think it would work more than once, since it would have blank "unused" acc values that would get sorted to the beginning of the array.

Here's my attempt at a solution, completely untested ...

Since the number of "real" elements in the array is not fixed (starts with 4 elements, ends up with 3 elements), we need a variable to keep track, say num_elems.

// sort the array by acc. If there is more than one element
// for any one acc, they will now be together in the array
sorta %subarr(acc : 1 : num_elems);

// merge the sum values for elements with the same acc value
i = 1;
remove_elems = 0;
dow i < num_elems;
// loop through the subsequent elements with the same
// acc value, and add all the sums to sum(i)
for j = (i + 1) to num_elems;
if acc(j) = acc(i);
sum(i) += sum(j);
// now that sum(j) is added to sum(i)
// we want to get rid of element j
// (but just mark it to be removed later)
acc(j) = *hival;
remove_elems += 1;
else;
// we have a new acc
leave;
endif;
// continue with the element after the
// last one we merged
i = j;
endfor;
enddo;

// re-sort the array to put the discarded ones
// at the end (they were set to *hival),
// then adjust the number of actual elements
sorta %subarr(acc : 1 : num_elems);
num_elems -= remove_elems;


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.