Skip to content

Instantly share code, notes, and snippets.

@LuanComputacao
Last active May 19, 2020 04:25
Show Gist options
  • Save LuanComputacao/f8c207c7c7a4fd95294a9ae9dd71e7f1 to your computer and use it in GitHub Desktop.
Save LuanComputacao/f8c207c7c7a4fd95294a9ae9dd71e7f1 to your computer and use it in GitHub Desktop.
// To execute
// gcc exemplo_de_troca_de_posicoes_em_c.c -o programinha && ./programinha
//
#include <stdio.h>
void trocaValor(int *vetorzinho, int primeiro, int segundo) // Definição da função "EsperaEnter"
{
int temporario = vetorzinho[primeiro];
vetorzinho[primeiro] = vetorzinho[segundo];
vetorzinho[segundo] = temporario;
}
void imprimeVetorMaroto(int *vetorMaroto, int tamanho)
{
for (int i = 0; i < tamanho; i++)
{
printf("%d ", vetorMaroto[i]);
}
printf("\n");
}
void preencheVetor(int *vetorComLixoDeMemoria, int tamanho)
{
int count = 10;
for (size_t i = 0; i < tamanho; i++)
{
vetorComLixoDeMemoria[i] = (i + 1) * 3;
}
}
void main()
{
int numeros[10];
int indicesDeTroca[2] = {2, 3};
int tamanhoDoVetor = sizeof(numeros) / sizeof(numeros[0]);
printf("Trocando posições %d e %d\n", indicesDeTroca[0], indicesDeTroca[1]);
printf("\n\n");
printf("Tamanho do Vetor: %d \n\n", tamanhoDoVetor);
preencheVetor(numeros, tamanhoDoVetor);
printf("Antes de trocar: \n");
imprimeVetorMaroto(numeros, tamanhoDoVetor);
trocaValor(numeros, indicesDeTroca[0], indicesDeTroca[1]);
printf("Depois de trocar: \n");
imprimeVetorMaroto(numeros, tamanhoDoVetor);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment