Difference between revisions of "2016 New Spec"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "=AS Question 5= The algorithm, represented using pseudo-code in Figure 4, describes a method to calculate the additive or multiplicative persistence of a two-digit integer. Th...")
 
(AS Question 5)
 
(One intermediate revision by the same user not shown)
Line 3: Line 3:
  
 
'''Example:''' calculating the additive persistence of 87  
 
'''Example:''' calculating the additive persistence of 87  
8 + 7 = 15
+
8 + 7 = 15
 
  1 + 5 = 6  
 
  1 + 5 = 6  
 
After 2 steps the method results in a one digit answer so the additive persistence of 87 is 2.  
 
After 2 steps the method results in a one digit answer so the additive persistence of 87 is 2.  
Line 11: Line 11:
 
  2 * 7 = 14  
 
  2 * 7 = 14  
 
  1 * 4 = 4  
 
  1 * 4 = 4  
After 3 steps the method results in a one digit answer so the multiplicative persistence of 39 is 3.  
+
After 3 steps the method results in a one digit answer so the multiplicative persistence of 39 is 3.
  
 
=Figure 4=
 
=Figure 4=
OUTPUT "Enter integer (0-99): "
+
OUTPUT "Enter integer (0-99): " <br/>
INPUT Value
+
INPUT Value <br/>
OUTPUT "Calculate additive or multiplicative persistence (a or m)? "
+
OUTPUT "Calculate additive or multiplicative persistence (a or m)? " <br/>
INPUT Operation
+
INPUT Operation <br/>
Count = 0
+
Count = 0 <br/>
WHILE Value > 9
+
WHILE Value > 9 <br/>
:IF Operation = "a" THEN
+
:IF Operation = "a" THEN <br/>
::Value = (Value DIV 10) + (Value MOD 10)
+
::Value = (Value DIV 10) + (Value MOD 10)<br/>
:ELSE
+
:ELSE<br/>
::Value = (Value DIV 10) * (Value MOD 10)
+
::Value = (Value DIV 10) * (Value MOD 10)<br/>
:ENDIF
+
:ENDIF<br/>
:Count = Count + 1
+
:Count = Count + 1<br/>
ENDWHILE
+
ENDWHILE<br/>
OUTPUT "The persistence is: "
+
OUTPUT "The persistence is: "<br/>
OUTPUT Count
+
OUTPUT Count<br/>
 
   
 
   
The MOD operator calculates the remainder resulting from an integer division, for example, 10 MOD 3 = 1. The DIV operator calculates integer division, for example 10 DIV 3 = 3.  
+
The MOD operator calculates the remainder resulting from an integer division, for example, 10 MOD 3 = 1. The DIV operator calculates integer division, for example 10 DIV 3 = 3.
  
 
=What you need to do=  
 
=What you need to do=  

Latest revision as of 13:08, 29 November 2016

AS Question 5

The algorithm, represented using pseudo-code in Figure 4, describes a method to calculate the additive or multiplicative persistence of a two-digit integer. The examples below illustrate how additive and multiplicative persistence are calculated.

Example: calculating the additive persistence of 87

8 + 7 = 15
1 + 5 = 6 

After 2 steps the method results in a one digit answer so the additive persistence of 87 is 2.

Example: calculating the multiplicative persistence of 39

3 * 9 = 27
2 * 7 = 14 
1 * 4 = 4 

After 3 steps the method results in a one digit answer so the multiplicative persistence of 39 is 3.

Figure 4

OUTPUT "Enter integer (0-99): "
INPUT Value
OUTPUT "Calculate additive or multiplicative persistence (a or m)? "
INPUT Operation
Count = 0
WHILE Value > 9

IF Operation = "a" THEN
Value = (Value DIV 10) + (Value MOD 10)
ELSE
Value = (Value DIV 10) * (Value MOD 10)
ENDIF
Count = Count + 1

ENDWHILE
OUTPUT "The persistence is: "
OUTPUT Count

The MOD operator calculates the remainder resulting from an integer division, for example, 10 MOD 3 = 1. The DIV operator calculates integer division, for example 10 DIV 3 = 3.

What you need to do

  1. Write a program for the algorithm in Figure 4.
  2. Test the program by showing the result of entering 47, followed by m when prompted by the program.
  3. Test the program by showing the result of entering 77, followed by a when prompted by the program.