Understand the gap between an algorithm's "idea" and its "implementation". Great preparation for programming exams.
| Pseudocode | C |
|---|---|
| Integer: x | int x; |
| Real: x | double x; |
| Char: c | char c; |
| x β 10 | x = 10; |
| Display x | printf("%d", x); |
| Input x | scanf("%d", &x); |
| If x > 0 then stmt Else stmt EndIf | if (x > 0) { stmt; } else { stmt; } |
| For i from 1 to n stmt EndFor | for (int i = 1; i <= n; i++) { stmt; } |
| While cond stmt EndWhile | while (cond) { stmt; } |
| Integer array: A[n] | int A[n]; |
| Function add(a, b) Return: a + b | int add(int a, int b) { return a + b; } |
| |x| (absolute value) | abs(x) or fabs(x) |
| sqrt of x | sqrt(x) |
| Swap A[i] and A[j] | int t=A[i]; A[i]=A[j]; A[j]=t; |
β is assignment, = is equality (C's ==). Semicolons and braces aren't used β indentation indicates structure.#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("positive\n"); } else if (x == 0) { // pseudocode = becomes C == printf("zero\n"); } else { printf("negative\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("Even count: %d\n", count); // 2 return 0; }
#include <stdio.h> int main(void) { int n = 5; while (n > 0) { printf("%d\n", n); n--; } printf("Liftoff!\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 in C (zero-based). You must pass the array size as an argument.int search(int A[], int n, int key) { for (int i = 0; i < n; i++) { if (A[i] == key) { // pseudocode = becomes 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--) { // descending loop if (A[j-1] > A[j]) { int tmp = A[j-1]; // swap needs a temp 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; // Γ· becomes / (integer division) 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) { // "address" -> pointer parameter *px = *px + 10; // "value at address" -> *px } int main(void) { int a = 5; addTen(&a); // "address of a" -> &a printf("%d\n", a); // 15 return 0; }
& (address-of) and * (dereference). That's the essence of pointers.#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; in 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) { // "Loop" -> infinite loop double x_new = x - f(x) / df(x); if (fabs(x_new - x) < eps) // |...| -> fabs() return x_new; x = x_new; } }
while(1). "|x|" is fabs(x). Passing functions is expressed with function pointers in C, though the simplest approach for beginners is to use global functions.| Pseudocode | C pitfall |
|---|---|
| x = y (comparison) | x == y (= is assignment!) |
| Swap A[i] and A[j] | Needs a temp variable (not a one-liner) |
| sqrt of x | sqrt(x) (need to include math.h) |
| Loop (unconditional) | while(1) + break to exit |
| i from 1 to n | for(i=1; i<=n; i++) (don't confuse with 0-based arrays) |
| func(array, size) | Must pass the array size explicitly (C has no length introspection) |
| pass by address | pointer parameter int *px and & at call site |
| Γ· (integer division) | / (between ints it truncates automatically) |