- Program and procedures
- Read/write methods
- File input/output methods
- FOR Loop
- Operators and math functions
- Dynamic memory management
- Real numbers and constants
This macro defines the entry point of your program. Thus, you need to include your main code in a PROGRAM {}
block.
#include "pzhelp"
PROGRAM {
WRITELN("Hello world!");
}
Declares a procedure, i.e.\ a function that returns no value.
PROC hello_world() {
WRITELN("Hello world!");
// no return value
}
Declares a function.
FUNC int square(int x) {
return x * x;
}
Reads an integer from the user input and returns its value.
int age = READ_INT();
Reads a real variable from the user input and returns its value.
REAL height = READ_REAL();
Reads a string, i.e.\ a sequence of characters, from the user input and returns its value. Requires two arguments, the first being the size of the string that will be read and the second being the string variable where the input will be stored. The function also returns a reference to that variable.
char * name;
READ_STRING(20, name);
or more shortly,
char * name = READ_STRING(20, name);
Skips a line from the user input by reading and ignoring characters until a new line or the end of file (EOF) is reached.
SKIP_LINE();
Receives a list of arguments which can be variables of different types and displays them to the user, each one separated by a space character. Supported variable types are integers (unsigned, long and long long modifiers are also supported), REALs, floats, doubles, characters, strings and booleans.
REALs, floats and doubles are all displayed with a precision of six decimal places.
WRITE(42, 3.14, ',', "no man is an island");
Offers the same functionality as WRITE, except it also prints a new line at the end of the output.
WRITELN("This string will be printed on its own line");
Opens the file corresponding to the path passed as argument and redirects stdin
to that file. This means that every input method, such as READ_INT
or READ_STRING
etc. will read from the file instead of the standard input.
INPUT("grades.csv");
char * first_entry = READ_STRING(100, first_entry);
Opens the file corresponding to the path passed as argument and redirects stdout
to that file. This means that every output method, such as WRITE
or WRITELN
etc. will print to that file instead of the standard output.
OUTPUT("results.txt");
WRITELN("This will be stored in the results.txt file");
Repeats a block of code for a given number of iterations. FOR also checks if the control variable or the loop limit is changed inside the loop, in which cases it throws an error.
You need to specify the following parameters:
- Control variable: The variable that is used to check the loop limits and that will change in each iteration. You need to have declared that variable before the loop block.
- Start value: This will be the initial value of the control variable. Thus, the control variable will have that value in the first iteration.
- End value: This will be the final value of the control variable. Thus, the control variable will have that value in the last iteration. Note that the loop doesn't stop right after the final value is assigned to the control variable, but at the end of that iteration.
- Step value: This will be the step by which the control variable will be increased or decreased. If the step is such that the control variable can't take the exact final value, the loop will be stopped right after the final value is exceeded and the corresponding iteration will not be executed.
The syntax is rather simple. Examples:
int i;
// Use "TO" between the start and the end value when the control variable is increasing
FOR(i, 1 TO 10) {
WRITE(i, ' '); // output: 1 2 3 4 5 6 7 8 9 10
}
int i;
// Use "DOWNTO" when the control variable is decreasing
FOR(i, 10 DOWNTO 1) {
WRITE(i, ' '); // output: 10 9 8 7 6 5 4 3 2 1
}
int i;
// this prints all the even numbers from 2 to 20
FOR(i, 2 TO 20 STEP 2) {
WRITE(i, ' '); // output: 2 4 6 8 10 12 14 16 18 20
}
int i;
// let's combine everything
FOR(i, 17 DOWNTO -5 STEP 4) {
WRITE(i, ' '); // output: 17 13 9 5 1 -3
}
Returns the remainder of the Euclidean division between two operands. If negative numbers are used, the sign of the remainder returned matches the sign of the dividend.
int r = 42 MOD 17;
Returns the result of the AND operation between two boolean operands. Therefore, it returns true
if and only if both of the operands have a boolean value of true
.
if(hour >= 5 AND hour < 12) {
WRITELN("Good morning!");
}
Returns the result of the OR operation between two boolean operands. Therefore, it returns true
if and only if either one of the operands has a boolean value of true
.
if(day == 6 OR day == 7) {
WRITELN("It's weekend, let's PaRtY!");
}
Negates the boolean value of an expression.
if(NOT(month == 12 AND day == 7)) {
WRITELN("It's not Noam Chomsky's birthday yet");
}
Returns the minimum of two values passed as arguments.
int answer = min(17, 42);
Returns the maximum of two values passed as arguments.
int only_answer = max(17, 42);
Dynamically allocates memory. You need to pass the variable type that will be stored, as the first argument, in order to allocate the correct number of bytes.
You can also allocate an array of variables of that type, instead of just one variable, by passing the size of the array as the second argument.
Returns the address of the newly created variable or the address of the first item of the newly created array. That's why we need to store the returned value as a pointer (remember, a pointer is just a variable containing the address of another variable).
int * shoe_size = NEW(int);
*shoe_size = 42;
// this allows you to declare an array whose size may not be known at compile time
int len = READ_INT();
int * shoe_sizes = NEW(int, len);
int i;
FOR(i, 0 TO len - 1) {
shoe_sizes[i] = 42;
}
Frees up memory that had been previously allocated. Receives as an argument a pointer to that memory. If the memory had been allocated to a dynamic array, DELETE will free up all the memory blocks which were occupied by that array.
DELETE(arr);
PZHelp declares the variable type REAL
, used for storing real numbers. REAL
variables accommodate 15 to 16 digits, including both those before and after the decimal point.
REAL pi = 3.14159265358979;
This is the number of decimal digits of precision for the REAL
variable type.
This is the minimum positive real number that is representable in type REAL
.
This is the maximum number representable in type REAL
.
This is the difference between 1 and the smallest number representable in type REAL
that is greater than 1.