Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need a for loop

Status
Not open for further replies.

choppysuey

Technical User
Mar 28, 2001
54
US
Hi all, could someone give me a hand?
Due to my lack of c++ programming skills I'm having trouble creating a nested for loop that would walk through a 6x6 2-D array in the following manner:

element[0][0]
element[0][1]
element[1][0]
element[1][1]
element[0][2]
element[0][3]
element[1][2]
element[1][3]
element[0][4]
element[0][5]
element[1][4]
element[1][5]
element[2][0]
element[2][1]
element[3][0]
element[3][1]
etc....




 
for(int indexOne = 0;indexOne<6;indexOne++)
{
for(int indexTwo = 0;indexTwo<6;indexTwo++)
{
element[indexOne][indexTwo]
}
}

Viola

Matt
 
Thx, but the loop had to iterate throught the 2-D array in the exact manner as I originally posted. Your loop would go from [0][0] - [0][5] and then move on to the second row.
 
#include <iostream.h>

int main()
{
int ione, itwo;
int element[6][6];


for(ione = 0; ione < 2; ione++) {
for(itwo = 0; itwo < 2; itwo++) {
element[ione][itwo];
cout << ione << itwo << endl;
}
}

for(ione = 0; ione < 2; ione++) {
for(itwo = 2; itwo < 4; itwo++) {
element[ione][itwo];
cout << ione << itwo << endl;
}
}

for(ione = 0; ione < 2; ione++) {
for(itwo = 4; itwo < 6; itwo++) {
element[ione][itwo];
cout << ione << itwo << endl;
}
}

for(ione = 2; ione < 4; ione++) {
for(itwo = 0; itwo < 2; itwo++) {
element[ione][itwo];
cout << ione << itwo << endl;
}
}

for(ione = 2; ione < 4; ione++) {
for(itwo = 2; itwo < 4; itwo++) {
element[ione][itwo];
cout << ione << itwo << endl;
}
}

for(ione = 2; ione < 4; ione++) {
for(itwo = 4; itwo < 6; itwo++) {
element[ione][itwo];
cout << ione << itwo << endl;
}
}

for(ione = 4; ione < 6; ione++) {
for(itwo = 0; itwo < 2; itwo++) {
element[ione][itwo];
cout << ione << itwo << endl;
}
}

for(ione = 4; ione < 6; ione++) {
for(itwo = 2; itwo < 4; itwo++) {
element[ione][itwo];
cout << ione << itwo << endl;
}
}

for(ione = 4; ione < 6; ione++) {
for(itwo = 4; itwo < 6; itwo++) {
element[ione][itwo];
cout << ione << itwo << endl;
}
}

return 0;
}

That will work I just didn't know how to back up in the array. Recursion maybe? Hope that will help you.

-CDudd
 
Thx for the post. I was hoping for an &quot;elegant&quot; nested loop but I couldn't figure it out. Maybe it can't be done elegantly. As for recursion, that's a little to advanced for me at this point. Thx again.
 
Just curious why'd you need to search through it like that anyway?

-CDudd
 
Sorry... i didnt see the order the first time... try this


void printOut(int a[][6],int idx,int sum)
{

int x,y,n;

x = idx;
y = idx+1;
for(int i = 0;i<6;i+=2)
{
n= i;
cout<<a[x][n]<<&quot; &quot;;
n = i+1;

if(n<6)
cout<<a[x][n]<<&quot; &quot;;
n=i;
if(y<6)
cout<<a[y][n]<<&quot; &quot;;
n=i+1;
if(n<6 && y<6)
cout<<a[y][n]<<endl;
}

//cout<<a[idx][5]<<endl;

}

void printOut(int a[][6],int idx)
{
for(int i = 0;i<idx;i+=2)
printOut(a,i,i+5);
}


And test it with this for easy debugging... i think this is what you need

int a[6][6] = {
{1,2,3,4,5,6},
{7,8,9,10,11,12},
{13,14,15,16,17,18},
{19,20,21,22,23,24},
{25,26,27,28,29,30},
{31,32,33,34,35,36}
};

printOut(a,5);

Matt
 
My original implementation required sum but i dont think it is necessary now... here was my reasoning

A pattern exists in what you are doing.. when idx is zero the sum of the 2 indices follow the sequence

0,1,1,2,2,3,3,4,4,5

when idx is 2 you get

2,3,3,4,4,5,5,6,6,7

so a pattern does exist, you just need to look for it. Another thought was you were counting in base 6 but that fell through.

Matt
 
Here are the elegant nested loops you need:

for (int m = 0; m<=2 ; m++)
for (int n = 0; n<=2; n++)
for (int i=0; i<=1; i++)
for (int j=0; j<=1; j++)
printf(&quot;%d %d \n&quot;, 2*m + i, 2*n + j);
 
Thx everyone for your time. I've learned a great deal. That loop that Jeffray posted was very slick and serves my purpose very.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top