CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Hybrid View

  1. #1
    Join Date
    Feb 2012
    Posts
    16

    Arrow Writing a complex loop

    hi,

    there's an array of integers with a known length (about 10 - 20 elements) and some initiated values (between 0 to 20). i want to loop through its values and modify it one by one.

    as a sample, suppose that this array has 4 elements and each element is set to number 3. i would like to get something like this:

    3 3 3 3
    3 3 3 2
    3 3 3 1
    3 3 3 0
    3 3 2 3
    3 3 2 2
    .
    .
    .
    3 1 0 3
    3 1 0 2
    3 1 0 1
    3 1 0 0
    3 0 3 3
    3 0 3 2
    .
    .
    .
    0 0 0 3
    0 0 0 2
    0 0 0 1
    0 0 0 0

    thanks for your help,
    Sean

  2. #2
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    70

    Re: Writing a complex loop

    Quote Originally Posted by sean.aulason View Post
    hi,

    there's an array of integers with a known length (about 10 - 20 elements) and some initiated values (between 0 to 20). i want to loop through its values and modify it one by one.

    as a sample, suppose that this array has 4 elements and each element is set to number 3. i would like to get something like this:

    3 3 3 3
    3 3 3 2
    3 3 3 1
    3 3 3 0
    3 3 2 3
    3 3 2 2
    .
    .
    .
    3 1 0 3
    3 1 0 2
    3 1 0 1
    3 1 0 0
    3 0 3 3
    3 0 3 2
    .
    .
    .
    0 0 0 3
    0 0 0 2
    0 0 0 1
    0 0 0 0

    thanks for your help,
    Sean
    I am not completely sure what you are trying to accomplish. Maybe you could post a better description of what exactly it is you are trying to accomplish and in what way. From what I can tell the program changes values depending on their value and location but you start with 24 values and end with 16.

    For now what I can recommend is to take a look at these things:
    -Multidimensial Array
    -For loop
    -Nested For loop

    Also If and Else if statements since your example looks like it is doing different things depending on the value of the number and the position.

  3. #3
    Join Date
    Dec 2008
    Posts
    144

    Re: Writing a complex loop

    This looks suspiciously like a homework question to me.

    Please tell us what you have tried and we can help, or we can provide a little guidance, but we won't give you the answer straight out.

    This is a question you can easily solve with some nested loops and a little ingenuity.
    Code:
    if (Issue.Resolved)
    {
         ThreadTools.Click();
         MarkThreadResolved();
    }

  4. #4
    Join Date
    Feb 2012
    Posts
    16

    Re: Writing a complex loop

    it's not a homework. i'm trying to write a program for myself so i can find the write answer for my tests.

    i have an array with 20 elements and each elements represents an index from 0 to 20 which is produced in another function.

    the biggest possible index will be calculated and every element in this array will be filled with that value.

    now i want to get all possible combinations (from 0 to the biggest index for each element separately), so i can find the correct combination of values.

    as an example, we suppose that MyArray is an array with 4 elements and the biggest possible index is set to 9. so the array is like this: { 9, 9, 9, 9 }

    now i would like to reduce the value of the first element one by one from 9 to 0 and this combination will be processed:
    { 9, 9, 9, 9 } -> { 9, 9, 9, 8 } -> . . . -> { 9, 9, 9, 0 }

    when MyArray[0] reaches zero, the second element should be reduced by one and once again the first element changes from 9 to 0:
    { 9, 9, 8, 9 } -> { 9, 9, 8, 8 } -> . . . -> { 9, 9, 8, 0 }

    so we will have this as the next calculation:
    { 9, 9, 7, 9 } -> { 9, 9, 7, 8 } -> . . . -> { 9, 9, 7, 0 }

    and it will continue for the next elements as well:
    { 9, 8, 9, 9 } -> { 9, 8, 9, 8 } -> . . . -> { 9, 8, 9, 0 } -> { 9, 8, 8, 9 } -> . . . -> { 9, 8, 8, 0 }

    and this loop should continue until it reaches to zero for all elements:
    { 0, 0, 1, 9 } -> { 0, 0, 1, 8 } -> . . . -> { 0, 0, 1, 0} -> { 0, 0, 0, 9 } -> . . . -> {0, 0, 0, 0}

    with each combination i have to put these indexes in another array and see if it gets me the correct answer or not.

    i tried this loop but it's not working:
    Code:
                        for (int j = 0; j <= iNumberOfElements; j++)
                        {
                            for (int k = j - 1; k >= 0; k--)
                            {
                                for (int l = i - 1; l >= 0; l--)
                                {
                                    aiPlanes[k] = l;
    
                                    //--- This is just to see the result in a list box
                                    string sTest = "";
                                    for (int b = 0; b < iNumberOfElements; b++)
                                    {
                                        sTest += aiPlanes[b].ToString() + ' ';
                                    }
                                    lstResult.Items.Add(sTest);
                                    //---
                                }
                                aiPlanes[k] = i - 1;
                            }
                        }
                    }
    the result IS NOT what i want... it gives me something like this:
    { 9, 9, 9, 9 } -> { 9, 9, 9, 8 } -> . . . -> { 9, 9, 9, 0 } -> { 9, 9, 8, 9 } -> { 9, 9, 7, 9 } -> . . .


    from what i see, i have to make a nested loop for each element in my array and it sounds impossible. is there any way to make it more clean and better.

    thanks in advance

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Writing a complex loop

    I'm not really clear on what you are trying to do? If you are just trying to display the info and the numbers in question will be single digits then you could
    step through the array in a loop
    convert each element to a string and concantonate to the previous element string value
    Convert the resulting string to a number
    In another loop
    Print the number formated with leading 0s up to the max number of characters.
    subtract 1 from the number
    repeat
    Always use [code][/code] tags when posting code.

  6. #6
    Join Date
    Feb 2012
    Posts
    16

    Re: Writing a complex loop

    thanks for your reply.

    no, i'm not trying to print the numbers on the screen. as i mentioned in my code, it's just a list box to show me the progress so i can realize the result is what i want to get or not.

    let's make it clear...

    i have an array with different values. let's call it "MyMainArray".

    now i would like to get a combination of MyMainArray values to reach my goal which is a number (say 63879). ofcourse this combination should be made by a specific number of elements. (say 20 elements of MyMainArray)

    so i declared another array which represents an index. let's call this one "MyArray"

    in my case, i have to start from the biggest value (MyMainArray.Length - 1). so i filled the MyArray values with this number.

    now i have to loop through all combinations to see which one is equivalent to my goal.

    so i have to loop through all indexes one by one and put these indexes in MyMainArray and get the sum of these values.

    anyway, i worked on it and modified the code like this one, but it doesn't work properly:
    Code:
                        int iCurrentRow = 0;
                        int iTempRow    = 0;
    
                        do
                        {
                            if (aiPlanes[iCurrentRow] == 0)
                            {
                                iCurrentRow++;
                            }
    
                            for (int j = iCurrentRow - 1; j >= 0; j--)
                            {
                                aiPlanes[j] = i - 1;
                            }
    
                            aiPlanes[iCurrentRow]--;
                            iTempRow = 0;
    
                            do
                            {
                                if (aiPlanes[iTempRow] == 0)
                                {
                                    iTempRow++;
                                    if (iTempRow >= iCurrentRow) { MessageBox.Show(iTempRow.ToString()); break; }
                                }
    
                                for (int j = iTempRow - 1; j >= 0; j--)
                                {
                                    aiPlanes[j] = i - 1;
                                }
    
                                if (iTempRow < iCurrentRow) aiPlanes[iTempRow]--;
    
                                for (int j = 0; j <= iTempRow; j++)
                                {
                                    for (int k = i - 1; k >= 0; k--)
                                    {
                                        aiPlanes[j] = k;
    
                                        string bobo = "";
                                        for (int b = 0; b < iHalfPlanes; b++)
                                        {
                                            bobo += aiPlanes[b].ToString() + ' ';
                                        }
                                        lstResult.Items.Add(bobo);
                                        lstResult.SelectedIndex = lstResult.Items.Count - 1;
                                    }
                                }
                            } while (iTempRow <= iCurrentRow);
                        } while (iCurrentRow <= iHalfPlanes);

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Writing a complex loop

    Your description is not clear enough to really know what you are trying to accomplish.

    now i would like to get a combination of MyMainArray values to reach my goal which is a number (say 63879). ofcourse this combination should be made by a specific number of elements. (say 20 elements of MyMainArray)
    This is very cryptic, no way to guess what you are trying to do here. Be specific about what you need to actually do. What operations would be done to the elements to reach this target? If you are trying to figure out what elements to use why not start with the target number and then have the code figure out what size and values you need? Of course this can not be done without knowing what you will do to these elements to reach your target and since you have not told us it is hard to help.

    You initial example looks as though it is simply counting down from 9999 to 0000 in steps of 1. A very complicated way of doing it but the resulting output would be exactly the same?
    Always use [code][/code] tags when posting code.

  8. #8
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    70

    Re: Writing a complex loop

    As stated by DataMiser your post are not clear one what you wish to accomplish. There are many different ways of accomplishing things and when it is not clear what your goal is people here can keep trying to help you but get nowhere.

    Your initial post looks like a simple counter in which an int could be used so I have some questions.
    -Is there a reason you need to use an array?
    -Does each position in the array have a rule? (Ex. testArray[0] = 1 * Value, testArray[1] = 2 * Value, testArray[2] = 4 * Value, testArray[3] = 8 * Value)
    -What exactly is the purpose of the application? You say to find the match to the mainArray but for what reason? There could be a more efficient way of doing this.

    If you have to use array with the limited information you provide I can think of a way to do this by:
    - Initializing an int Array
    - Creating a for loop
    - Breaking counter value into separate chars and assigning each into a position in the testArray
    - Check testArray against mainArray (If all values match break out of loop)

    Does not seem like the most efficient way of doing it but would get the job done or at least what seems like is your goal.

  9. #9
    Join Date
    Feb 2012
    Posts
    16

    Re: Writing a complex loop

    since it's too complicated, i guess i have to write down some parts of my code. it may even be a simpler way of accomplishing this (i hope)

    this is my array:
    Code:
    static readonly integer[] PLANE = { 1, 7, 19, 37, 61, 91, 127, 169, 217, 271, 331, 397, 469,
                                        547, 631, 721, 817, 919, 1027, 1141 };
    each plane contains a specific number of components (the above numbers). so A1 has 1 component, A2 has 7 components and so on.

    i have 11 planes. one of them should be the biggest one. i get the value of the minimum number for the biggest plane in another procedure. in this case, it's the 10th element of the above array.

    now i have to place smaller planes in other slots. they should be smaller than the first one.

    so i have to loop through these values to see by which combination of planes i'll get ,say, 3000 components. it's obvious that i'll get more than 1 answer and that's what i want.

    so i have to replace each plane in each slot one by one and calculate the number of components each time.

    this is what i want...

    but what i did till now...

    i created another array of integers (called aiPlanes - as you can see in my code snippets) to keep the index of each plane (as working with indexes is way easier than the number of components) and filled this array with the number 9. (remember that my biggest plane was the 10th element of PLANES array)

    so i have something like this:
    { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }

    the rest of what i want is described above. i want to reduce each element from 9 to 0 one by one so i get all the possible combinations.

    thanks for your time

  10. #10
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Writing a complex loop

    Again you can get all those possible combinations by simply taking all the elements to form a string then convert the resulting string to a number and loop through subtracting 1 each pass. This is not complicated at all.

    Still you are spending a lot of time telling us how you are trying to do it rather than the why it needs to be done. What are these numbers going to be used for?

    From what I have saw so far the only things that are complicated here is the way you are trying to explain your problem and the code you are trying to write. The problem seems simple.
    Always use [code][/code] tags when posting code.

  11. #11
    Join Date
    Feb 2012
    Posts
    16

    Re: Writing a complex loop

    thanks, but if these numbers have just one digit, your solutions works. what if i have two digits for each element?

    can you please add a few lines of code as well.

  12. #12
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Writing a complex loop

    If you could find a way to explain what you want to do maybe we could help.

    All of your examples so far show single digits.

    Now you do realize that if you want to display all possible values in the manner you described from an array of 20 elements each with a value that could be 10 or more that your loop is going to take a very very long time just to list the results and it is going to be far to much for any user to absorb from looking at it.

    Just to do this
    9, 9, 9, 9, 9, 9, 9, 9, 9, 9
    would take longer than most people would be willing to wait and that is if your loop is fast and simple. It is after all going to take 10 billion cycles of the loop
    Always use [code][/code] tags when posting code.

  13. #13
    Join Date
    Feb 2012
    Posts
    16

    Re: Writing a complex loop

    I don't know why i should explain more to get help. Let me start another question and forget everyhing i said.

    This is my question:

    I have an array with 10 elements like this:
    Code:
    {11, 11, 11, 11, 11, 11, 11, 11, 11, 11 }
    now i want to write a loop in which the first element (MyArray[0]) goes from 11 to 0 and when it reaches 0, the next element's value (MyArray[1]) is reduced by one and so on.

    Please help with with this loop.

  14. #14
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Writing a complex loop

    The reason you should explain more is so maybe someone can understand what you are trying to accomplish.

    As to the new question If I understand what you are asking it would be a series of nested loops with the first element being at the deepest portion stepping out to the last element. Seems to be oppostite of what you were doing before but still can't see why you would be doing it and it is going to take a while to process with your example numbers.
    Last edited by DataMiser; February 17th, 2012 at 11:54 PM.
    Always use [code][/code] tags when posting code.

  15. #15
    Join Date
    Feb 2012
    Posts
    16

    Re: Writing a complex loop

    Ok. I know that i have to write 'n' nested loops and n is the number of elements in my array.

    That's exactly what i'm trying to do. But is there any way to make it clean?
    i really hate to have 11 nested loops.

    Would you please write a simple example?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured