Introduction To Programming in Turing Chapter 5: Variables and Constants
Introduction To Programming in Turing Chapter 5: Variables and Constants
SOLUTION:
5-2. Write a program that asks for a person's year of birth and outputs their age in the
current year. Write the program to work for whatever the current year happens to be.4
SOLUTION:
5-3. Write a program that inputs the starting time and finishing time of an automobile trip as
well as the distance in kilometers traveled and outputs the average speed in km/hr. The
times are to be given in two parts: the hours, then the minutes.
SOLUTION:
5-4. Write a program that reads in four numbers and then outputs the four numbers all on one
line with commas between the numbers.
SOLUTION:
5-5. Write a program to input a name and output a message that greets a name. For
example, if the name is "Sue", then the message might be "Hello Sue!". Use constants for
the greeting.
SOLUTION:
SOLUTION:
5-7. Write a program that inputs five full names and outputs the names in reverse order.
SOLUTION:
5-8. Write a program that inputs a first name and a last name then outputs this in the form last
name, first name.
Try to write a version which inputs the two names from a single input line.
SOLUTION:
Try to write a version which inputs the two names from a single input line.
or
5-9. Experiment with programs where you purposely make syntax errors to see what the error
messages are like.
SOLUTION:
5-10. Experiment with programs where you purposely input the wrong type of data to
see what happens. For example, enter an integer when a string is expected.
SOLUTION:
SOLUTION:
age := age + 1
This program line reassigns the variable age to be one more than the original
value of age. Thus original value of age will be overwritten.
Yes, it would make the program more understandable. Since the program
calculates the age-next-year based on the current age, it makes more sense to
create and use a variable that will be assigned the value of the age-next-year.
Introduction to Programming in Turing
Chapter 5: Variables and Constants
5 -12. Experiment with adding comments to a program. See if you can add a comment to the
end of a line. Can a comment be in the middle of a line?
SOLUTION:
% The "05-12 Soln" Program
var age : int % This line declares the
% variable age as an integer.
put "Enter age" % This line prompts for input.
get age % This line temporarily halts
% execution of the program and
% waits for input.
age := age + 1 % This is an assignment
%statement.
put "age is ", age % This is an output line
% that blends a string
% literal and a variable
A comment line cannot be in the middle of a line since its definition is that it begins with
a % sign and ends with a Return.
Introduction to Programming in Turing
Chapter 5: Variables and Constants
5-13. The Prom Committee at your school will sell tickets at $65 per person. Expenses
include the cost of the food, the DJ, the hall, the decorations, and the waiting staff. To these
expenses, add $100 for miscellaneous expenditures. Write a program to ask for each of the
expense totals incurred and then calculate and output to the committee how many tickets
they must sell to break even.
SOLUTION:
5-14. A student wrote 5 tests. Ask the student for their name and then what each test is marked
out of and what mark they received on each test. At the end of the run calculate and output
the percentage for each test as well as the average on all five tes also as a percent. When
querying the student, address each request with their name Make sure that output
statements include the name of the student.
SOLUTION:
"..
get test3Total
test3Average := test3Markl / test3Total* 100
put""
put "Enter the mark for your fourth test",
name,":"..
get test4Mark
put "What is the maximum mark value for this test? "
..
get test4Total
test4Average := test4Mark / test4Total* 100
put""
put "Enter the mark for your fifth test", name,":"
..
get test5Mark
put "What is the maximum mark value for this test? "
..
get test5Total
test5Average := test5Mark / test5Total* 100
put""
put "The average for", test1 Mark," /",
test1Total," is ", test1 Average, " %"
put "The average for", test2Mark," /", test2Total,
" is ", test2'Average, " %"
put "The average for", test3Mark," /",
test3Total," is ", test3Average, " %"
put "The average for", test4Mark, " /", test4Total,
" is ", test4Average, " %
put "The average for", test5Mark," /",
test5Total," is ", testSAverage," %"
put ""
put "Your overall average ", name,", is ",
(test1Mark + test2Mark + test3Mark+ test4Mark +
test5Mark) / (test1Total + test2Total +
test3Total+ test4Total+ test5Total) * 100," %"
Introduction to Programming in Turing
Chapter 5: Variables and Constants
5-15. Ask the user for a real number which expresses the area of a figure. Assume that the figure
was a circle. Output the radius and then the circumference of the circle. Now assume that
the figure was a square. Output the length and width and then the perimeter of the square.
Use complete statements in each case.
SOLUTION:
Assume that the figure was a circle. Output the radius and then the circumference of the
circle.
Now assume that the figure was a square. Output the length and width and then the
perimeter of the square.