πŸ‡―πŸ‡΅ ζ—₯本θͺž | πŸ‡ΊπŸ‡Έ English
Advertisement

Pseudocode to C Conversion Practice

Understand the gap between an algorithm's "idea" and its "implementation". Great preparation for programming exams.

Pseudocode ↔ C Mapping Table

Let's first see how pseudocode constructs map to C.
PseudocodeC
Integer: xint x;
Real: xdouble x;
Char: cchar c;
x ← 10x = 10;
Display xprintf("%d", x);
Input xscanf("%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 xsqrt(x)
Swap A[i] and A[j]int t=A[i]; A[i]=A[j]; A[j]=t;
Pseudocode note: ← is assignment, = is equality (C's ==). Semicolons and braces aren't used β€” indentation indicates structure.

Beginner: Basic Syntax Conversions

P-01
Compute a sum
Integer: sum ← 0
For i from 1 to 10
sum ← sum + i
EndFor
Display sum
#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;
}
P-02
Sign check
Integer: x
Input x
If x > 0 then
Display "positive"
Else if x = 0 then
Display "zero"
Else
Display "negative"
EndIf
#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;
}
P-03
Count even numbers
Integer array: A[5] ← {3, 8, 15, 22, 7}
Integer: count ← 0
For i from 0 to 4
If A[i] mod 2 = 0 then
count ← count + 1
EndIf
EndFor
Display "Even count:" and count
#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;
}
P-04
Countdown
Integer: n ← 5
While n > 0
Display n
n ← n - 1
EndWhile
Display "Liftoff!"
#include <stdio.h>

int main(void) {
    int n = 5;
    while (n > 0) {
        printf("%d\n", n);
        n--;
    }
    printf("Liftoff!\n");
    return 0;
}
P-05
Max of two numbers
Function max(a, b)
If a > b then
Return: a
Else
Return: b
EndIf

Display max(7, 12)
#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;
}

Intermediate: Algorithm Conversions

P-06
Array maximum
Integer array: A[n] // n elements
Integer: max ← A[0]
For i from 1 to n-1
If A[i] > max then
max ← A[i]
EndIf
EndFor
Return max
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;
}
Conversion note: Pseudocode's "to n-1" becomes i < n in C (zero-based). You must pass the array size as an argument.
P-07
Linear search
Function search(A, n, key)
For i from 0 to n-1
If A[i] = key then
Return i
EndIf
EndFor
Return -1 // not found
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;
}
P-08
Bubble sort
Function bubbleSort(A, n)
For i from 0 to n-2
For j from n-1 downto i+1
If A[j-1] > A[j] then
Swap A[j-1] and A[j]
EndIf
EndFor
EndFor
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;
            }
        }
    }
}
Conversion note: "Swap" is a single pseudocode line but takes three lines with a temp variable in C. Descending loop uses j--.
P-09
Euclidean algorithm (GCD)
Function gcd(a, b)
While b β‰  0
Integer: temp ← b
b ← a mod b
a ← temp
EndWhile
Return a
int gcd(int a, int b) {
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}
P-10
Binary search
Function binarySearch(A, n, key)
Integer: lo ← 0, hi ← n - 1
While lo ≀ hi
Integer: mid ← (lo + hi) Γ· 2
If A[mid] = key then
Return mid
Else if A[mid] < key then
lo ← mid + 1
Else
hi ← mid - 1
EndIf
EndWhile
Return -1
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;
}

Advanced: Recursion, Pointers, and Structs

P-11
Factorial (recursion)
Function factorial(n)
If n ≀ 1 then
Return 1
Else
Return n Γ— factorial(n - 1)
EndIf
int factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}
P-12
Modify a value through a pointer
Function addTen(address of x)
value at address of x ← value at address of x + 10

Integer: a ← 5
Call addTen(address of a)
Display a // becomes 15
#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;
}
Conversion note: Pseudocode's "address" maps to C's & (address-of) and * (dereference). That's the essence of pointers.
P-13
Manage coordinates with a struct
Struct Point:
Real: x, y

Function distance(p1, p2) // distance between two points
dx ← p2.x - p1.x
dy ← p2.y - p1.y
Return sqrt(dxΒ² + dyΒ²)

Point: a ← (0.0, 0.0)
Point: b ← (3.0, 4.0)
Display distance(a, b) // 5.0
#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;
}
P-14
Quicksort (recursion)
Function quickSort(A, lo, hi)
If lo >= hi then exit
Integer: pivot ← A[hi]
Integer: i ← lo
For j from lo to hi-1
If A[j] < pivot then
Swap A[i] and A[j]
i ← i + 1
EndIf
EndFor
Swap A[i] and A[hi]
quickSort(A, lo, i - 1)
quickSort(A, i + 1, hi)
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);
}
Conversion note: Extract "swap" into its own function. Pseudocode's "exit" becomes return; in C.
P-15
Newton's method
Function newton(f, df, x0, eps)
Real: x ← x0
Loop
Real: x_new ← x - f(x) / df(x)
If |x_new - x| < eps then
Return x_new
EndIf
x ← x_new
EndLoop
#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;
    }
}
Conversion note: Pseudocode's unconditional "Loop" becomes 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.

Common Conversion Mistakes

PseudocodeC pitfall
x = y (comparison)x == y (= is assignment!)
Swap A[i] and A[j]Needs a temp variable (not a one-liner)
sqrt of xsqrt(x) (need to include math.h)
Loop (unconditional)while(1) + break to exit
i from 1 to nfor(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 addresspointer parameter int *px and & at call site
Γ· (integer division)/ (between ints it truncates automatically)
Share this article
Share on X