Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ARRAYs
#1
Hello All,

I am working on a project that I think an ARRAY would be perfect for but I am unsure of how to set it up properly. Here is an example of what I am working with.

I have an input of "Monthly Benefit" and I need to convert it to a "Daily Benefit".

MB / 30 Days = DB

Monthly Benefit (MB) = $5000
Daily Benefit (DB) = $166.666666667

The trouble is in the software that I am programing in QuickMacros only accepts a Daily Benefit in whole 10 dollars increments. In other words, when you divide $5000 MB by 30 days you get an odd number that does not match with the accepted input in the software. I think using an ARRAY with 2 dimensions would allow me to use the calculation of DB act as a pointer value to the element in the 1 dimension in the array which then points to the associated 2 dimension in the array. See reference below.

Monthly Benefit----Dimension 1-----Dimension 2
4800-----------------160---------------160
4900-----------------163.333333------160
5000-----------------166.666666------170
5100-----------------170---------------170

I can easly create this list in an excel file but I don't know how to create it in QuickMacros and how to use the array to look up the proper value. Can someone give me an example of how this might work?

Thanks in advance!

Paul Daugs
#2
How about a function to compute just the "Daily Benefit"?

Macro Macro52
Code:
Copy      Help
double MB=4500
out
rep 10
,out "MB = %.0f, DB = %d" MB sub.getDB(MB)
,MB+100

#sub getDB
function double'MB

int DB
double Result=MB/30
;out "Daily Benefit = %.0f" Result
str s.format("%.0f" Result)
int r=val(s)%10
if(r<5) DB=val(s)-r
else DB=val(s)+(10-r)
ret DB
#3
Macro Macro2485
Code:
Copy      Help
;2-dim array
ARRAY(double) a
a.create(3 2) ;;3 columns, 2 rows
;row 0
a[0 0]=4800
a[1 0]=160
a[2 0]=160
;row 1
a[0 1]=4900
a[1 1]=163.333333
a[2 1]=160
;resizing example
a.redim(3) ;;3 rows
;row 2
a[0 2]=5000
a[1 2]=166.666666
a[2 2]=170
;__________________________

int i
for i 0 a.len
,out F"{a[0 i]} {a[1 i]} {a[2 i]}"

To set a 2-dim array easier, can be used CSV
Macro Macro2483
Code:
Copy      Help
str csv=
;4800, 160, 160
;4900, 163.333333, 160
;5000, 166.666666, 170
ICsv x._create
x.FromString(csv)
ARRAY(str) a
x.ToArray(a)
;__________________________

int i
for i 0 a.len
,out F"{a[0 i]} {a[1 i]} {a[2 i]}"

;note: the array is str, not double. If need numeric values, use val to convert.
for i 0 a.len
,double d0 d1 d2
,d0=val(a[0 i] 2)
,d1=val(a[1 i] 2)
,d2=val(a[2 i] 2)
,out F"{d0} {d1} {d2}"
#4
Hello lionking & Gintaras,

Two really great solutions to my problem! Now I guess the question is which one will process faster?

Gintaras,

Also, I was wondering if I could use the csv option you listed by pulling in a csv file I created and saved. In other words, I put the full chart of values into a csv file and then use a version of your code to pull that data into the array created in the code.

As always I really apprecaite all the help form the QM community!

Best Regard,

Paul Daugs
#5
The array method is much faster especially if you know ahead the full chart of values.
#6
replace FromString to FromFile.

To measure code speed, use functions PerfFirst, PerfNext and PerfOut.
#7
Hello lionking,

I was wondering if you could help me understand a line of the code you provided to compute the daily benefit. I am trying to understand the line below.

Code:
Copy      Help
str s.format("%.0f" Result)

What I don't understand is the
Code:
Copy      Help
"%.0f"
. I have been reading the help section on the format function but I don't see this format being mentioned in the help file. Can you help understand what this format operation is doing so I have a better understanding of what is taking place in the function you wrote?

Paul
#8
Macro Macro62
Code:
Copy      Help
double Result=1.2345
str s.format("%.0f" Result)
out s
"%.0f" is to get rid off the decimal part of the double number.
#9
read here
http://www.quickmacros.com/help/Tables/IDP_PRINTF.html
click the links for each part of the syntax for more details

http://www.quickmacros.com/help/Tables/ ... _TYPE.html
http://www.quickmacros.com/help/Tables/ ... FLAGS.html
http://www.quickmacros.com/help/Tables/ ... WIDTH.html
http://www.quickmacros.com/help/Tables/ ... ISION.html
http://www.quickmacros.com/help/Tables/ ... _SIZE.html


should explain things better
It is also in QM help file
click index tab search or look for format fields.
#10
Hello Kevin and Lionking,

Thanks for the help on this I was trying to find the details in the help file but I just couldn't seem to find it. This really helps for programming in the future!

Paul Daugs


Forum Jump:


Users browsing this thread: 2 Guest(s)