Thread: !urgent need help with writiing loop

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    44

    !urgent need help with writiing loop

    say i have an array of size 4

    and this is what i want to to...
    tmp[0][0].id is initialized to [0].id

    if tmp[0][0].id = a[1].id
    tmp[0][1].id = a[1].id
    if it isnt true go to next statement
    tmp[1][0].id = a[1].id
    then next loop

    if tmp[0][0].id = a[2].id
    tmp[0][2].id = a[2].id
    if it isnt true
    if tmp[1][0].id = a[2].id
    tmp[1][1].id = a[2].id
    if it isnt true
    tmp[2][0].id = a[2].id
    then next loop

    if tmp[0][0].id = a[3].id
    tmp[0][3].id = a[3].id
    if it isnt true
    if tmp[1][0].id = a[3].id
    tmp[1][2].id = a[3].id
    if it isnt true
    if tmp[2][0].id = a[3].id
    tmp[2][1].id = a[3].id
    if it isnt true
    tmp[3][0].id = a[3].id

    how would i go about doing this for an array with size N?

    this is the idea for the code i have so far... wrote on paper syntax is not correct
    y is the size of the array
    Code:
    int z = 1;
    for(int x=0;x<y;x++)
    {
         for(z;z<y;z++)
         {
            if(tmp[x][x].id = a[z].id;
             tmp[x][z].id = a[z].id;
          }
        else
        tmp[z][x].id = a[z].id
        }
    }
    Last edited by loso44x; 11-16-2005 at 06:40 PM.

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    I have no idea what you are trying to express with your [x][x] type of syntax. Is that a two dimensional array, or some other thing that you are making up for the purposes of psudocode? And are you trying to do a comparison in the if statement, or is that an assignment? And in your for loop, why are you trying to increment z in the initializer? I know you said it wasn't valid c++ code, but come on. How is anyone supposed to understand what you mean without reading your mind?
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    44
    Quote Originally Posted by IfYouSaySo
    I have no idea what you are trying to express with your [x][x] type of syntax. Is that a two dimensional array, or some other thing that you are making up for the purposes of psudocode? And are you trying to do a comparison in the if statement, or is that an assignment? And in your for loop, why are you trying to increment z in the initializer? I know you said it wasn't valid c++ code, but come on. How is anyone supposed to understand what you mean without reading your mind?
    [x][x] is two dimension
    sry i revised my thread is it understandable?

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    first of all equality comparisons requires the == operator.

    so if(tmp[x][x].id = a[z].id; is not valid

    if(tmp[x][x].id == a[z].id)

    It's hard as I don't really know what you want to do

  5. #5
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    What is this .id?

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    44
    id is an int variable of a class - a[0].id thru a[n].id is alrdy filled with product ids and tmp[0][0].id is initialized to a[0]...what i want to do is check to see if each product id is equal in the array if it is equal, the loop would store each equal value in tmp[0][1].id tmp[0][2].id .. etc.. and another diff id would go in tmp[1][0].id tmp[1][1].id ..

    for example a[0].id= 12 a[1].id=13 a[2].id=12 a[3].id=12
    so...
    tmp[0][0].id = 12
    tmp[0][1].id = 12
    tmp[0][2].id = 12

    tmp[1][0] = 13
    Last edited by loso44x; 11-16-2005 at 07:30 PM.

  7. #7
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Well, to start, your if's are wrong.

    Code:
    =
    sets things equal to each other.
    Code:
    ==
    check to see if they are equal. Nor do you have your closing ) on your if statement.

    Other than that...it is kinda of confusing what you are trying to do in what pattern exactly. Perhaps if you tried to explain it a little better?

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It appears you are looking for the proper algorithm, as opposed to technical details.

    One question I have is whether you start over from the beginning after a number didn't match. For example, how would the following input be added:

    a[0] = 2;
    a[1] = 5;
    a[2] = 2;
    a[3] = 3;

    Edit.. never mind, you answered that above. Sorry.
    Last edited by Daved; 11-16-2005 at 07:39 PM.

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    44
    Quote Originally Posted by Daved
    It appears you are looking for the proper algorithm, as opposed to technical details.

    One question I have is whether you start over from the beginning after a number didn't match. For example, how would the following input be added:

    a[0] = 2;
    a[1] = 5;
    a[2] = 2;
    a[3] = 3;

    tmp[0][0].id = 2
    tmp[0][1].id = 2

    tmp[1][0].id = 5

    tmp[2][0].id = 3

    is there an easier way to do this?

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Sorry, just noticed you already answered that, but it doesn't exactly match your initial post.

    I assume that the values that are not set are already defaulted to some number (like 0 or -1) that cannot be an actual value for the id.

    If that is the case, then for each id, you'd start at tmp[0][0]. If it matches, then you loop through tmp[0][j] until it is not used (it has that initial value 0 or -1) and then place it there. If tmp[0][0] doesn't match, you move on to tmp[1][0] and repeat. Continue until tmp[i][0] has the inital value (0 or -1), meaning it hasn't been used yet. At that point set it to the current value.

    That algorithm can be done in two simple loops, where you have a done variable that allows you to break out of the two loops when you actually findthe right location. A third loop can be used to cycle through the a array.

  11. #11
    Registered User
    Join Date
    Oct 2005
    Posts
    44
    Quote Originally Posted by Daved
    Sorry, just noticed you already answered that, but it doesn't exactly match your initial post.

    I assume that the values that are not set are already defaulted to some number (like 0 or -1) that cannot be an actual value for the id.

    If that is the case, then for each id, you'd start at tmp[0][0]. If it matches, then you loop through tmp[0][j] until it is not used (it has that initial value 0 or -1) and then place it there. If tmp[0][0] doesn't match, you move on to tmp[1][0] and repeat. Continue until tmp[i][0] has the inital value (0 or -1), meaning it hasn't been used yet. At that point set it to the current value.

    That algorithm can be done in two simple loops, where you have a done variable that allows you to break out of the two loops when you actually findthe right location. A third loop can be used to cycle through the a array.
    i wrote it but something is working incorrectly it stores again at diff locations

    here is the code


    y is size of STORE1
    Code:
    tmp[0][0].custnum = STORE1[0].custnum;
    
         for(int j=1;j<y;j++) {
                 for(int k=0;k<row;k++) {
                         if(STORE1[j].custnum == tmp[k][0].custnum) {
                                              for(int i=0;i<col;i++) {
                                                      if(tmp[k][i].custnum == 0) {
                                                          tmp[k][i].custnum = STORE1[j].custnum;
                                                          break;
                                                      }
                 if(tmp[k][0].custnum == 0) {
                     tmp[k][0].custnum = STORE1[j].custnum;
                     break;
                 }
                                              }
                         }
                 
                 }
         }
    here is the output

    Code:
    [0][0]:10212
    [0][1]:10212
    [0][2]:10212
    [0][3]:0
    [0][4]:0
    [1][0]:13114
    [1][1]:0
    [1][2]:0
    [1][3]:0
    [1][4]:0
    [2][0]:11234
    [2][1]:0
    [2][2]:0
    [2][3]:0
    [2][4]:0
    [3][0]:10212
    [3][1]:10212
    [3][2]:0
    [3][3]:0
    [3][4]:0
    [4][0]:10212
    [4][1]:0
    [4][2]:0
    [4][3]:0
    [4][4]:0
    Last edited by loso44x; 11-17-2005 at 02:29 AM.

  12. #12
    Registered User
    Join Date
    Oct 2005
    Posts
    44
    any1 know whats wrong?

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Code:
    tmp[0][0].custnum = STORE1[0].custnum;
    
    for(int j=1;j<y;j++)
    {
        for(int k=0;k<row;k++)
        {
            if(STORE1[j].custnum == tmp[k][0].custnum)
            {
                for(int i=0;i<col;i++)
                {
                    if(tmp[k][i].custnum == 0)
                    {
                        tmp[k][i].custnum = STORE1[j].custnum;
                        break;
                    }
                    if(tmp[k][0].custnum == 0)
                    {
                        tmp[k][0].custnum = STORE1[j].custnum;
                        break;
                    }
                }
            }
        }
    }
    I didn't change your code, I just modified the indentation so that you could see what was happening. I'm not sure how that code gave you that output (what does the input look like?), but as you can see if you follow the code, it won't follow the algorithm the way you want it to.

    The first problem I see is that when you find the location to store the variable, you call break, but that only breaks out of one loop. You need a separate variable that you set to false at the beginning of the outer loop, and then set to true when you've stored the current value from STORE1. You then need to check that variable in both inner loops and break if it is true so that you start again with the next value from STORE1.

    The second problem is the if statement in between the second and third loop. I don't think it ends where you want it to, since all the code that could add the value is inside there and will never get called if you don't have a match already in there. Maybe moving the closing brace for that and using an else might help.

    Overall it is looking very good and I think you are close.

  14. #14
    Registered User
    Join Date
    Oct 2005
    Posts
    44
    Quote Originally Posted by Daved
    Code:
    tmp[0][0].custnum = STORE1[0].custnum;
    
    for(int j=1;j<y;j++)
    {
        for(int k=0;k<row;k++)
        {
            if(STORE1[j].custnum == tmp[k][0].custnum)
            {
                for(int i=0;i<col;i++)
                {
                    if(tmp[k][i].custnum == 0)
                    {
                        tmp[k][i].custnum = STORE1[j].custnum;
                        break;
                    }
                    if(tmp[k][0].custnum == 0)
                    {
                        tmp[k][0].custnum = STORE1[j].custnum;
                        break;
                    }
                }
            }
        }
    }
    I didn't change your code, I just modified the indentation so that you could see what was happening. I'm not sure how that code gave you that output (what does the input look like?), but as you can see if you follow the code, it won't follow the algorithm the way you want it to.

    The first problem I see is that when you find the location to store the variable, you call break, but that only breaks out of one loop. You need a separate variable that you set to false at the beginning of the outer loop, and then set to true when you've stored the current value from STORE1. You then need to check that variable in both inner loops and break if it is true so that you start again with the next value from STORE1.

    The second problem is the if statement in between the second and third loop. I don't think it ends where you want it to, since all the code that could add the value is inside there and will never get called if you don't have a match already in there. Maybe moving the closing brace for that and using an else might help.

    Overall it is looking very good and I think you are close.

    okay the input looks like this

    STORE1[0].custnum = 10212
    STORE1[1].custnum = 11234
    STORE1[2].custnum = 10212
    STORE1[3].custnum = 10212
    STORE1[4].custnum = 13114

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM