Thread: Loop

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    17

    Loop

    Is there any way not to exit a loop until I finsh all the index in that certain loop befor I go to the other one, example I have this code:
    [cod]

    #include<iostream>
    #include <climits>

    using namespace std;


    int main(){

    int c,i,r;
    int b[3][1][5]={{0,1,2},{0},{0,1,2,3,4}};


    for (c=0;c<5;c++){


    r=0;

    for( i=0;i<3;i++){// Here I do not want to exit this loop until I go three times on" i"


    cout<<b[i][r][c]<<endl;

    }

    }


    return 0;

    }


    [/code]


    To be clearer I want my loop to show [0][0][0] not exit the "i" loop and show[1][0][0]then [2][0][0] after this one it exit and goes to the other loop and becomes[0][0][1],[1][0][1],[2][0][1]and now exit again and so on so in every time the last index is not change unless the loop turn three times.I appreciate the reply and the help.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    So you are wanting to output the indices not the contents of b right? Because you talk about [0][0][1] and such but yet you are indexing into the b array in your example. I tweaked your code to get it to display the indices. Is this what you intended?

    Code:
    #include<iostream>
    #include <climits>
    
    using namespace std;
    
    
    int main() {
    
      int c,i,r;
      int b[3][1][5]={{0,1,2},{0},{0,1,2,3,4}};
    
      r = 0;
    
      for (c=0;c<3;c++) {    
        for( i=0;i<5;i++) {
          cout<< c << r << i << endl;
        }
        cout << endl;
      }
    
      return 0;
    }
    I switched the order of your for loops for starters and then just displayed the index variables.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    17
    Thank you for the reply first. I do not want just to show the index. but I have a 3 dim array and I want to fill it in the following order [0][0][0],[1][0][0],[2][0][0]
    [0][0][1],[1][0][1],[2][0][1]
    [0][0][2],[1][0][2],[2][0][2]
    [0][0][3],[1][0][3],[2][0][3]
    [0][0][4],[1][0][4],[2][0][4]

    as you can see this array has 3 raws and 1 raw and 5 columns.
    so I want to fill the first 3 rows from each colums before I move to second one I hope I'm not confusing you, just let me know if you need more clarification. I want just to grab the idea of how to read the loop three times without changing the last 2 indices in every time of loop. Thanks.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Oh I think I see now. So you want to fill them in as such:

    000
    001
    002

    100
    101
    102

    .
    .
    .

    Right? Well then your loop is correct. All you need to do is assign values to the array. To prove the output is correct try making your for loop this:

    Code:
      for (c=0;c<5;c++){
        for( i=0;i<3;i++){// Here I do not want to exit this loop until I go three times on" i"
          cout<< i << r << c << " "; 
        }
    
        cout << endl;
      }
    If this still is not what you mean, then I'm confused. Let me know.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    17
    YES you are very very close. I did like the loop you posted but it gave different result. I will tell you what I want and ver easier way:

    when c is = 0
    then i is =012

    when c is 1 then
    i is 012
    c is 2
    is is 012
    and so on

    and I did this loop
    [/cod]

    for (c=0;c<5;c++){


    r=0;

    for( i=0;i<3;i++){


    cout<<b[i][r][c]<<endl;
    }



    }
    [/code]

    but it gave differen result like this:000,101,202,003,004

    and what I wanted is 000,100,200
    001,101,201
    002,102,202
    and so on so you see the last one is changing only every 3 times. Thank you very much for trying to help.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    17
    Hey MrWizard
    you are very right I checked your code and it is exactly what I wanted but the problem here is I need to fill those index in the same order.is that possible? I do not know why when I try to show them usingb[i][r][c] the do not appear like the same way when I use cout<<i<<r<<c;.Thanks.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by Absy
    Hey MrWizard
    you are very right I checked your code and it is exactly what I wanted but the problem here is I need to fill those index in the same order.is that possible? I do not know why when I try to show them usingb[i][r][c] the do not appear like the same way when I use cout<<i<<r<<c;.Thanks.
    *sighs*

    Something like this???

    Code:
    #include <iostream>
    using namespace std;
    
    void FillArray( int A[3][1][5] )
    {
      int i, r = 0, c, num = 0;
    
      for (c=0;c<5;c++){
        for( i=0;i<3;i++){
          A[i][r][c] = num++;
        }
      }
    }
    
    void DisplayArray( int A[3][1][5] )
    {
      int i, r = 0, c;
    
      cout << "Values in the array:" << endl;
    
      for (c=0;c<5;c++){
        for( i=0;i<3;i++){
          cout << A[i][r][c] << " ";
        }
    
        cout << endl;
      }
    }
    
    int main( void )
    {
      int b[3][1][5];
      
      FillArray( b );
      DisplayArray( b );
    
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM