Programming Task
Programming Task
Write a program that asks the user to enter a string. It should then change the order of the
vowels in the string and display the result.
If there are n vowels in the string, the 1st vowel in the string should swap with the nth
vowel in the string, the 2nd vowel in the string should swap with the (n-1)th vowel in the
string, and so on.
Examples
If the user enters the string horse then the program should display
the string herso.
If the user enters the string goose then the program should display
the string geoso.
If the user enters the string nakedmolerat then the program should
display the string nakedmolerat.
If the user enters the string lynx then the program should display
the string lynx.
If the user enters the string pig then the program should display the
string pig.
You may assume the string that the user enters will only contain lowercase letters.
(b) SCREEN CAPTURE(S) showing the results of three tests of the program by
entering the strings persepolis, darius and xerxes.
(1)
(Total 13 marks)
Q2.
A Harshad number is a positive integer which is exactly divisible by the sum of its digits.
The first twelve Harshad numbers are 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12 and 18
• 36 is a Harshad number. The digits of 36 are 3 and 6; the sum of these digits is 9 as
3 + 6 = 9 and 36 is exactly divisible by 9 (36 ÷ 9 = 4)
• 300 is a Harshad number. The digits of 300 are 3, 0 and 0; the sum of these digits is
3 as 3 + 0 + 0 = 3 and 300 is exactly divisible by 3 (300 ÷ 3 = 100)
Page 1 of 21
• 15 is not a Harshad number. The digits of 15 are 1 and 5; the sum of these digits is
6 as 1 + 5 = 6 and 15 is not exactly divisible by 6
Write a program that asks the user to enter a number, n, and will then calculate and
display the nth Harshad number.
Example
If the user enters the number 12 then the program should calculate
and display the twelfth Harshad number. The twelfth Harshad
number is 18
You may assume that the number that the user enters will be a positive integer.
Page 2 of 21
Mark schemes
Q1.
(a) 4 marks for AO3 (design) and 8 marks for AO3 (programming)
Mark Scheme
Guidance
Page 3 of 21
Evidence of design to look for in responses:
Note that AO3 (design) points are for selecting appropriate techniques to use
to solve the problem, so should be credited whether the syntax of
programming language statements is correct or not and regardless of whether
the solution works.
DPT. mark points 7 and 8 if only checks for some vowels or includes at most
one non-vowel character.
VB.Net
Page 4 of 21
UserString = NewString
StartPos += 1
EndPos -= 1
Else
StartPos += 1
End If
Else
EndPos -= 1
If Not "aeiou".Contains(UserString(StartPos)) Then
StartPos += 1
End If
End If
End While
Console.WriteLine(UserString)
Console.ReadLine()
Alternative answer
Python 3
Page 5 of 21
StartPos += 1
EndPos -= 1
else:
StartPos += 1
else:
EndPos -= 1
if UserString[StartPos] not in "aeiou":
StartPos += 1
print(UserString)
Alternative answer
Python 2
Alternative answer
Page 6 of 21
StartPos = 0
StartNewString = ""
EndNewString = ""
while EndPos > StartPos:
if UserString[EndPos] in "aeiou":
if UserString[StartPos] in "aeiou":
EndNewString = UserString[StartPos] + EndNewString
StartNewString += UserString[EndPos]
StartPos += 1
EndPos -= 1
else:
StartNewString += UserString[StartPos]
StartPos += 1
else:
EndNewString = UserString[EndPos] + EndNewString
EndPos -= 1
if UserString[StartPos] not in "aeiou":
StartNewString += UserString[StartPos]
StartPos += 1
if StartPos == EndPos:
print StartNewString + UserString[StartPos] + EndNewString
else:
print StartNewString + EndNewString
C#
Page 7 of 21
Alternative answer
Alternative answer
Page 8 of 21
char temp = charArray[startPos];
charArray[startPos] = charArray[endPos];
charArray[endPos] = temp;
endPos--;
}
}
startPos++;
}
userString = new string (charArray);
Console.WriteLine(userString);
Pascal/Delphi
var
UserString, Vowels, NewString : string;
EndPos, StartPos : integer;
begin
Vowels := 'aeiou';
write('Enter a string: ');
readln(UserString);
EndPos := length(UserString);
StartPos := 1;
while EndPos > StartPos do
begin
if pos(UserString[EndPos], Vowels) > 0 then
begin
if pos(UserString[StartPos], Vowels) > 0 then
begin
NewString := copy(UserString, 1, StartPos - 1) +
UserString[EndPos] + copy(UserString, StartPos + 1, EndPos -StartPos
- 1) + UserString[StartPos] + copy(UserString, EndPos + 1,
length(UserString) - EndPos);
UserString := NewString;
inc(StartPos);
dec(EndPos);
end
else
inc(StartPos);
end
else
begin
dec(EndPos);
if pos(UserString[StartPos], Vowels) = 0 then
inc(StartPos);
end;
end;
writeln(UserString);
readln;
end.
Alternative
var
UserString, Vowels, StartNewWord, EndNewWord : string;
Page 9 of 21
EndPos, StartPos : integer;
begin
Vowels := 'aeiou';
write('Enter a string: ');
readln(UserString);
EndPos := length(UserString);
StartPos := 1;
StartNewWord := '';
EndNewWord := '';
while EndPos > StartPos do
begin
if pos(UserString[EndPos], Vowels) > 0 then
begin
if pos(UserString[StartPos], Vowels) > 0 then
begin
EndNewWord := UserString[StartPos] + EndNewWord;
StartNewWord := StartNewWord + UserString[EndPos];
inc(StartPos);
dec(EndPos);
end
else
begin
StartNewWord := StartNewWord + UserString[StartPos];
inc(StartPos);
end;
end
else
begin
EndNewWord := UserString[EndPos] + EndNewWord;
dec(EndPos);
if pos(UserString[StartPos], Vowels) = 0 then
begin
StartNewWord := StartNewWord + UserString[StartPos];
inc(StartPos);
end;
end;
end;
if StartPos = EndPos then
writeln(StartNewWord + UserString[StartPos] + EndNewWord)
else
writeln(StartNewWord + EndNewWord);
readln;
end.
Java
Page 10 of 21
}
else {
startNewWord += input.charAt(startPos);
startPos++;
}
}
else {
endNewWord = input.charAt(endPos) + endNewWord;
endPos--;
if (input.charAt(startPos) == 'a' || input.charAt(startPos) ==
'e' || input.charAt(startPos) == 'i'
|| input.charAt(startPos) == 'o' || input.charAt(startPos) ==
'u') {
startNewWord += input.charAt(startPos);
startPos++;
}
}
}
if (startPos == endPos) {
Console.writeLine(startNewWord + input.charAt(startPos) +
endNewWord);
}
else {
Console.writeLine(startNewWord + endNewWord);
}
12
Screen capture showing the string persepolis being entered and then the
string pirsopeles being displayed and screen capture showing the string
darius being entered and then the string durias being displayed and screen
capture showing the string xerxes being entered and then the string xerxes
being displayed;
I. order of tests
1
[13]
Page 11 of 21
Q2.
(a) 4 marks for AO3 (design) and 8 marks for AO3 (programming)
Mark
Level Description
Range
Guidance
Page 12 of 21
Harshad number is found
3. Identifying that nested iteration is needed
4. Selection structure that compares sum of digits (I. incorrectly
calculated) with a number
VB.Net
Page 13 of 21
NumbersFoundSoFar += 1
If NumbersFoundSoFar = Number Then
Console.WriteLine(CurrentNumber)
End If
End If
CurrentNumber += 1
End While
Alternative answer
Recursive answer
Sub Main()
Console.Write("Enter value for n: ")
Dim n As Integer = Console.ReadLine()
Dim NthHarshad As Integer = 0
Dim Counter As Integer = 1
Dim Value As Integer
While n <> NthHarshad
Value = SumDigits(Counter)
If Counter Mod Value = 0 Then
NthHarshad += 1
End If
If n = NthHarshad Then
Console.WriteLine(Counter)
End If
Counter = Counter + 1
End While
End Sub
Python2
Page 14 of 21
Number = int(raw_input("Enter value for n: "))
NumbersFoundSoFar = 0
CurrentNumber = 1
while NumbersFoundSoFar != Number:
SumOfDigits = 0
NumAsString = str(CurrentNumber)
for count in range (0, len(NumAsString)):
SumOfDigits += int(NumAsString[count])
if CurrentNumber % SumOfDigits == 0:
NumbersFoundSoFar += 1
if NumbersFoundSoFar == Number:
print CurrentNumber
CurrentNumber += 1
Alternative answer
Recursive answer
def SumDigits(Num):
if Num == 0:
Sum = 0
else:
Sum = Num % 10 + SumDigits(Num//10)
return Sum
Python3
Page 15 of 21
if NumbersFoundSoFar == Number:
print(CurrentNumber)
CurrentNumber += 1
Alternative answer
Recursive answer
def SumDigits(Num):
if Num == 0:
Sum = 0
else:
Sum = Num % 10 + SumDigits(Num//10)
return Sum
C#
Page 16 of 21
if ((numbersFoundSoFar == number))
{
Console.WriteLine(currentNumber);
}
}
currentNumber++;
}
Console.ReadLine();
Alternative answer
Recursive answer
Page 17 of 21
if ((counter % value) == 0)
{
nthHarshad++;
}
if ((n == nthHarshad))
{
Console.WriteLine(counter);
}
counter = (counter + 1);
}
Console.ReadLine();
}
PASCAL/Delphi
var
Number : integer;
NumbersFoundSoFar : integer;
CurrentNumber : integer;
SumOfDigits : integer;
NumAsString : string;
Count : integer;
begin
write('Enter value for n: ');
readln(Number);
NumbersFoundSoFar := 0;
CurrentNumber := 1;
while (NumbersFoundSoFar <> Number) do
begin
SumOfDigits := 0;
NumAsString := inttostr(CurrentNumber);
for Count := 1 to length(NumAsString) do
SumOfDigits := SumOfDigits + strtoint(NumAsString[Count]);
if CurrentNumber mod SumOfDigits = 0 then
begin
inc(NumbersFoundSoFar);
if NumbersFoundSoFar = Number then
writeln(CurrentNumber);
end;
inc(CurrentNumber);
end;
readln;
end.
Alternative answer
var
Number : integer;
NumbersFoundSoFar : integer;
CurrentNumber : integer;
SumOfDigits : integer;
Temp : integer;
begin
write('Enter value for n: ');
readln(Number);
NumbersFoundSoFar := 0;
CurrentNumber := 1;
while NumbersFoundSoFar <> Number do
begin
Temp := CurrentNumber;
SumOfDigits := 0;
while Temp > 0 do
begin
Page 18 of 21
SumOfDigits := SumOfDigits + (Temp mod 10);
Temp := Temp div 10;
end;
if CurrentNumber mod SumOfDigits = 0 then
begin
inc(NumbersFoundSoFar);
if NumbersFoundSoFar = Number then
writeln(inttostr(CurrentNumber));
end;
inc(CurrentNumber);
end;
readln;
end.
Recursive answer
var
N, NthHarshad, Counter, Value : integer;
begin
write('Enter value for n: ');
readln(N);
NthHarshad := 0;
Counter := 1;
while (N <> NthHarshad) do
begin
Value := SumDigits(Counter);
if Counter mod Value = 0 then
inc(NthHarshad);
if N = NthHarshad then
writeln(inttostr(Counter));
inc(Counter);
end;
readln;
end.
JAVA
Page 19 of 21
if (numbersFoundSoFar == number) {
Console.writeLine(currentNumber);
}
}
currentNumber++;
}
Alternative answer
Recursive answer
Page 20 of 21
**** SCREEN CAPTURE ****
Must match code from (a), including prompts on screen capture
matching those in code.
Code for (a) must be sensible.
Screen capture showing the number 600 being entered and then a
message displayed saying 3102
1
[13]
Page 21 of 21