Faça a ponte entre a "ideia" e a "implementação" de um algoritmo. Ótima preparação para provas de programação.
| Pseudocódigo | C |
|---|---|
| Inteiro: x | int x; |
| Real: x | double x; |
| Caractere: c | char c; |
| x ← 10 | x = 10; |
| Exibir x | printf("%d", x); |
| Ler x | scanf("%d", &x); |
| Se x > 0 então stmt Senão stmt FimSe | if (x > 0) { stmt; } else { stmt; } |
| Para i de 1 até n stmt FimPara | for (int i = 1; i <= n; i++) { stmt; } |
| Enquanto cond stmt FimEnquanto | while (cond) { stmt; } |
| Array de inteiros: A[n] | int A[n]; |
| Função add(a, b) Retornar: a + b | int add(int a, int b) { return a + b; } |
| |x| (valor absoluto) | abs(x) ou fabs(x) |
| raiz quadrada de x | sqrt(x) |
| Trocar A[i] e A[j] | int t=A[i]; A[i]=A[j]; A[j]=t; |
← é atribuição e = é igualdade (o == em C). Ponto-e-vírgula e chaves não são usados — a indentação transmite a estrutura.#include <stdio.h> int main(void) { int sum = 0; for (int i = 1; i <= 10; i++) { sum = sum + i; } printf("%d\n", sum); // 55 return 0; }
#include <stdio.h> int main(void) { int x; scanf("%d", &x); if (x > 0) { printf("positivo\n"); } else if (x == 0) { // o = do pseudocódigo vira == em C printf("zero\n"); } else { printf("negativo\n"); } return 0; }
#include <stdio.h> int main(void) { int A[] = {3, 8, 15, 22, 7}; int count = 0; for (int i = 0; i <= 4; i++) { if (A[i] % 2 == 0) { count++; } } printf("Pares: %d\n", count); // 2 return 0; }
#include <stdio.h> int main(void) { int n = 5; while (n > 0) { printf("%d\n", n); n--; } printf("Decolar!\n"); return 0; }
#include <stdio.h> int max(int a, int b) { if (a > b) return a; else return b; } int main(void) { printf("%d\n", max(7, 12)); // 12 return 0; }
int find_max(int A[], int n) { int max = A[0]; for (int i = 1; i < n; i++) { if (A[i] > max) { max = A[i]; } } return max; }
i < n em C (base zero). Você também precisa passar o tamanho do array como argumento.int search(int A[], int n, int key) { for (int i = 0; i < n; i++) { if (A[i] == key) { // o = do pseudocódigo vira == em C return i; } } return -1; }
void bubbleSort(int A[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = n - 1; j > i; j--) { // laço decrescente if (A[j-1] > A[j]) { int tmp = A[j-1]; // troca exige uma variável temporária A[j-1] = A[j]; A[j] = tmp; } } } }
j--.int gcd(int a, int b) { while (b != 0) { int temp = b; b = a % b; a = temp; } return a; }
int binarySearch(int A[], int n, int key) { int lo = 0, hi = n - 1; while (lo <= hi) { int mid = (lo + hi) / 2; // ÷ vira / (divisão inteira) if (A[mid] == key) return mid; else if (A[mid] < key) lo = mid + 1; else hi = mid - 1; } return -1; }
int factorial(int n) { if (n <= 1) return 1; return n * factorial(n - 1); }
#include <stdio.h> void addTen(int *px) { // "endereço" -> parâmetro ponteiro *px = *px + 10; // "valor no endereço" -> *px } int main(void) { int a = 5; addTen(&a); // "endereço de a" -> &a printf("%d\n", a); // 15 return 0; }
& (endereço-de) e * (dereferência) de C. Isso é, em resumo, o que ponteiros são.#include <stdio.h> #include <math.h> struct Point { double x, y; }; double distance(struct Point p1, struct Point p2) { double dx = p2.x - p1.x; double dy = p2.y - p1.y; return sqrt(dx*dx + dy*dy); } int main(void) { struct Point a = {0.0, 0.0}; struct Point b = {3.0, 4.0}; printf("%.1f\n", distance(a, b)); // 5.0 return 0; }
void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; } void quickSort(int A[], int lo, int hi) { if (lo >= hi) return; int pivot = A[hi]; int i = lo; for (int j = lo; j < hi; j++) { if (A[j] < pivot) { swap(&A[i], &A[j]); i++; } } swap(&A[i], &A[hi]); quickSort(A, lo, i - 1); quickSort(A, i + 1, hi); }
return; em C.#include <math.h> double f(double x) { return x*x - 2; } double df(double x) { return 2*x; } double newton(double x0, double eps) { double x = x0; while (1) { // "Laço" -> laço infinito double x_new = x - f(x) / df(x); if (fabs(x_new - x) < eps) // |...| -> fabs() return x_new; x = x_new; } }
while(1), e "|x|" é fabs(x). Passar funções como argumentos usa ponteiros de função em C, embora usar funções globais seja a abordagem mais simples para iniciantes.| Pseudocódigo | Armadilha em C |
|---|---|
| x = y (comparação) | x == y (= é atribuição!) |
| Trocar A[i] e A[j] | Precisa de uma variável temporária (não é uma linha só) |
| raiz quadrada de x | sqrt(x) (precisa incluir math.h) |
| Laço (incondicional) | while(1) + break para sair |
| i de 1 até n | for(i=1; i<=n; i++) (cuidado com arrays base zero) |
| func(array, tamanho) | Você precisa passar o tamanho do array explicitamente — C não tem introspecção de tamanho |
| passar por endereço | parâmetro ponteiro int *px, com & no local da chamada |
| ÷ (divisão inteira) | / — entre ints trunca automaticamente |