Turing/Answers To Programming Language Exercises

From Wikiversity
Jump to navigation Jump to search

Lecture 1[edit]

Introduction to Turing Programming Language

1. Make a program that outputs a string on one line and then another one on the next.

put "Hello, World"
put "This is another line"

2. Make the second example print out both strings on the same line, without removing the second put. Note: You will have to do some research on this.

put "This part ends, ".. %This command is very useful for get statements after text
put "But this one starts immediatetly after."

Lecture 2[edit]

Variables and Types in Turing

Exercises:

1.Make a program that has the user input their name and then prints it out (Hint: to print a variable, you cannot put quotation marks around it)

var name : string
put "Please enter your name here: "..
get name
put "Your name is "..
put name

2.Make a variable that stores 10 names in an array, and if you want, prints them out. You will learn a more efficient way of doing this next lesson.

var names : array 1..10 of string
get names (1)
get names (2)
get names (3)
get names (4)
get names (5)
get names (6)
get names (7)
get names (8)
get names (9)
get names (10)
put names (1)
put names (2)
put names (3)
put names (4)
put names (5)
put names (6)
put names (7)
put names (8)
put names (9)
put names (10)

Lecture 3[edit]

Control Structures and Logical Expressions in Turing

1.Make the user input a user name and password. (Hint: If statement)

var username, password : string
put "What is your username?" ..
get username
put "What is your password?" ..
get password
if (username = "user1") and (password = "drowssap") then
    put "Welcome, username
else
    put "Incorrect password or username"
end if

2.Make the program give the user as many tries as they want, or if you want a challenge, 5 tries (Hint: Loop)

var username, password : string
var tries : int %Anything in this code with a '%' after it is for the challenge
loop
put "What is your username?" ..
get username
put "What is your password?" ..
get password
if (username = "user1") and (password = "drowssap") then
    put "Welcome, username
else
    put "Incorrect password or username"
end if
tries += 1 %
exit when tries = 5 %
end loop

3.Re-do exercise 2 (input 10 values from an array) from the last one, with for statements

var names : array 1..10 of int
for i : 1..10
get name (i)
end for
for i : 1..10
put name (i)
end for

Lecture 4[edit]

Functions and Subroutines 1.Make a function that calculates the number times 6, plus 3 divided by 2

var iput : int
get iput
function math (x : int) : real
    result (x * 6 + 3) / 2
end math
put math (iput)

2.Make a procedure that puts 'hi' and then one that gets a string. Then call on them at random. (Hint: Use Rand.Int: Rand.Int (1,5) randomly outputs a number between 1 and 5.)

var iput : string
var which : int
procedure hi
    put hi
end hi
prodedure getiput
    get iput
end getiput
loop
    which := Rand.Int (1,2)
    if (which = 1) then
         hi
    elsif (which = 2) then
         getiput
    end if
end loop