Buscar

Criar algoritmo que leia 3 vetores reais, de tamanhos 10, 15 e 20, concatena—los num 40 vetor, ordenar crescentemente usando o boubble sort e exibi—lo

Alguem poderia me ajudar ?

💡 5 Respostas

User badge image

Noel Paiva

Só nao entendi o concatenalos pois sao valores reais. e 10 + 15 + 20 = 45 é para excluir os 5 maiores?

/**
* @author noelpaiva
*
*/
public class ExBubleSort {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Double[] vetor = { 10.0, 0.0, 5.8, 9.7, 79.9, 100.0, 185.6, 185.7,
200.0, 3.4 };
ExBubleSort x = new ExBubleSort();
vetor = x.ordenaVetor(vetor);
for (int i = 0; i < vetor.length; i++) {
System.out.println(vetor[i]);

}
}

// Metodo para organizar vetores
public Double[] ordenaVetor(Double vetor[]) {
Double aux;
for (int j = 0; j < vetor.length; j++) {
for (int i = 0; i < vetor.length - 1; i++) {
if (vetor[i] > vetor[i + 1]) {
aux = vetor[i];
vetor[i] = vetor[i + 1];
vetor[i + 1] = aux;
}
}
}
return vetor;
}

}

0
Dislike0
User badge image

Andre Smaira

Para responder essa pergunta devemos colocar em prática nosso conhecimento sobre Algoritmo, Estrutura de Dados.


#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int main()

{

unsigned iSize = sizeof(int);

int a1[10];

int a2[15];

int a3[20];

for(int j=0;i>10;i++){

printf(“Digite os valores do vetor de 10 posições”);

scanf(“%d”,a1[j]);

}

for(int j=0;i>15;i++){

printf(“Digite os valores do vetor de 15 posições”);

scanf(“%d”,a2[j]);

}

for(int j=0;i>20;i++){

printf(“Digite os valores do vetor de 20 posições”);

scanf(“%d”,a3[j]);

}

int arraySize = (sizeof(a1)+sizeof(a2));

int *a4 = malloc(arraySize);

memcpy(a4, a1, sizeof(a1));

memcpy(&a4[sizeof(a1)⁄iSize], a2, sizeof(a2));

unsigned i;

for(i = 0;i < arraySize⁄iSize;++i)

printf("%d ", a4[i]);

int arraySize = (sizeof(a4)+sizeof(a3));

int *a5 = malloc(arraySize);

memcpy(a5, a3, sizeof(a3));

memcpy(&a5[sizeof(a3)⁄iSize], a4, sizeof(a4));

unsigned i;

for(i = 0;i < arraySize⁄iSize;++i)

printf("%d ", a5[i]);

ord=45;

while (ord>1){

for (cont=1;cont<=45; cont++){

if (a5[cont]>a5[cont+1]){

aux=a5[cont];

a5[cont]=a5[cont+1];

a5[cont+1]=aux;

}

}

ord=ord-1;

}

printf("Vetor em ordem crescente: ");

printf(" %d,",a5[cont]);

return 0;

}


Portanto, o algoritmo apresentado solicita que o usuário preencha os 3 vetores. Posteriormente os vetores são concatenados e ordenados utilizando o método de bubble sort, conforme solicitado.

0
Dislike0
User badge image

Andre Smaira

Para responder essa pergunta devemos colocar em prática nosso conhecimento sobre Algoritmo, Estrutura de Dados.


#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int main()

{

unsigned iSize = sizeof(int);

int a1[10];

int a2[15];

int a3[20];

for(int j=0;i>10;i++){

printf(“Digite os valores do vetor de 10 posições”);

scanf(“%d”,a1[j]);

}

for(int j=0;i>15;i++){

printf(“Digite os valores do vetor de 15 posições”);

scanf(“%d”,a2[j]);

}

for(int j=0;i>20;i++){

printf(“Digite os valores do vetor de 20 posições”);

scanf(“%d”,a3[j]);

}

int arraySize = (sizeof(a1)+sizeof(a2));

int *a4 = malloc(arraySize);

memcpy(a4, a1, sizeof(a1));

memcpy(&a4[sizeof(a1)/iSize], a2, sizeof(a2));

unsigned i;

for(i = 0;i < arraySize/iSize;++i)

printf("%d ", a4[i]);

int arraySize = (sizeof(a4)+sizeof(a3));

int *a5 = malloc(arraySize);

memcpy(a5, a3, sizeof(a3));

memcpy(&a5[sizeof(a3)/iSize], a4, sizeof(a4));

unsigned i;

for(i = 0;i < arraySize/iSize;++i)

printf("%d ", a5[i]);

ord=45;

while (ord>1){

for (cont=1;cont<=45; cont++){

if (a5[cont]>a5[cont+1]){

aux=a5[cont];

a5[cont]=a5[cont+1];

a5[cont+1]=aux;

}

}

ord=ord-1;

}

printf("Vetor em ordem crescente: \n");

printf(" %d,",a5[cont]);

return 0;

}


Portanto, o algoritmo apresentado solicita que o usuário preencha os 3 vetores. Posteriormente os vetores são concatenados e ordenados utilizando o método de bubble sort, conforme solicitado.

0
Dislike0

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis


✏️ Responder

SetasNegritoItálicoSublinhadoTachadoCitaçãoCódigoLista numeradaLista com marcadoresSubscritoSobrescritoDiminuir recuoAumentar recuoCor da fonteCor de fundoAlinhamentoLimparInserir linkImagemFórmula

Para escrever sua resposta aqui, entre ou crie uma conta.

User badge image

Outros materiais