Your first program
Write, generate and run a program that reads a value and displays a result.
The smallest program
Algorithm Helloversion '1.0'language Cbeginwrite("Hello, world!\n")endend algorithm
Output
Hello, world!hello.aml on GitHubTry itSoonEvery program has the same frame:
Algorithm Hellogives the program its name. The generated Java class and the file names use it.versionis a free text, between quotes.language Cchooses the language to generate. Change it toJAVAorPYTHONand generate again: the algorithm stays the same.- The instructions go between
beginandend, and the file ends withend algorithm.
write displays text and values. "\n" goes to the next line.
Reading values
A program becomes useful when it works on data given by the user. Declare variables before begin, then read them with read.
Algorithm Greetingversion '1.0'language Cvariable name : STRINGvariable age : INTbeginwrite("What is your name? ")read(name)write("How old are you? ")read(age)write("Hello ", name, ", next year you will be ", age + 1, ".\n")endend algorithm
Input
Ada 36Output What is your name? How old are you? Hello Ada, next year you will be 37.greeting.aml on GitHubTry itSoonvariable name : STRINGdeclares a variable callednamethat holds text.read(name)waits for the user to type a value and stores it inname.writeaccepts several values separated by commas and displays them one after the other, with no space added. Put the spaces in your strings.
Generate and run
In VS Code, open the file and run the AlgoML generate command. From a terminal:
algoml-generate greeting.aml .gcc Greeting.c -o greeting./greeting
For Java, compile with javac Greeting.java and run java Greeting. For Python, run python3 Greeting.py.
Exercise
Exercise
Read a number of minutes and display it in hours and minutes. For example, 135 gives 2 h 15 min. Hint: div gives the quotient of a division and mod gives the remainder.