Convert pseudocode to C
Codoritmo translates your algorithm into a C program ready to compile: it works out whether each variable is an int, a double, a bool or text, turns Escribir into printf and Leer into scanf, and declares your functions before main. It can also generate C++, with cout and cin.
Opens the program in Codoritmo; choose C in View › Translate To.
The same algorithm in pseudocode and in C
The “GCD and LCM” example, with a function that uses Mientras and MOD, and the C that Codoritmo generates for it, untouched.
Pseudocode
Funcion resultado <- MCD(a, b) Definir resultado, resto Como Entero // Algoritmo de Euclides Mientras b <> 0 Hacer resto <- a MOD b a <- b b <- resto FinMientras resultado <- aFinFuncionAlgoritmo MCDyMCM Definir a, b, divisor Como Entero Escribir "Primer número:" Leer a Escribir "Segundo número:" Leer b Si a <= 0 O b <= 0 Entonces Escribir "Usa números enteros positivos." SiNo divisor <- MCD(a, b) Escribir "MCD(", a, ", ", b, ") = ", divisor Escribir "MCM(", a, ", ", b, ") = ", Trunc(a * b / divisor) FinSiFinAlgoritmoC generated by Codoritmo
/* Este código fue generado por Codoritmo a partir del pseudocódigo. Puede que no sea completamente correcto: revíselo antes de usarlo. */#include<stdio.h>/* Funciones auxiliares, definidas al final */int truncar(double x);/* Declaraciones adelantadas de los subprocesos */int mcd(int a, int b);int main() { int a = 0; int b = 0; int divisor = 0; printf("Primer número:\n"); scanf("%d", &a); printf("Segundo número:\n"); scanf("%d", &b); if (a<=0 || b<=0) { printf("Usa números enteros positivos.\n"); } else { divisor = mcd(a, b); printf("MCD(%i, %i) = %i\n", a, b, divisor); printf("MCM(%i, %i) = %i\n", a, b, truncar((double)(a*b)/divisor)); } return 0;}int mcd(int a, int b) { int resto = 0; int resultado = 0; while (b!=0) { resto = a%b; a = b; b = resto; } resultado = a; return resultado;}/* Como trunc() de PSeInt: un valor a menos de 1e-11 del entero siguiente *//* se trunca a ese entero, así trunc(100*0.57) da 57 y no 56. */int truncar(double x) { int t = (int)x; if (t+1-x<1e-11) return t+1; if (t-1-x>-1e-11) return t-1; return t;}This is exactly the code Codoritmo generates for this program, including the comment at the top, which is always in Spanish.
How to translate your pseudocode to C
Write or open your algorithm
It must be free of syntax errors; the Problems panel lists any.
Choose C or C++
In View › Translate To, choose C or C++. The code tab in the side panel shows the translation.
Compare and copy
The translation updates as you type. Copy it, or download it as .c or .cpp with File › Export As.
Compile it
With gcc: gcc program.c -o program -lm, then ./program. The -lm is needed when the code uses math.h.
Opens the program in Codoritmo; choose C in View › Translate To.
What changes in C
C has no arrays sized with a value known only at run time, like Dimension datos[n]. In that case the code uses a fixed maximum size, ARREGLO_MAX (100), and explains it in a comment.
Text values are char arrays of 256 characters (MAX_STRLEN), including the terminating character.
C arrays start at 0: datos[i] becomes datos[i-1] when your profile uses 1-based arrays.
Aleatorio uses rand(). For different numbers on every run, add srand(time(NULL)); at the start of main and include time.h.
How each instruction is translated
Each row is one PSeInt instruction translated by Codoritmo inside a program with integers n and i, a real x, a text nombre and an array datos[5]. The output is generated when this page is built.
| Instruction | PSeInt | C |
|---|---|---|
| Write (Escribir) | Escribir "Hola, ", nombre | printf("Hola, %s\n", nombre); |
| Write without a newline | Escribir Sin Saltar "Total: " | printf("Total: "); |
| Read an integer (Leer) | Leer n | scanf("%d", &n); |
| Read a real number | Leer x | scanf("%lf", &x); |
| Random number | n <- Aleatorio(1, 6) | n = (1+rand()%(6-1+1)); |
| If … else (Si … SiNo) | Si n MOD 2 = 0 Entonces
Escribir "par"
SiNo
Escribir "impar"
FinSi | if (n%2==0) {
printf("par\n");
} else {
printf("impar\n");
} |
| Switch (Segun) | Segun n Hacer
1: Escribir "uno"
De Otro Modo: Escribir "otro"
FinSegun | switch (n) {
case 1:
printf("uno\n");
break;
default:
printf("otro\n");
} |
| While (Mientras) | Mientras n > 0 Hacer
n <- n - 1
FinMientras | while (n>0) {
n = n-1;
} |
| Repeat … until (Repetir) | Repetir
Leer n
Hasta Que n > 0 | do {
scanf("%d", &n);
} while (n<=0); |
| For (Para) | Para i <- 1 Hasta 10 Hacer
Escribir i
FinPara | for (i=1; i<=10; ++i) {
printf("%i\n", i);
} |
| Array element | datos[1] <- n | datos[0] = n; |
Codoritmo and PSeInt desktop
PSeInt desktop also exports to C. These are the differences.
What Codoritmo adds
- You see the translation next to the pseudocode, and it updates as you type.
- It also exports to C#, PHP, Pascal, Visual Basic, QBasic, MatLab and HTML, like PSeInt.
- It generates C and C++.
What PSeInt desktop does that Codoritmo doesn't yet
- Its exporter has many years of classroom use; Codoritmo's is newer, so review the code before you hand it in.
Codoritmo is an independent project, inspired by PSeInt and compatible with its syntax and school profiles. It is not PSeInt and is not affiliated with it. Official PSeInt website
Frequently asked questions
Which compiler should I use?
Any standard C compiler: gcc, clang, or the one that comes with Dev-C++ or Code::Blocks.
C or C++?
Use the one your course asks for. The C version uses printf and scanf; the C++ one uses cout, cin and string.
Are subprocesses and functions translated?
Yes. Each SubProceso or Funcion becomes a C function, declared before main so it can be used in any order.
Why is there an ARREGLO_MAX?
Because your algorithm sizes an array with a variable. C needs the size at compile time, so room for 100 elements is reserved. If you need more, change the number in the #define.
Examples to try
Solved programs, each with its code, its flowchart and a sample run.
GCD and LCM
Computes the greatest common divisor and the least common multiple.
IntermediateTimes Table
Prints the multiplication table for the number you choose.
BeginnerPrime Numbers
Finds every prime number up to a limit.
IntermediateFibonacci Sequence
Prints the first terms of the Fibonacci sequence.
Beginner
By concept:Functions examplesMath examples
More Codoritmo tools
- PSeInt onlineWrite and run pseudocode with PSeInt's syntax from your browser.
- PSeInt alternativeAn honest comparison of Codoritmo and PSeInt desktop.
- Online flowchart makerYour algorithm's flowchart, generated from the code and animated as it runs.
- Trace tablesA table of every variable's value at each step, filled in while the program runs.
- Pseudocode to PythonYour algorithm translated into a Python program you can run.
- Pseudocode to JavaYour algorithm translated into a Java class with a main method.
- Block-based programmingBuild your algorithm with Scratch-like blocks and see the pseudocode they write.