DownD 43 Posted July 8, 2015 Boas eu estava a tentar fazer um exercicio que era pegar em numeros inseridos pelo utilizador e depois mete-los por ordem crescente. int output[30] = {0}; int input[10]; int cunt2 = 30; int n = 1; int m = 1; cout << "Input 10 Values" << endl; for (int i = 0; i < cunt; i++) { cin >> input[i]; cout << "Next " << endl; if ((input[i] < output [i-1]) && i != 0) { output[15 - n] = input [i]; n++; } if ((input[i] > output [i-1]) && i != 0) { output[15 + m] = input [i]; m++; } output[0] = input[0]; } cout << "In order crescent are: " << endl; n = 1; for (int i = 0; i < cunt2; i++) { if(output[i] != 0) { cout << n << " - " << output[i] << endl; n++; } } Alguem pode ajudar sff Mas este programa apenas voltar a meter os numeros da mesma forma que foram inseridos pelo utilizador. Share this post Link to post Share on other sites
Auros ( ͡ʘ ͜ʖ ͡ʘ) 172 Posted July 8, 2015 Arrays.sort() se não me engano faz por ordem crescente. Documentação na internet ajuda, nunca te esqueças disso. 1 DownD reacted to this Share this post Link to post Share on other sites
DownD 43 Posted July 8, 2015 Arrays.sort() se não me engano faz por ordem crescente. Documentação na internet ajuda, nunca te esqueças disso. Obrigado, mas não existe uma maneira manual ? eu queria resolver de acordo com o que aprendi até agora. Share this post Link to post Share on other sites
Auros ( ͡ʘ ͜ʖ ͡ʘ) 172 Posted July 8, 2015 Com arrays... A maneira mais fácil é com a função que te disse, sem ela só por comparações(?). Mal aprendes a trabalhar com arrays ensinam te a ordenar, eu só me lembro de usar o sort. Como disse por comparações também poderás fazer, o vê este exemplo: Share this post Link to post Share on other sites
Xandy 46 Posted July 28, 2015 A única coisa que tens de fazer para ordernar é comparar o valor na posição actual do array com o valor da posição seguinte e ver se é ou não maior... Se for, guardas o antigo valor numa variável temporária para nã perder informação e fazes a troca... Share this post Link to post Share on other sites
danyscp1 21 Posted July 30, 2015 Boas rapaz tens algum mail k me possas fornecer eu ja passei a p1 no meu curso e ainda tenho as coisas de programaçao gurdadas e tenho materia e um exerciciomuito semelhante a esse se kiseres enviote para o mail Share this post Link to post Share on other sites
Xandy 46 Posted August 5, 2015 O objetivo é ele aprender e não ter as coisas já feitas... Share this post Link to post Share on other sites
Jorge22 3 Posted August 7, 2015 As vezes vendo o exercicio resolvido e tentando perceber todos os passos é onde se aprende melhor. Deixo aqui um código que fiz rapidamente. Pode ter problemas, mas funciona para o objetivo proposto. #include <iostream> #include <iomanip> #include <string> using namespace std; const int NNUMEROS = 5; int main() { int numeros[NNUMEROS]; // Preencher o Array for (int i = 0; i < NNUMEROS; i++) { system("CLS"); cout << ">> Introduz um numero. \n>> "; while (!(cin >> numeros[i])) { cin.clear(); string dummy; cin >> dummy; // throw away garbage. cout << ">> Nao introduziste um numero.\n>> "; } if (cin.eof()) { cin.clear(); } } system("CLS"); // Colocar por ordem crescente cout << ">> Deseja colocar os numeros por ordem crescente? S ou N \n>> "; char dados; cin >> dados; if (dados == 'S' || dados == 's') { for (int j = 0; j < NNUMEROS; j++) { int index = j; int menor = numeros[j]; for (int i = j; i < NNUMEROS; i++) { if (numeros[i] < menor) { menor = numeros[i]; index = i; } } int y; y = numeros[j]; numeros[j] = menor; numeros[index] = y; } } // Mostrar o array system("CLS"); for (int i = 0; i < NNUMEROS; i++) { cout << numeros[i] << " "; } cout << endl; } Share this post Link to post Share on other sites