mZ Posted May 7, 2008 at 04:40 AM Report Share #183729 Posted May 7, 2008 at 04:40 AM boas pessoal! Tenho uma duvidazita... Alguém sabe fazer um for para percorrer um array bidimensional de uma ponta a outra na diagonal? se for possível deixem ai um exemplo que eu depois desenrasco-me... tks 🙂 Link to comment Share on other sites More sharing options...
Knitter Posted May 7, 2008 at 09:55 AM Report Share #183755 Posted May 7, 2008 at 09:55 AM Se estivermos a falar de uma matriz quadrada então o for seguinte percorre a diagonal principal. int[][] array = {{0, 1, 2}, {3, 4, 5},{6, 7, 8}}; for(int z = 0; z < array.length; z++) { System.out.println(array[z][z]); } www.sergiolopes.eu Link to comment Share on other sites More sharing options...
mZ Posted May 7, 2008 at 12:18 PM Author Report Share #183764 Posted May 7, 2008 at 12:18 PM sim é uma matriz quadrada... mas o objectivo era ele percorrer todas as diagonais possíveis, começar numa ponta e acabar noutra... assim ele vai só à diagonal do meio... o array tem este aspecto: http://img507.imageshack.us/img507/7710/arraytg8.jpg e o que é pedido é passar naquelas linhas de cor na diagonal...e depois ao contrário mas isso e mudar valores... tks 🙂 Link to comment Share on other sites More sharing options...
RA Posted May 8, 2008 at 12:58 PM Report Share #183968 Posted May 8, 2008 at 12:58 PM Se bem percebi o problema, penso que seja isto (a não ser que me tenha escapado alguma burrice minha ?): int[][] array = {{0, 1, 2}, {3, 4, 5},{6, 7, 8}}; int coluna = 1; int linha = 0; // testes: limites inferiores, superiores e se ou linha == 0 ou coluna == 0 (para ter uma diagonal valida) int fim; if (linha != 0) fim = array.length - linha; else fim = array.length - coluna; for(int z = 0; z < fim; z++) { System.out.println(array[z+linha][z+coluna]); } Link to comment Share on other sites More sharing options...
mZ Posted May 8, 2008 at 01:57 PM Author Report Share #183977 Posted May 8, 2008 at 01:57 PM neige esse ciclo vai só fazer uma diagonal e depois para... o objectivo e fazer todas as diagonais do array como ta na figura... acho que vai ser mais que um for... tens alguma ideia como se faz? Link to comment Share on other sites More sharing options...
RA Posted May 8, 2008 at 04:32 PM Report Share #183998 Posted May 8, 2008 at 04:32 PM Oi! A ideia é tu mudares os valores das variáveis linha e coluna. Ou seja, esse ciclo faz a diagonal que começa em [linha, coluna] = [1, 0].. Funciona para as outras diagonais. Basta mudares esses valores. Por exemplo, [linha, coluna] = [2, 0], ou [linha, coluna] = [0, 1]. Era esta a minha ideia... Mas lá está, não sei é se percebi bem a questão! 😄 Edit: Penso que já percebi a situação! Queres fazer a listagem completa da matriz, mas listando diagonal a diagonal, certo? Link to comment Share on other sites More sharing options...
mZ Posted May 8, 2008 at 07:13 PM Author Report Share #184035 Posted May 8, 2008 at 07:13 PM hmm eu vou tentar mudar os valores para que passe em todas as diagonais... sim a ideia era essa... vamos ver se consigo com este for! obrigado 😄 Link to comment Share on other sites More sharing options...
DanielAmorim Posted May 9, 2008 at 09:08 AM Report Share #184111 Posted May 9, 2008 at 09:08 AM Este é um modo possível: int[][] array = {{0, 1, 2, 3}, {1, 0, 1, 2}, {2, 1, 0, 1}, {3, 2, 1, 0}}; for(int i = 0 ; i < array.length ; ++i){ System.out.print("\nDiagonal[" + i + "][0] : "); for(int j = 0 ; j < array.length - i ; ++j){ System.out.print(array[j][j+i] + " "); } if(i!=0){ System.out.print("\nDiagonal[0][" + i + "] : "); for(int j = 0 ; j < array.length - i ; ++j){ System.out.print(array[j][j+i] + " "); } } } Resultado: Diagonal[0][0] : 0 0 0 0 Diagonal[1][0] : 1 1 1 Diagonal[0][1] : 1 1 1 Diagonal[2][0] : 2 2 Diagonal[0][2] : 2 2 Diagonal[3][0] : 3 Diagonal[0][3] : 3 Podes retirar um ciclo for em função do problema em questão, eu como queria mostrar na consola dos valores da diagonal teve de ser assim. Daniel Amorim VP for xRTML http://www.xrtml.org http://www.realtime.co Link to comment Share on other sites More sharing options...
mZ Posted May 9, 2008 at 12:37 PM Author Report Share #184147 Posted May 9, 2008 at 12:37 PM oi gnomoamorim ta ai qualquer coisa a falhar... tenta mudar os valores do array tem qualquer coisa a bater mal... Link to comment Share on other sites More sharing options...
DanielAmorim Posted May 9, 2008 at 03:29 PM Report Share #184178 Posted May 9, 2008 at 03:29 PM A mim parece-me bem ? Pensando de outro modo, uma matriz 4x4 tem estas posições: (0,0) (0,1) (0,2) (0,3) (1,0) (1,1) (1,2) (1,3) (2,0) (2,1) (2,2) (2,3) (3,0) (3,1) (3,2) (3,3) Mudando só o output: int[][] array = {{0, 1, 2, 3}, {1, 0, 1, 2}, {2, 1, 0, 1}, {3, 2, 1, 0}}; for(int i = 0 ; i < array.length ; ++i){ System.out.print("\nDiagonal : "); for(int j = 0; j < array.length - i ; ++j){ System.out.print("([" + j + "," + (j+i) + "])"); } if(i!=0){ System.out.print("\nDiagonal : "); for(int j = 0 ; j < array.length - i ; ++j){ System.out.print("([" + (j+i) + "," + j + "])"); } } } Dá isto: Diagonal : ([0,0])([1,1])([2,2])([3,3])Diagonal : ([0,1])([1,2])([2,3]) Diagonal : ([1,0])([2,1])([3,2]) Diagonal : ([0,2])([1,3]) Diagonal : ([2,0])([3,1]) Diagonal : ([0,3]) Diagonal : ([3,0]) Vê lá se não é o que querias 😄 Daniel Amorim VP for xRTML http://www.xrtml.org http://www.realtime.co Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now