Array math problem - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Array math problem

Lazarus404Lazarus404 Posts: 15Member
Hi All,

I'm having a little math problem (logic really) which goes a little something like this.

I have an array. This array has a known number of items of numerical value. Some of these items are numerical, and some are null. I also have a seperate value which tells me the total sum of these values, whether or not they are null. ie:

Array(null, null, null, null, 10);
Total = 90;

With this, I know that the first four items must equal 80, so I can divide 80 by the number of null items and assign each one the value 20. Simple.

However, theres more. I have a second array. This array can contain items that specify that multiple items may equal a certain amount. ie

Values = Array(null, null, null, null, 10);
modifier = Array();
modifier(0)(80 with length of 3, null, null, null, null); // I've set this up using bitwise values!
modifier(1)(null, null, null, 20 with length of 2, null);
modifier(2)(null, null, 80 with length of 3, null, null);
Total = 100;

With this, I know that the 4th item must be 10 (because that and the next item must equal 20), the third item must equal 60 (because the last two equal 20 and the sum of the last three must equal 80) and the first to must total 20, so I can average them to 10 a piece, meaning I now have an array like this...

Array(10,10,60,10,10); // equals 100!!!

So... My question now is "How do I represent this problem in code?"

Before this is tackled, though, imagine the following example...

Values = Array(null, null, null, null, 50, null, null, null, null);
modifier = Array();
modifier(0)(600 with length of 4, null, null, null, null, null, null, null, null); // I've set this up using bitwise values!
modifier(1)(null, null, null, null, null, null, null, null, null);
modifier(2)(null, 350 with length of 2, null, null, null, null, null, null, null);
modifier(3)(null, null, null, null, null, null, null, null, null);
modifier(4)(null, null, null, null, null, null, null, null, null);
modifier(5)(null, null, null, 350 with length of 5, null, null, null, null, null);
Total = 850;

(equals Array(100, 100, 250, 150, 50, 50, 50, 50, 50); - easy to work out in your head, but how in code?)

I've been trying to tackle this problem for over a week with little joy :-(

Thanks millions in advance,
Hard Up Coder ;-)

Comments

  • CyGuyCyGuy Posts: 312Member
    [b][red]This message was edited by CyGuy at 2005-10-6 6:2:6[/red][/b][hr]
    : Hi All,
    :
    : I'm having a little math problem (logic really) which goes a little something like this.
    :
    : I have an array. This array has a known number of items of numerical value. Some of these items are numerical, and some are null. I also have a seperate value which tells me the total sum of these values, whether or not they are null. ie:
    :
    : Array(null, null, null, null, 10);
    : Total = 90;
    :
    : With this, I know that the first four items must equal 80, so I can divide 80 by the number of null items and assign each one the value 20. Simple.
    :
    : However, theres more. I have a second array. This array can contain items that specify that multiple items may equal a certain amount. ie

    [blue]items? that specify that items??[/blue][code]
    :
    : Values = Array(null, null, null, null, 10);
    : modifier = Array();
    : modifier(0)(80 with length of 3, null, null, null, null); // I've set this up using bitwise values!
    : modifier(1)(null, null, null, 20 with length of 2, null);
    : modifier(2)(null, null, 80 with length of 3, null, null);
    : Total = 100;
    :
    [/code][blue]80 with length of 3?? bitwise values?? is that binary, octal, or hex?? so then 80 is x080 or x800?? or what?? then one more thing... what does the Values array have to do with the rest of the problem??[/blue]

    : With this, I know that the 4th item must be 10 (because that and the next item must equal 20), the third item must equal 60 (because the last two equal 20 and the sum of the last three must equal 80) and the first to must total 20, so I can average them to 10 a piece, meaning I now have an array like this...
    :
    [blue]where do you get these rules?? or better yet which array is the item comming from??[/blue]
    : Array(10,10,60,10,10); // equals 100!!!
    [blue]now what array is this?? I assume it is the imaginary total array... so when adding items, the first items of each array add to equal 10, the second items of each array add to equal 10, the third item of each array add to equal 60, and so on. is that how this works?? or does array position have nothing to do with it?m[/blue]
    :
    : So... My question now is "How do I represent this problem in code?"
    :
    : Before this is tackled, though, imagine the following example...
    :
    : Values = Array(null, null, null, null, 50, null, null, null, null);
    : modifier = Array();
    : modifier(0)(600 with length of 4, null, null, null, null, null, null, null, null); // I've set this up using bitwise values!
    : modifier(1)(null, null, null, null, null, null, null, null, null);
    : modifier(2)(null, 350 with length of 2, null, null, null, null, null, null, null);
    : modifier(3)(null, null, null, null, null, null, null, null, null);
    : modifier(4)(null, null, null, null, null, null, null, null, null);
    : modifier(5)(null, null, null, 350 with length of 5, null, null, null, null, null);
    : Total = 850;
    :
    : (equals Array(100, 100, 250, 150, 50, 50, 50, 50, 50); - easy to work out in your head, but how in code?)
    :
    : I've been trying to tackle this problem for over a week with little joy :-(
    :
    : Thanks millions in advance,
    : Hard Up Coder ;-)
    :
    [blue]dear friend,
    I want to help, but am confused on your description of the problem. I try to point out the confustion, so maybe i can help.[/blue]


  • Lazarus404Lazarus404 Posts: 15Member
    Right, I start with an array of integers. some of the array items may be null. With this array, I know what the sum of these items must add up to, so I can average out the missing items to find their value. Like in my first example:

    Array(null, null, null, null, 10);
    Total = 90;

    I can fill the null values with 20, because I know the 5th item is 10, so...

    20 + 20 + 20 + 20 + 10 = 90 (Total value)

    Ok, that part is easy, but... I have an array with multiple dimensions that store bitwise values. The values tell me the number of items and what that number should be equal to. So, a value of 0x033C will tell me that the next 3 items (includes current item) will be a total of 60. ie

    0x033C = 2 bytes, 1 byte = 3 and one 60.

    each of these group item values span numerous arrays in my multidimensional array... Like in this example:

    Values = Array(null, null, null, null, 10);
    modifier = Array();
    modifier(0)(80 with length of 3, null, null, null, null); // I've set this up using bitwise values!
    modifier(1)(null, null, null, 20 with length of 2, null);
    modifier(2)(null, null, 80 with length of 3, null, null);
    Total = 100;

    As you can see, I have an array of three arrays specifying grouped item totals.

    I hope this makes it clearer. I really do need to find a simple solution. Thanks very much for trying to help me with this.

    Laz
  • CyGuyCyGuy Posts: 312Member
    : Right, I start with an array of integers. some of the array items may be null. With this array, I know what the sum of these items must add up to, so I can average out the missing items to find their value. Like in my first example:
    :
    : Array(null, null, null, null, 10);
    : Total = 90;
    [blue]this array is in decimal[/blue]

    :
    : I can fill the null values with 20, because I know the 5th item is 10, so...
    :
    : 20 + 20 + 20 + 20 + 10 = 90 (Total value)
    [blue]total is given, and all items of this single array add up to total...or 90[/blue]
    :
    : Ok, that part is easy, but... I have an array with multiple dimensions that store bitwise values. The values tell me the number of items and what that number should be equal to. So, a value of 0x033C will tell me that the next 3 items (includes current item) will be a total of 60. ie
    :
    : 0x033C = 2 bytes, 1 byte = 3 and one 60.
    :
    [blue]gotcha understood
    fyi... 0x033C is 4 bytes(double word)
    ...... 0x3C=60 and is 2 bytes aka word
    [/blue]

    : each of these group item values span numerous arrays in my multidimensional array... Like in this example:
    :
    : Values = Array(null, null, null, null, 10);
    : modifier = Array();
    : modifier(0)(80with length of 3, null, null, null, null);
    : modifier(1)(null, null, null, 20 with length of 2, null);
    : modifier(2)(null, null, 80 with length of 3, null, null);
    : Total = 100;
    :
    : As you can see, I have an array of three arrays specifying grouped item totals.

    [blue]okay i see it now. each array does modify the Values array. gotcha, now give me a little bit to find the logic to this problem. and each item in the Values array must add up to Total. Total is given as part of the problem right? what language are you coding in? doesn't matter logic will be the same[/blue]
    :
    : I hope this makes it clearer. I really do need to find a simple solution. Thanks very much for trying to help me with this.
    :
    [blue]much.. now let me see[/blue]
    :
    :
    russ
  • CyGuyCyGuy Posts: 312Member
    [green]Laz, i got the solution to this problem. you need a condition routine that checks the condition of each of your Modifier(), but before that you should have a loader that will load the Modifier() into a conditionCheck object... any given item values in the Value() should be loaded as well.

    After that it is a simple matter of checking every value possible, until the condition routine returns true!!

    [/green]
    yours truely,
    russ

  • Lazarus404Lazarus404 Posts: 15Member
    I sorta have that already (with a few shortcuts here and there to save processing), but I know by looking at it that there must be a much smaller cleaner way. I was hoping someone here could help me find it. Thanks anyway, though, Russ.

    Laz

    : [green]Laz, i got the solution to this problem. you need a condition routine that checks the condition of each of your Modifier(), but before that you should have a loader that will load the Modifier() into a conditionCheck object... any given item values in the Value() should be loaded as well.
    :
    : After that it is a simple matter of checking every value possible, until the condition routine returns true!!
    :
    : [/green]
    : yours truely,
    : russ
    :
    :

  • CyGuyCyGuy Posts: 312Member
    : I sorta have that already (with a few shortcuts here and there to save processing), but I know by looking at it that there must be a much smaller cleaner way. I was hoping someone here could help me find it. Thanks anyway, though, Russ.
    :
    : Laz


    i don't know what you have and what you don't. i do know what you are talking about, but figuring that much logic would take me weeks if not more... perhaps this link may help. i sort of understand; i do not want to inhibit you on your quest any further... good luck

    he talks about adders, complex logic simplification, and hardware here: http://maven.smith.edu/~thiebaut/ArtOfAssembly/CH02/CH02-1.html#HEADING1-0

  • BodkinBodkin Posts: 100Member
    Your problem is of the kind: "Solve n equations containing n unknown values"
    This is best done with a matrix.

    your example:

    [CODE]
    Values = Array(null, null, null, null, 50, null, null, null, null);
    modifier = Array();
    modifier(0)(600 with length of 4, null, null, null, null, null, null, null, null);
    modifier(1)(null, null, null, null, null, null, null, null, null);
    modifier(2)(null, 350 with length of 2, null, null, null, null, null, null, null);
    modifier(3)(null, null, null, null, null, null, null, null, null);
    modifier(4)(null, null, null, null, null, null, null, null, null);
    modifier(5)(null, null, null, 350 with length of 5, null, null, null, null, null);
    Total = 850;
    [/CODE]

    Each line is actually an equation like this:
    A + B + C + D + E + F + G + H + I = Rn

    So the first line (Values) would look like this:
    0A + 0B + 0C + 0D + 1E + 0F + 0G + 0H + 0I = 50

    and the last line (Total=850) would look like this:
    1A + 1B + 1C + 1D + 1E + 1F + 1G + 1H + 1I = 850

    All the lines could be represented with a matrix, but you need to have only ONE multiple item per line and then no extra values.

    [CODE]
    *- -*
    | 0 0 0 0 1 0 0 0 0 | 50 |
    | 1 1 1 1 0 0 0 0 0 | 600 |
    | 0 0 0 0 0 0 0 0 0 | 0 |
    | 0 1 1 0 0 0 0 0 0 | 350 |
    | 0 0 0 0 0 0 0 0 0 | 0 |
    | 0 0 0 0 0 0 0 0 0 | 0 |
    | 0 0 0 1 1 1 1 1 0 | 350 |
    | 1 1 1 1 1 1 1 1 1 | 850 |
    | 0 0 0 0 0 0 0 0 0 | 0 | (added to make the matrix type n x n, but not really important)
    *- -*
    [/CODE]
  • DrMartenDrMarten Posts: 748Member
    [b][red]This message was edited by DrMarten at 2006-2-28 18:12:1[/red][/b][hr]

    Could (equals Array(100, 100, 250, 150, 50, 50, 50, 50, 50); - easy to work out in your head, but how in code?)

    not also be Array(94,94,94,94,94,94,94,94,98) ?

    It still adds up to 850 and is also 9 terms.

    850/9=94.44444444

    Take the integer part as 94.

    I would split the integer off in a string function.
    The CINT function in VISUAL BASIC rounds up if the decimal part is greater than 0.5 so if the sum became 94.5555 for example
    CINT would return 95.

    Do "number of terms-1" so 8*94=752
    850-752=98.

    Would this approach be okay for you?


    Regards,

    Dr M.



  • Lazarus404Lazarus404 Posts: 15Member
    Ahh, I already solved this issue... I built the array as a mock linked list and allowed each item to rely on it's neighbours value, this way, if the neighbours value increases, it's own increases automatically... It's proven more effective than I could have ever thought.

    Thanks for the try, though.

    Lee
  • CyGuyCyGuy Posts: 312Member
    : Ahh, I already solved this issue... I built the array as a mock linked list and allowed each item to rely on it's neighbours value, this way, if the neighbours value increases, it's own increases automatically... It's proven more effective than I could have ever thought.
    :
    : any given item values in the Value() should be loaded as well.

    After that it is a simple matter of checking every value possible, until the condition routine returns true!! Thanks for the try, though.
    :
    : Lee
    :

    [green]WHAT you;see_is_what=you.get;[/green]
    -Russ aka DangeRuss

Sign In or Register to comment.