Z
Suppose that Zamyla has designed a new programming language called Z. The language is "loosely typed," which means you needn’t specify a variable’s type; values that look like integers will be treated as integers, values that look like strings will be treated as strings, and true
and false
will be treated as Booleans. As in C, a program written in Z must have a function called main
, which is automatically called when the program is executed. However, in Z, functions don’t need to be declared, as with prototypes, before they are called. As in C, comments in Z begin with //
. But the language comprises only these building blocks, wherein { }
denotes one or more lines of code:
function name(parameters) { }
-
which defines a function called
name
that accepts as input a comma-separated list of zero or more parameters return(value)
-
which returns a single
value
from a function $variable
-
which represents a variable (of any type) called
$variable
x <- y
-
which stores
y
inx
, wherex
is necessarily a variable if (x) { }
-
which executes any code within
{ }
if and only ifx
istrue
x < y
-
which evaluates to
true
ifx
is less thany
andfalse
otherwise add(x, y)
-
which returns the sum of
x
andy
, which are assumed to be integers get()
-
which gets a value (of any type) from the user and returns it
not(x)
-
which converts
true
tofalse
andfalse
totrue
print(x)
-
which prints
x
(of any type) and supports escape sequences like\n
Z has no other features. In particular, it does not support binary operators like +
, -
, *
, /
, %
, &&
, ||
, !
, >
, >=
, ==
, or <=
(though it does support -
as a prefix for negative integers). And it does not have loops. But you can still program in it!
For instance, whereas in C you might write:
x = x - y;
in Z you would write:
$x <- add($x, -$y)
And here’s a whole program in Z that gets a user’s name name and then says hello:
function main()
{
// get user's name
$name <- get()
// say hello
hello($name)
}
function hello($s)
{
// print "hello, $s\n", where $s is user's name
print("hello, ")
print($s)
print("\n")
}
Answer the below in z.txt
.
Questions
-
(4 points.) Write a program in Z that gets two integers from the user and then prints "equal" if and only if the two integers are equal. Assume that the user will indeed input integers.
-
(6 points.) Write a program in Z that gets an integer from the user and then prints, one per line, all of the integers from that number through (i.e., including) 0, starting with that integer and ending with 0. Assume that the user will input a positive integer.
Debrief
-
Which resources, if any, did you find helpful in answering this problem’s questions?
-
About how long did you spend on this problem’s questions?