askazy Posted July 7, 2014 Report Share Posted July 7, 2014 Exercicio: Crie uma função que receba um vetor como parâmetro e retorne i) 1 se o vetor estiver ordenado em ordem não crescente ii) -1 se o vetor estiver ordenado em ordem não decrescente iii) 0 se o vetor não estiver ordenado Fiz o seguinte: #include <stdio.h> #include <stdlib.h> #define MAX 10 int verifica_ordem(int vet[]) { int i, cont = 0, cont2 = 0; for (i = 0; i < MAX; i++) { if (vet[i] <= vet[i + 1]) cont++; } if (cont == (MAX - 1)) return 1; else { for (i = 0; i < MAX; i++) { if (vet[i] > vet[i + 1]) cont2++; } if (cont2 == (MAX)) return -1; else return 0; } } int main() { int i, res, vet[MAX]; for (i = 0; i < MAX; i++) { scanf("%d", &vet[i]); } res = verifica_ordem(vet); if (res == 1) printf("O vetor esta em ordem nao decrescente\n"); else if (res == -1) printf("O vetor esta em ordem nao crescente\n"); else printf("O vetor nao esta ordenado\n"); return 0; } Não sei se interpretei corretamente o não decrescente o não crescente, o não decrescente eu interpretei como crescente e o não crescente eu interpretei como estritamente decrescente, ou talvez o exercício esteja errado. Se alguém puder ajudar agradeço. E na hora de executar o meu código achei algo estranho if (cont2 == (MAX)) return -1; Porque aqui não funciona igual o pra verificar se é crescente com (MAX-1)? Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted July 8, 2014 Report Share Posted July 8, 2014 o teu código diz : for (i = 0; i < MAX; i++) { // para todos os valores da lista if (vet[i] <= vet[i + 1]) // se o valor i+1 for maior que i então cont++; // é não crescente ?!?!?! } E na hora de executar o meu código achei algo estranho if (cont2 == (MAX)) return -1; Porque aqui não funciona igual o pra verificar se é crescente com (MAX-1)? for (i = 0; i < MAX; i++) { // que valor tem i na última passagem do ciclo ? if (vet[i] <= vet[i + 1]) // que índices estás a usar na comparação ? e valores ? cont++; } só para baralhar, aqui fica uma solução só um um ciclo #include <stdio.h> #define MAX 100 #define COMP(a, b) (((a) - (b)) == 0 ? 0 : (((a) - (b)) / abs((a) - (b)))) int verifica_ordem(int v[], int n) { int i, check, state = 0; for (i = 1; i < n; ++i) { if (state == 0) state = COMP(v[i-1], v[i]); else if (COMP(v[i-1], v[i]) != 0 && state != COMP(v[i-1], v[i])) return 0; } return state; } int main() { int i, n, array[MAX]; scanf(" %d", &n); n = n > MAX ? MAX : n; for (i = 0; i < n; i++) scanf(" %d", &array[i]); switch (verifica_ordem(array, n)) { case 1: printf("O vector esta ordenado por ordem nao crescente\n"); break; case 0: printf("O vetor nao esta ordenado ou tem menos que dois elementos\n"); break; case -1: printf("O vector esta ordenado por ordem nao decrescente\n"); break; default: printf("Valor de retorno inesperado\n"); break; } return 0; } IRC : sim, é algo que ainda existe >> #p@p Portugol Plus 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