Exercises from AQA book

advertisement
P4
1.
2.
3.
4.
5.
6.
7.
8.
What is meant by computation?
What did professor Dijkstra say about computing?
What is meant by computability?
What is computing?
What is artificial intelligence?
What is an algorithm?
What is the purpose of an algorithm?
An algorithm is not the same thing as a computer program. Do you agree? Explain your
answer.
9. Is there a route in Fig.1.1.1 that takes the salesperson through every city and back to the
starting city A with a cost of £480 at most?
10. What are the two main principles of computation?
P8
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
Explain what is meant by “problem”
Explain what is meant by “problem solving”
Explain what is meant by “to understand the problem”
Name the four components a well-defined problem should have
Give one reason for a problem remaining unsolved in a situation involving a team of people
Explain what is meant by “constraints” in the context of problem solving.
Explain why challenging your assumptions is an important aspect of problem solving.
What is meant by “defining boundaries” in problem solving?
What other kinds of constraints occur in problem solving other than boundary constraints?
Why is lateral thinking useful in problem solving?
How would you use lateral thinking in a problem-solving situation?
State five considerations in planning a solution
What is a module in the context of problem solving?
Explain (a) top-down design and (b) stepwise refinement
What is a structure table?
Show how you would use stepwise refinement to plan a route between London and
Manchester
P18
1. Figure 1.3.6 shows an FSM and the state transition diagram with which it is programmed.
The machine accepts input sequences consisting of the letters a and b, e.g. aabbbabaab. The
machine has a lamp labelled ACCEPT. The machine starts out in state 1 and the accepting
state is also state 1. When the machine is in state 1 after the machine has exhausted the
input, the lamp lights. What does this machine detect and indicate with the lamp?
2. Draw a state transition diagram, for a combination lock with combination 956. Draw the
corresponding state transition table.
3. Draw state transition diagram with output for an FSM that gives an output of 1 if and only if
the input string read so far ends with 111 otherwise it gives an output of 0.
4. Draw a decision table for the following logic:
if X is greater than 6 or Y is less than 7
Then Output “Pass”
Else Output “Fail”
5. Name the three constructs that may be used to construct any algorithm. Explain each
construct.
6. Use stepwise refinement to develop an algorithm for fixing a puncture in the front wheel of
a bicycle
7. Use stepwise refinement to develop an algorithm to stack 24 chairs in classroom with no
more than 6 chairs per stack.
8. Name the four major steps to be used in computer-based problem solving
9. What is meant by assignment?
10. Find and display the largest of three numbers entered through a keyboard. Solve this
problem using four major steps method.
11. Hand trace the following algorithm using as input the numbers 2, 6, 34, 12, 0 in that order.
Result  0
Repeat
Input n
Result  Result + n
Until n=0
12. The following problem has been set. A right-angled triangle has side a, b and c. Pythagoras’
theorem states that a2=b2+c2. The problem is to create a process that will find and output
side a for any given inputs b and c. Express the solution to this problem in (a) structured
English and (b) pseudocode.
13. Draw a program flow chart for the algorithm in Question 11.
P29
Using the case convention of your chosen programming language, choose meaningful variable
identifiers to store these values
1.
2.
3.
4.
5.
The total points scored by player 1 in a game
The player number whose turn is next
The number of questions answered correctly
The total number of questions in a quiz
The number of questions that have been attempted
Using the programming language of your choice and sensible variable identifiers, write assignment
statements to calculate these values:
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
The number of questions answered wrongly so far in the quiz
The percentage of questions answered correctly from those attempted
The percentage of questions attempted out of the total number of questions in the quiz
How many £5 notes you would get for an amount of money in pounds stored in the variable
amount.
Write programs to do the following:
Display the word “Hello World!” on screen
Display two messages of your choice on screen, separated by an empty line
Display the sum and average of three numbers entered at the keyboard
Read in two integers and display how many times the first integer divide into the second one
(using integer division). Display the remainder from this division. (use modulo operation)
The user enters an amount of money as a whole number. The program should calculate and
display how many £20, £10, £5 notes and £2 and £1 coins to make up this amount of money.
For example, £37 would give 1x£20, 1x£10, 1x£5, 1x£2. Hint; use integer division and the
modulo operation.
Extra (2.2)
1. When would you use a global variable?
2. When would you use a local variable?
When would you use a constant?
3. What are differences between integer, double, float, single, etc.
4. What is a primitive data type and what is a user-defined type? What is enumerated type?
5. How does logical bitwise operator work? NOT, AND, OR, XOR
P47
In the language of your choice, write programs to evaluate these Boolean expressions
Assignment is to assign a value to a variable. Expressions are sequences of operators and operands
that are used for one or more of these purposes:
Computing a value from the operands.
Designating objects or functions.
Generating "side effects." (Side effects are any actions other than the evaluation of the expression
— for example, modifying the value of an object.)
1.
2.
3.
4.
5.
6.
Number is equal to zero
Number is non-zero
Number is less than or equal to zero
Number is between 1 and 20 exclusive
Number is between 1 and 20 inclusive
Number is between 1 and 10 or between 100 and 120 exclusive
Write a program to do the following
1. Display a message if two numbers read from the keyboard are the same.
2. Display a message if the first number read from the keyboard is greater than the second
one.
P49
1. Ask the user’s age to be typed in at the keyboard, then display a message that states
whether or not the user is old enough to drive/
2. Ask for two numbers to be typed in at the keyboard, then display the larger of the two
3. Ask for a number to be typed in at the keyboard, then display a message to say whether
this number is within the range of 0 to 100.
P50
1. Ask for a number to be typed in at the keyboard, then display a message to say whether
the number is within the range of 10 to 20, above this range or below this range.
2. Ask for the user’s age to be typed in at the keyboard, then display a message to say
whether the user is a child, teenager or an adult
3. Ask for three numbers to be typed in at the keyboard, then display the largest one.
P51
1. Ask the user for a month number, then display the name of the month; for example, the
input 9 should produce a display of September.
2. Ask the user for a year, then display whether this year is a leap year. Hint: use the modulo
operation
3. Ask the user for a month number, then display the number of days in that month
4. Ask the user for a month number, display the number of day in that month, taking into
account of leap years
5. Input a hexadecimal number, display the denary (decimal) equivalent
P52
Display the message “Hello World!” 25 times
Ask the user for a message and then ask for the number of times it should be repeated for display,
then display the message with the number of repetition
Ask the user which times table they wish to see, then display it in a list format
1*7=7
2*7=14
….
12*7=84
Ask the user to enter 10 numbers, then display the largest number
Display them all in descending order, ascending order.
Ask the user how many star they wish to see in a row. Then display the row of stars.
Expand the answer to previous question - ask how many rows to see, then display the rectangle of
stars. (Hint: nested if)
P53
1. Declare an array to store 5 integers. Initialise each element with an integer of your choice.
Display the numbers on screen.
2. Expand your answer above, display the sum of the numbers too. (Hint: use a variable to keep
the running total, so each round of the loop the number is added to total.)
1. Ask for the user to type in a name at the keyboard, if the input is “X” then the program
terminates. (my insertion)
2. Expand the above to count how many names have been entered
3. Add up a series of numbers entered by the user, display the total, the program terminates
when the input is 0.
4. Expand the above to display the average of the inputs, not including the last 0.
P54
1. Ask a user to enter a whole number greater than 99. If the number is greater than 99 display
an error message and ask again, repeat this process until the user enters a valid number.
2. Expand your solution to accept between 100 and 200
A statement block
P57
1. Read a name consisting first name and surname. Retrieve the initial letter of the first name.
Find the beginning location surname, extract the initial letter from the surname. Display the
initials.
2. Read a date as a string from the keyboard, convert it to a date type, add a year, convert it
back to string, display it on screen (console)
P59
1. Declare and test a function ConvertToCelsius to convert a temperature given in Fahrenheit
to Celsius. The conversion formula is DegreesCelsius = (DegreesFahrenheit-32)*(5/9)
2. Declare and test a function ConvertToKg to convert imperial pounds to kilograms (1Kg =
2.2lb)
3. Declare and test a function IsLeapYear that will tell you if a year is a leap year
P65
1. Write and test a procedure DisplaySymbols which takes two parameters, an integer x and a
character s. the procedure is to display x number of symbol s on the same line. For example,
a procedure call DisplaySymbols(3,’*’) should display *** in the console window.
2. Expand your solution to Q1 so that the user is asked how many of the symbols and which of
the symbol should be displayed.
3. Expand your answer to Q1. Write and test a procedure DisplayRectangle that calls
DisplaySymbols so that the output resembles a rectangle of symbols. For example, the
procedure call DisplaySymbols(4,3,’%’) should display
%%%%
%%%%
%%%%
4. Write a program to simulate the game “last one loses”. The game is for two players and
starts with a set of x counters. The players take turns at removing 1, 2, or 3 counters from
the set. The game is over when there are more counters. The player who took the last
counter loses.
5. Expand your solution to Q4 to let one player play against the computer. You will need to use
random functions to simulate the computer choosing to take 1, 2 or 3 counters.
6. Modify the calls to parameters by reference instead of value
P68
1. Write down the reference for the element that contains (a) Harry, (b) Zak and (c) Fred
2. Write down the contents of the element referred to by (a) Student[5], (b) Student[0] and
Student[7]
Student
Fred
Jack
0
1
Chris
Ali
Harry
Bill
Zak
Phil
2
3
4
5
6
7
P69
1. Initialise a string array with eight elements with the names shown in the array Student.
Display these names in the order given and then in reverse order
2. Declare an integer array of ten elements. Fill the elements with the values the user types in
at the keyboard. Display these values on the console
3. Expand your program above to calculate the sum and average of the numbers stored in the
array. Display each element with a message stating if the number is below, above or equal to
the average.
4. Expand the solution above to count how many numbers were above the average and how
many numbers were below the average. Display your results.
5. Implement the worked example on P69. Display the value of each element as a value, then
as symbols that make it look more like a conventional tally chart.
P70
1. Initialise a string array of eight elements with the names shown in the array Student.
Initialise an integer array of eight elements with marks for each of them (6, 25, 92, 49, 38, 72,
99, 54). Display names with relevant marks next to them on the console
2. Expand your program to calculate the average of the marks, display name, mark and a
message stating above, below or equal to the average
3. Expand the program to count how many were above and below the average. Display the
results.
P71
1. Using array Distance, write down the reference for the element that contains (a) the
distance between Oxford and Manchester, (b) the distance between Manchester and
London
2. Using array Distance, write down the meaning of the content referred to by following
notation. Distance[1,2]; Distance[2,1]; Distance[0,4]; Distance[0,0]
Distance
London
Manchester
Oxford
Sheffield
London
184
56
160
Manchester
184
144
38
Oxford
56
144
130
Sheffield
160
38
130
-
P72
1. Declare a two-dimensional integer array and store the distance chart values given above.
Declare a one-dimensional string array to store the city names. Display on the console the
name of each city, except London, and the distance from London
2. Expand the program to display the chart as shown above (distance)
3. Find out about the game Battleship. Declare a 10x10 character array and initialise it with
some ships. Ask the user to input the coordinates for the target. The program should
respond with whether this was a hit or not. When all areas of the ships have been hit, the
program should respond with “Game Over” and display the number of shots required.
P73
1. Using the array Student2, write down the data items referred to by Student2[5].Name and
Student2[0].Marks.
2. Using the array Student2, write down the reference for the field that contains (a) Harry’s
marks and (b) Zak’s name
Student
Name: Fred
Marks: 67
Name: Jack
Marks: 25
Name: Chris
Marks: 92
Name: Ali
Marks: 49
Name: Harry
Marks: 38
Name: Bill
Marks: 72
Name: Zak
Marks: 99
Name: Phil
Marks: 54
0
1
2
3
4
5
6
7
P73
1. Declare a record type TCar with fields Make, Model, EngineSize and Price. Declare an array
of type TCar to store 5 cars. Set initial values and then display the cars
2. Extend your program to calculate the average price of the cars. Display the cars with a
message stating whether the price is below, above or equal to the average price
3. Expand your program to count how many cars have the price below or above the average
price. Display the results.
P83 xxxxx
1. Read from an existing text file and display the contents of the file on the console. Create
your text file in your text editor.
2. Expand the program above to count the number of lines in the file and display the total.
3. Expand the program to count the number of words read. (Hint: a word terminates with a
space, punctuation marks or the end of line. You need to use string handling functions to
find these characters)
4. Declare a record type student details and store in a variable of this type once student’s
details entered at the keyboard. Before the program terminates, it should save the records
into a binary file.
5. Expand the above solution to store student’s record in an array of records and store the
contents of the array in a file before the program terminates.
Download