alves077 Posted December 7, 2012 at 12:28 AM Report #486097 Posted December 7, 2012 at 12:28 AM Boa noite, É possivel ter um ciclo for para percorrer uma matriz, em que 1º percorra as colunas do meio e no final irá para a 1ª e em seguida para última? Tenho dividido em dois for's, primeiramente percorria uma for para as colunas do meio, outro for para os extremos, mas queria por só num for, alguém pode dar uma ideia? Obrigado pela atenção, alves077
HappyHippyHippo Posted December 7, 2012 at 12:56 AM Report #486100 Posted December 7, 2012 at 12:56 AM int columns[] = {1, 0, 2}; int i; for(i = 0; i < 3; i++) { matrix[columns[1]]; } IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
pmg Posted December 7, 2012 at 10:47 AM Report #486136 Posted December 7, 2012 at 10:47 AM (edited) Tens que ter em atenção se o número de colunas é par ou impar: #include <stdio.h> void outwards(int ncols) { int k, firstindex; printf("indexes for %d columns:", ncols); firstindex = (ncols - 1) / 2; for (k = 0; k < ncols; k++) { int index, val, sign = 1; if (ncols % 2 == 0) sign = -1; if (k % 2 == 1) sign *= -1; val = (k + 1) / 2; // 0 1 1 2 2 3 3 4 ... index = firstindex + sign * val; // 0 +/-1 -/+1 +/-2 -/+2 +/-3 ... printf(" %d", index); } printf("\n"); } int main(void) { outwards(8); outwards(9); outwards(10); outwards(11); return 0; } [edit]algumas melhorias[/edit] void outwards(int ncols) { int k, firstindex, sign = 1; printf("indexes for %d columns:", ncols); firstindex = (ncols - 1) / 2; if (ncols % 2) sign = -1; for (k = 0; k < ncols; k++) { int index, val, offset; sign *= -1; // swap sign val = (k + 1) / 2; // 0 1 1 2 2 3 3 4 ... offset = sign * val; // 0 +/-1 -/+1 +/-2 -/+2 +/-3 ... index = firstindex + offset; printf(" %d", index); } printf("\n"); } Edited December 7, 2012 at 01:05 PM by pmg algumas melhorias What have you tried? Não respondo a dúvidas por PM A minha bola de cristal está para compor; deve ficar pronta para a semana. Torna os teus tópicos mais atractivos e legíveis usando a tag CODE para colorir o código!
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