Modula-2 Shopping Cart - Modula2 Typing CST Test
Loading…
Modula-2 Shopping Cart — Modula2 Code
Adds and removes items in a shopping cart with total cost.
MODULE ShoppingCart;
IMPORT InOut;
VAR cart: ARRAY 10 OF ARRAY 50 OF CHAR; prices: ARRAY 10 OF INTEGER; count: INTEGER := 0;
PROCEDURE addItem(item: ARRAY OF CHAR; price: INTEGER);
BEGIN
cart[count] := item; prices[count] := price; count := count + 1;
(* print cart and total *)
END addItem;
PROCEDURE removeItem(idx: INTEGER);
VAR i: INTEGER;
BEGIN
FOR i := idx TO count-2 DO cart[i] := cart[i+1]; prices[i] := prices[i+1] END;
count := count -1;
(* print cart and total *)
END removeItem;
BEGIN
addItem("Apple", 2);
addItem("Banana", 3);
removeItem(0);
END ShoppingCart.Modula2 Language Guide
Modula-2 is a statically typed, modular, procedural programming language designed for systems programming and teaching structured programming concepts, created as a successor to Pascal.
Primary Use Cases
- ▸Teaching structured and modular programming
- ▸Systems programming and embedded applications
- ▸Operating system and compiler development
- ▸Prototyping modular software architectures
- ▸Applications requiring strong type safety
Notable Features
- ▸Module system for encapsulation
- ▸Strong static typing
- ▸Procedural programming with structured control
- ▸Separate compilation for modules
- ▸Support for low-level systems programming
Origin & Creator
Developed by Niklaus Wirth in 1978 at ETH Zurich as a follow-up to Pascal, focusing on modularity and systems programming.
Industrial Note
Modula-2 found niche use in teaching programming concepts, embedded systems, and certain early OS development projects, though modern usage is mostly academic.