Flames Posted March 21, 2012 Report Share Posted March 21, 2012 #include <stdio.h> #include <stdlib.h> void rightRotatebyOne(int arr[], int n); void rightRotate(int arr[], int d, int n); void printArray(int arr[], int size); int main() { int arr[] = {1, 2, 3}; rightRotate(arr, 2, 3); printArray(arr, 3); return 0; } void rightRotate(int arr[], int d, int n) { int i,temp; for (i = 0; i < d; i++) rightRotatebyOne(arr, n); } void rightRotatebyOne(int arr[], int n) { int i,x,temp; temp=arr[2]; for (i = 2; i > 0; i--){ arr[i] = arr[i-1]; } arr[0]=temp; } /* utility function to print an array */ void printArray(int arr[], int size) { int i; for(i = 0; i < size; i++) printf("%d ", arr[i]); } Eu meti com numeros e tal na funcao righrotatebyone para testar para um caso particular depois penso de um modo geral... Alguém me consegue explicar o porquê desse meu pseudo-algoritmo estar a falhar? :S Eu guardo o ultimo caracter para por no fim no index 0 o que faco é ir desde o fim e substituir pelo anterior ate chegar ao index 1 mas falha-me... AFFFFFFFFFF Link to comment Share on other sites More sharing options...
pmg Posted March 21, 2012 Report Share Posted March 21, 2012 No ideone ( http://ideone.com/vW09Y ) esta a funcionar. Removi as variaveis temp de RightRotate() e x de rightRotatebyOne() para o compilador nao dar erro. Nota que a instrucao rightRotate(arr, 2, 3); faz com que o array seja rodado 2 vezes! Experimenta com um array maior. Sugestao: da nomes maiores que 1 caracter as variaveis. d nao quer dizer nada; mas n é aceitavel 😉 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! Link to comment Share on other sites More sharing options...
Flames Posted March 22, 2012 Author Report Share Posted March 22, 2012 No ideone ( http://ideone.com/vW09Y ) esta a funcionar. Removi as variaveis temp de RightRotate() e x de rightRotatebyOne() para o compilador nao dar erro. Nota que a instrucao rightRotate(arr, 2, 3); faz com que o array seja rodado 2 vezes! Experimenta com um array maior. Sugestao: da nomes maiores que 1 caracter as variaveis. d nao quer dizer nada; mas n é aceitavel 😉 Thanks pmg que raios será a minha mente que crashou? XD omg como é possivel a resposta a frente dos olhos :S enfim... Sim normalmente uso variaveis com uma melhor designacao essa foi infeliz :x 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