14

Click here to load reader

Introduction to Programming in Turing Chapter 5: Variables and Constants

Embed Size (px)

Citation preview

Page 1: Introduction to Programming in Turing Chapter 5: Variables and Constants

Introduction to Programming in Turing Chapter 5: Variables and Constants

Chapter 5: Variables and Constants - Section 5.11 5-1. There are 2.54 cm in one inch. Write a program to input the length of a desk in inches and

output its length in centimeters. Use a constant for the conversion factor. Be sure to prompt for the input and to label the output.

SOLUTION:

% The "05-01 Soln" Program const factor := 2.54 % number of cm in 1 inch put "Enter the length of the desk in inches: " .. var len : real get len put "The length of the desk in centimeters is:",

len * factor," cm."

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: % The "05-02 Soln" Program const currentYear := 2004 put "Enter the year you were born (e.g. 1957):" .. var birthYear: int get birthYear put "In ", currentYear," you will be ",

currentYear - birthYear," years old"

Page 2: Introduction to Programming in Turing Chapter 5: Variables and Constants

Introduction to Programming in Turing Chapter 5: Variables and Constants

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: % The "05-03 Soln" Program var leaveHour, leaveMin, arriveHour, arriveMin: int put "What time did you leave on your trip?" put "Enter the hour (24-hour clock):" .. get leaveHour put "Enter the minute:" .. get leaveMin put "What time did you arrive?" put "Enter the hour (24-hour clock):" .. get arriveHour if arriveHour < leaveHour then

arriveHour := arriveHour + 24 % arrive next day end if put "Enter the minute: " .. get arriveMin % Calculate the duration in hours const duration : real := (arriveHour*60+ arriveMin) - (leaveHour * 60 + leaveMin) / 60

put "How far did you drive (in kilometers):".. var distance: real get distance put "Your average speed was ", distance / duration : 5 : 2," km/hr"

Page 3: Introduction to Programming in Turing Chapter 5: Variables and Constants

Introduction to Programming in Turing Chapter 5: Variables and Constants

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:

% The “05-04 Soln” Progrma var number1, number2, number3, number4 : int put “Enter 4 numbers” get number1, number2, number3, number4 put “Here are the 4 numbers that you entered: “ .. put number1, “, “,number2, “, “,number3, “, “,number4, “, “

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: % The "Ch05-05" Program const GREETING := "Hello " const PUNCTUATION := "!" var name : string put "Enter your name please" get name put GREETING, name, PUNCTUATION

Page 4: Introduction to Programming in Turing Chapter 5: Variables and Constants

Introduction to Programming in Turing Chapter 5: Variables and Constants

5-6. Write a program to calculate and output the product of three numbers entered via the keyboard. Also output the square of the product.

SOLUTION:

% The "Ch05-06 " Program var number1, number2, number3, answer: int put "Enter three numbers" get number1 number2, number3 answer := number1* number2* number3 put "The product of ", number1, " * ", number2," * ", number3," = ", answer put"" put "The square of", answer," is ", answer ** 2

Page 5: Introduction to Programming in Turing Chapter 5: Variables and Constants

Introduction to Programming in Turing Chapter 5: Variables and Constants

5-7. Write a program that inputs five full names and outputs the names in reverse order.

SOLUTION:

% The "Ch05-07 " Program var name1, name2, name3, name4, name5 : string put "Enter the full names for five people" put "What is the first full name?" get name1 : * put "What is the second full name?" get name2: * put "What is the third full name?" get name3: * put "What is the fourth full name?" get name4: * put "What is the fifth full name?" get name5: * put "The names in reverse order are" put name5, ",", name4, ",", name3, ",", name2, ",", name1

Page 6: Introduction to Programming in Turing Chapter 5: Variables and Constants

Introduction to Programming in Turing Chapter 5: Variables and Constants

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:

% The "Ch05-08a " Program var firstName, lastName : string put "Enter your first and last name" get firstName get lastName put lastName,",", firstName Try to write a version which inputs the two names from a single input line. % The "Ch05-08b " Program var name, firstName, lastName : string put "Enter your first and last name" get firstName, lastName put lastName,",", firstName or % The "Ch05-08c " Program % Uses the predefined string function "index" var name, firstName, lastName : string put "Enter your first and last name" get name : * firstName := name (1 .. index (name, "")) lastName := name (index (name,"") + 1 .. *) put lastName, ", ", firstName

Page 7: Introduction to Programming in Turing Chapter 5: Variables and Constants

Introduction to Programming in Turing Chapter 5: Variables and Constants

5-9. Experiment with programs where you purposely make syntax errors to see what the error

messages are like.

SOLUTION:

% The "05-09a Soln" Program % Error free version. var name: string put "Enter a name" get name put "Hello", name Some programs with typical errors

% The "05-09b Soln" Program % Error message = 'string' has not been declared. put "Enter a name" get name put "Hello", name

% The "05-09c Soln" Program % Error message = Syntax error at 'string', expected ':'. var name string put "Enter a name" get name put "Hello", name % The "05-09d Soln" Program % Error message = String literal ends at end of line. var name : string put "Enter a name get name put "Hello ", name

Page 8: Introduction to Programming in Turing Chapter 5: Variables and Constants

Introduction to Programming in Turing Chapter 5: Variables and Constants

% The "05-09e Soln" Program % Error message = 'name' is not a procedure and hence cannot be called. var name : string put "Enter a name" get name put "Hello" name

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: % The "05-10a Soln" Program % This program expects string input. var name: string put "Enter a name" get name put "Hello ", name Entering an integer when a string is expected does not produce an error. % The "05-1 Ob Soln" Program % This program expects integer input var age: int put "Enter your age" get age put "Next year you will be ", age + 1," years old." Entering a string like 'abc' when an integer is expected produces an error of 'Invalid integer input.'

Page 9: Introduction to Programming in Turing Chapter 5: Variables and Constants

Introduction to Programming in Turing Chapter 5: Variables and Constants

5-11. See what happens when you run this program

var age: int put "Enter age" get age age := age + 1 put "age is ", age How do you interpret the assignment statement age := age + 1 Would another variable called ageNextYear make the program more understandable? SOLUTION:

How do you interpret the assignment statement 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. Would another variable called ageNextYear make the program more understandable? 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.

Page 10: Introduction to Programming in Turing Chapter 5: Variables and Constants

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.

Page 11: Introduction to Programming in Turing Chapter 5: Variables and Constants

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: % The "05-13 Soln' Program const MISC:= 100 const TICKETCOST := 65 var foodCost, diskJockey, hallRental, decorations, waitingStaff, total: real put "Enter the cost of food" get foodCost put "Enter the cost of the DJ" get diskJockey put "Enter the cost of the rental of the hall" get hallRental put "Enter the cost of the decorations" get decorations put "Enter the cost of the waiting staff" get waitingStaff put “” total:= foodCost + diskJockey + hallRental + decorations + waitingStaff+ MISC put "To break even you must sell ", total/ TICKETCOST," tickets"

Page 12: Introduction to Programming in Turing Chapter 5: Variables and Constants

Introduction to Programming in Turing Chapter 5: Variables and Constants

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:

% The "05-14 Soln" Program var test1Mark, test2Mark, test3Mark, test4Mark, test5Mark: real var test1Total, test2Total, test3Total, test4Total, test5Total: real var test1 Average, test2Average, test3Average, test4Average, test5Average : real var name: string put "Please enter your name" get name put"" put "Enter the mark for your first test", name,":".. get test1Mark put "What is the maximum mark value for this test? ".. get test1Total test1 Average := test1 Mark / test1Total* 100 put"" put "Enter the mark for your second test", name,":" .. get test2Mark put "What is the maximum mark value for this test? ".. get test2Total test2Average := test2Markl / test2Total* 100 put"" put "Enter the mark for your third test", name,":" .. get test3Mark put "What is the maximum mark value for this test?

Page 13: Introduction to Programming in Turing Chapter 5: Variables and Constants

Introduction to Programming in Turing Chapter 5: Variables and Constants

".. 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," %"

Page 14: Introduction to Programming in Turing Chapter 5: Variables and Constants

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. % The "Ch05-15a" Program var area, circumference, radius: real const PI:= 3.14159 put "Enter the area of a circle:".. get area radius := sqrt (area / PI) circumference := 2 * radius * PI put "The radius of the circle is ", radius put "The circumference of the circle is ", circumference Now assume that the figure was a square. Output the length and width and then the perimeter of the square. % The "Ch05-15b" Program var area, widthLength, perimeter : real put "Enter the area of the square ".. get area widthLength := sqrt (area) perimeter := widthLength * 2 + widthLength * 2 put "The width and length of the square is ", widthLength put "The perimeter of the square is ", perimeter