Skip to content

Instantly share code, notes, and snippets.

@JPBM135
Last active September 21, 2025 20:11
Show Gist options
  • Save JPBM135/853ef98a01144aadb7c09265455eff4d to your computer and use it in GitHub Desktop.
Save JPBM135/853ef98a01144aadb7c09265455eff4d to your computer and use it in GitHub Desktop.
Estruturas de dados

Executando os arquivos

FILE_NAME=$1

gcc $FILE_NAME.c -Wall -Wextra -Werror -o $FILE_NAME.out && ./$FILE_NAME.out

Isso irá gerar um arquivo .out e executálo, o gcc está no modo mais estrito.

Exemplo:

$ sh run.sh Average
#include <stdio.h>
#include <limits.h>
#define SIZE 5
int findValue(int haystack[], int needle)
{
for (int i = 0; i < SIZE; i++)
{
if (haystack[i] == needle)
{
return i; // Retorna o índice do valor encontrado
}
}
return -1;
}
int main()
{
int arr[SIZE] = {3, 5, 7, 2, 8};
int valueToFind = 4;
int result = findValue(arr, valueToFind);
if (result < 0)
{
printf("Valor não encontrado\n");
}
else
{
printf("Valor encontrado no índice: %d\n", result);
}
return 0;
}
#include <stdio.h>
int biggestValue(int arr[], int size)
{
if (size <= 0)
{
return -1; // Indica que o array está vazio
}
int max = arr[0];
for (int i = 1; i < size; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
return max;
}
int main()
{
int arr[] = {3, 5, 7, 2, 8};
int size = sizeof(arr) / sizeof(arr[0]);
int max = biggestValue(arr, size);
if (max != -1)
{
printf("O maior valor no array é: %d\n", max);
}
else
{
printf("O array está vazio.\n");
}
return 0;
}
#include <stdio.h>
float avarageValue(int arr[], int size)
{
if (size <= 0)
{
return -1; // Indica que o array está vazio
}
int sum = 0;
for (int i = 1; i < size; i++)
{
sum += arr[i];
}
float avg = (float)sum / (float)size;
return avg;
}
int main()
{
int arr[] = {3, 5, 7, 2, 8, 10, 12, 5, 6, 1};
int size = sizeof(arr) / sizeof(arr[0]);
float avg = avarageValue(arr, size);
if (avg != -1)
{
printf("A média do array é: %f\n", avg);
}
else
{
printf("O array está vazio.\n");
}
return 0;
}
#include <stdio.h>
int smallestValue(int arr[], int size)
{
if (size <= 0)
{
return -1; // Indica que o array está vazio
}
int min = arr[0];
for (int i = 1; i < size; i++)
{
if (arr[i] < min)
{
min = arr[i];
}
}
return min;
}
int main()
{
int arr[] = {3, 5, 7, 2, 8};
int size = sizeof(arr) / sizeof(arr[0]);
int min = smallestValue(arr, size);
if (min != -1)
{
printf("O menor valor no array é: %d\n", min);
}
else
{
printf("O array está vazio.\n");
}
return 0;
}
#include <stdio.h>
#define LINHAS 3
#define COLUNAS 3
int matrixSum(int matrixOne[LINHAS][COLUNAS], int matrixTwo[LINHAS][COLUNAS], int result[LINHAS][COLUNAS])
{
int matrizOneSize = LINHAS;
int matrizTwoSize = LINHAS;
if (matrizOneSize != matrizTwoSize)
{
return -1; // Indica que as matrizes têm tamanhos diferentes
}
for (int i = 0; i < matrizOneSize; i++)
{
for (int j = 0; j < COLUNAS; j++)
{
result[i][j] = matrixOne[i][j] + matrixTwo[i][j];
}
}
return 0;
}
int main()
{
int matrixOne[LINHAS][COLUNAS] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
int matrixTwo[LINHAS][COLUNAS] = {
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}};
int result[LINHAS][COLUNAS];
if (matrixSum(matrixOne, matrixTwo, result) == -1)
{
printf("As matrizes têm tamanhos diferentes.\n");
return -1;
}
printf("Resultado da soma das matrizes:\n");
for (int i = 0; i < LINHAS; i++)
{
for (int j = 0; j < COLUNAS; j++)
{
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
#include <stdio.h>
#define LINHAS 2
#define COLUNAS 3
void matrixTranspose(int matrix[LINHAS][COLUNAS], int result[COLUNAS][LINHAS])
{
for (int i = 0; i < LINHAS; i++)
{
for (int j = 0; j < COLUNAS; j++)
{
result[j][i] = matrix[i][j];
}
}
}
int main()
{
int matrixOne[LINHAS][COLUNAS] = {
{1, 2, 3},
{4, 5, 6}};
int result[COLUNAS][LINHAS];
matrixTranspose(matrixOne, result);
printf("Resultado da transposição das matrizes:\n");
for (int i = 0; i < COLUNAS; i++)
{
for (int j = 0; j < LINHAS; j++)
{
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}

calc-distancia.c

#include <stdio.h>
#include <math.h>

#include "ponto/ponto.c"

int main()
{
  Ponto ponto1 = ponto_criar(1, 1);
  Ponto ponto2 = ponto_criar(3, 4);

  float distance = ponto_distancia(&ponto1, &ponto2);

  printf("A distancia entre os pontos é: %f\n", distance);
}

ponto/ponto.c

// <> - Includes do sistema
#include <math.h>
#include <stdlib.h>

// "" - Includes do desenvolvedor
#include "ponto.h"

Ponto ponto_criar(int x, int y)
{
  Ponto p = {x, y};
  return p;
}

void ponto_mover(Ponto *p, int dx, int dy)
{
  p->x += dx;
  p->y += dy;
}

float ponto_distancia(const Ponto *p1, const Ponto *p2)
{
  // O sinal é irrelevante para o calculo de distância
  int horizontalDistance = abs(ponto_x(p1) - ponto_x(p2));
  int verticalDistance = abs(ponto_y(p1) - ponto_y(p2));

  float hypotenuseBeforeSqrt = powf(horizontalDistance, 2) + powf(verticalDistance, 2);

  return sqrtf(hypotenuseBeforeSqrt);
}

int ponto_x(const Ponto *p)
{
  return p->x;
}

int ponto_y(const Ponto *p)
{
  return p->y;
}

ponto/ponto.h

#ifndef PONTO_H
#define PONTO_H

typedef struct
{
  int x;
  int y;
} Ponto;

Ponto ponto_criar(int x, int y);

void ponto_mover(Ponto *p, int dx, int dy);

float ponto_distancia(const Ponto *p1, const Ponto *p2);

int ponto_x(const Ponto *p);
int ponto_y(const Ponto *p);

#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment