Author Topic: Embedding a Color in an Array  (Read 4232 times)

0 Members and 1 Guest are viewing this topic.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Embedding a Color in an Array
« on: June 30, 2020, 03:15:42 pm »
I have two arrays - one with positive numbers the second with negative numbers. All number either + or - have a two digit decimal place already. Each array has been ordered decending. I now want to capture the top 5 of each array and make a new mixed decending ordered array however I still need to know which of the 10 numbers are + or - . I have been trying to use color (ie green for + and red for -) but to do it has turned into a lot of coding using string values. Is there an easier way to mix two colored arrays and keep their original color in the new array? Can't really Dim a color to an array can you? If the numbers weren't already carrying a decimal value, i could have simply added a .1 to the positive values and .2 the negative or even just add a value to one array to help distinguish that array from the other but the value change throws of the calculation routines. I haven't got the string conversion working just yet but it's promising and will keep at it if I have too, but  it is somewhat combersome.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Embedding a Color in an Array
« Reply #1 on: June 30, 2020, 03:40:08 pm »
Why mix the arrays at all?

The positives will descend straight down to the negatives (or vice versa with ascending).

BTW if you want to compare the magnitude of the positive versus the negatives use ABS in the comparison sort.
« Last Edit: June 30, 2020, 03:42:45 pm by bplus »

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Embedding a Color in an Array
« Reply #2 on: June 30, 2020, 06:59:23 pm »
Yes it is the magnitude. The mixed decending order has all the +'s at the top ( ie  5.06 comes before -6.34). Using ABS value would definitely get that 6.34 before the 5.06 but you can't tell which array it orginated from. But if the 5 was green and the ABS(-6) was red, then the origin is very clear. Over time, as the data increases, the wanning/strengthing of the mixed array becomes very apparent when you can easily tell if the mix is weighted towards the + green or - red. Thanks for the suggestion. BTW if you ever write a book on the life and times of a bplus programmer, I'd love to buy a copy.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Embedding a Color in an Array
« Reply #3 on: June 30, 2020, 07:19:15 pm »
Yes it is the magnitude. The mixed decending order has all the +'s at the top ( ie  5.06 comes before -6.34). Using ABS value would definitely get that 6.34 before the 5.06 but you can't tell which array it orginated from. But if the 5 was green and the ABS(-6) was red, then the origin is very clear. Over time, as the data increases, the wanning/strengthing of the mixed array becomes very apparent when you can easily tell if the mix is weighted towards the + green or - red. Thanks for the suggestion. BTW if you ever write a book on the life and times of a bplus programmer, I'd love to buy a copy.

LOL yes, I will spend a whole chapter on this: https://www.qb64.org/forum/index.php?topic=2756.0

Seems to me, with the two arrays already separated you could track theGreatestNegative (tGN) from the one array and theGreatestPositive (tGP) from the other array decide:

do
     if tGN > tGP then
          color Negative: print tGN
          get next greatest negative 'probably need an negative index tracking progress through array
     ELSE
          color Positive : print tGP
          get next greatest positive ' probably need an positive index tracking progress through array
     end if
Loop until all the greatest have been reported

I remember (for the book) at Walter's Forum doing this to merge 10+ separate files into one sorted one, the files were already sorted, oh but I did not track a color for each file source that would be fun!
« Last Edit: June 30, 2020, 07:22:52 pm by bplus »

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Embedding a Color in an Array
« Reply #4 on: June 30, 2020, 07:51:45 pm »
Ya I loved Cobalt's line " not like we have a life".

I was working on a routine similar to the one you are out lining. Couldn't get it to work but perhaps I gave up on it too soon. So rather than creating a 3rd All Mixed Array, just use the two arrays ( the + one and the - one) and in a Do Loop make the ABS value comparisons, write the results to the screen with the appropriate color. Maybe write the results with color to a file rather than an array.

I'm do'n the COBALT.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Embedding a Color in an Array
« Reply #5 on: June 30, 2020, 09:06:17 pm »
Yes many ways to do this sort of thing, depends how data comes and what you want to do with results.

I could write a code example but it would be quicker maybe to try to fix your code.

hmm... Mr I Got Nolife is wondering now just how fast he could whip it up...

I'm going to check a lottery program first.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Embedding a Color in an Array
« Reply #6 on: June 30, 2020, 11:06:28 pm »
;-)
Code: QB64: [Select]
  1. REDIM greens(0), reds(0)
  2. FOR i = 1 TO 20
  3.     g = INT(RND * 75 * 100) / 100 'reds will be tops for awhile
  4.     loadSort g, greens()
  5.     r = INT(RND * 100 * 100) / 100
  6.     loadSort r, reds()
  7. FOR i = 1 TO 20
  8.     COLOR 10
  9.     PRINT greens(i),
  10.     COLOR 12
  11.     PRINT reds(i)
  12. gI = 1: rI = 1
  13. gG = greens(gI): gR = reds(rI): row = 1: col = 40
  14.     IF gG > gR THEN
  15.         COLOR 10
  16.         LOCATE row, col: PRINT gG
  17.         row = row + 1: gI = gI + 1: gG = greens(gI)
  18.     ELSE
  19.         COLOR 12
  20.         LOCATE row, col: PRINT gR
  21.         row = row + 1: rI = rI + 1: gR = reds(rI)
  22.     END IF
  23.     IF row = 21 THEN EXIT DO
  24.  
  25.  
  26. SUB loadSort (insertN, dynArr()) ' note this leaves dynArr(0) empty! so ubound of array is also number of items in list
  27.     DIM ub, j, k
  28.     ub = UBOUND(dynArr) + 1
  29.     REDIM _PRESERVE dynArr(LBOUND(dynArr) TO ub)
  30.     FOR j = 1 TO ub - 1
  31.         IF insertN > dynArr(j) THEN '  GT to LT according to descending or ascending sort
  32.             FOR k = ub TO j + 1 STEP -1
  33.                 dynArr(k) = dynArr(k - 1)
  34.             NEXT
  35.             EXIT FOR
  36.         END IF
  37.     NEXT
  38.     dynArr(j) = insertN
  39.  
  40.  
« Last Edit: June 30, 2020, 11:08:37 pm by bplus »

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Embedding a Color in an Array
« Reply #7 on: July 01, 2020, 08:48:39 am »
Wow - how the hell did you come up with that so quickly? I worked on it well into the late evening and only just found your algorythm a minute ago. Honestly, I just ran your code and can't believe it actually works, I'm going to have to study it to understand why it works but I am extremely impressed.

So the moral is - if the joy is in the journey then I'm still having a wonderful time.

That you again.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Embedding a Color in an Array
« Reply #8 on: July 01, 2020, 11:03:20 am »
Hi @Dimster

Thanks, I like your morals ;-))

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Embedding a Color in an Array
« Reply #9 on: July 02, 2020, 04:46:22 pm »
Just remember to breathe and smile.
Granted after becoming radioactive I only have a half-life!