Could a programmer please read this Pseudocode and explain what it does Watch

Truthteller10
Badges: 1
Rep:
?
#1
Report Thread starter 3 years ago
#1
program projectx
String x;
Boolean f;
integer a,z,g;
x = readinput;
g = length of x;
z = g minus one;
a = zero
if g = 1 then
f = 1
else
f = 1;
while f=1 and a less than z
while character at a of x is one of
[whitespace,comma,semicolon, exclamation_mark, question_mark,
or full stop]
a++;
while character at z of x is one of
[whitespace,comma,semicolon, exclamation_mark, question_mark,
or full stop]
z--;
if the toUpperCase of character at a of x is identical to the
toUpperCase of character at z of x then
a++ and z--;
else
f=0;
if f=0 then
print out appropriate message
else
print out other appropriate message
0
reply
JamesN88
Badges: 20
Rep:
?
#2
Report 3 years ago
#2
It looks like it's all linked to a event occurring(button click,box being checked etc)

The Boolean value is just true or false with 1 meaning true and 0 meaning false.

If all those parameters between "while f=1 and a= less than z............and else f=0" are met and the Boolean value is true then the program should print a message

If the Boolean value is false while the event occurs then the program should print a different message, e.g "Conditions not met - try again" or something of that nature.

I don't know what the a++ and the z-- means.
0
reply
sputum
Badges: 9
Rep:
?
#3
Report 3 years ago
#3
it's supposed to be checking that the string has a certain property.
0
reply
unprinted
Badges: 19
Rep:
?
#4
Report 3 years ago
#4
It's looking at a string, character by character - what's it doing to them?
0
reply
unprinted
Badges: 19
Rep:
?
#5
Report 3 years ago
#5
(Original post by JamesN88)
I don't know what the a++ and the z-- means.
Increment a and decrement z, respectively - taken from C-like languages.
1
reply
VannR
Badges: 19
Rep:
?
#6
Report 3 years ago
#6
First thing we were told in programming labs: if you do not indent your code, I will refuse to mark it, or even read it.
0
reply
unprinted
Badges: 19
Rep:
?
#7
Report 3 years ago
#7
I suspect leading spaces have been stripped by a 'too smart for its own good' editor. Let's see...

This para should have one leading space.

This one should have two.
0
reply
EricPiphany
Badges: 15
#8
Report 3 years ago
#8
(Original post by Truthteller10)
if g = 1 then
f = 1
else
f = 1;
You've typed something wrong?
0
reply
EricPiphany
Badges: 15
#9
Report 3 years ago
#9
Checks for white space and punctuation characters in a weird way.
0
reply
JordanL_
Badges: 14
Rep:
?
#10
Report 3 years ago
#10
(Original post by EricPiphany)
Checks for white space and punctuation characters in a weird way.
I thought this at first, but it's not. OP what is this, A level? I thought this was really difficult, although it could've been written much more clearly.

f=1 (because it's always f=1, as assigned some lines above what I've quoted) and a is less than z (unless the user input was one character - in which case z = 0, a = 0. In this case, the loop won't run at all). So we proceed into this loop.

"while character at a of x is one of..." okay, so this loop is meant to go UP through all the characters of the string, starting at 0. If the character is whitespace or punctuation, we add one to a++. Since this is a loop, we won't then move on to the next part ("while character at z of x..."), instead we'll repeat the process. If the next character in the string is whitespace etc we'll add to a++. a++ is the position in the string, so each time we find whitespace etc we move along by 1 and check the next character. Note that this is a while loop, not do while, so if the first character in the string isn't white space or punctuation the statements in the loop won't occur at all, so a will remain 0.

Once we find a character that IS NOT whitespace etc., the loop stops and we move on to the next loop, which counts down from the final character in the string (z is g-1: since arrays start at 0, and g is the length of the string in characters - so the number of indices in the array - g-1 gives us the array index of the final character in the string). This loop counts DOWN from the final character, each time checking whether the character at the current position is whitespace etc. If it's whitespace/punc, z decreases by one so we check the next character down. Otherwise the loop will end and we'll go onto the next bit. Note again that it's possible for the loop not to happen at all, if z (final character in the string) is not whitespace or punctuation.

After this loop is finished, a and z should both give us the position of a letter or number in the string, because we've passed all the whitespace or punctuation. We convert whatever character is at these two positions to uppercase and compare them (note, since we're converting to uppercase, that if char[a] = d and char[z] = D, they'd be considered equal). If they're the same, we add one to a and subtract one from z, and repeat this entire process. So the next "while character at a..." loop will start one character along, and the next "while character at z..." loop will start one character along in the other direction. If a and z don't represent the same character, we make f=0 which breaks the loop.

I'm fairly sure that this program would check for palindromes (words/phrases that are the same forwards and backwards, i.e race car spelled backwards is race car). It does this by ignoring all punctuation and whitespace, and instead checks the first letter/number at the front and the first letter/number at the back. If they're the same, we check the second letter/number from the front and the second from the back. We keep going until a becomes greater than z - at this point we've passed the middle of the word. We stop when this happens, because we know that it's a palindrome, and the program finishes.

This:

if g = 1 then
f = 1
else
f = 1;
Is incorrect, I think. If g=1 then it can't be a palindrome (since it's only 1 char long), so you should then set f = 1. If g isn't equal to 1, we set f = 1 and proceed to check for a palindrome.
2
reply
EricPiphany
Badges: 15
#11
Report 3 years ago
#11
(Original post by JordanL_)
I thought this at first, but it's not. OP what is this, A level? I thought this was really difficult, although it could've been written much more clearly.
Good job.
0
reply
Incongruous
Badges: 12
Rep:
?
#12
Report 3 years ago
#12
It's very hard to read.

Either write in very simple English or in formal logic/mathematical language or in a proper programming language.

If you do a mish-mash, it makes it much harder to read.
1
reply
PortalofKnowledge
Badges: 0
#13
Report 3 years ago
#13
(Original post by Truthteller10)
program projectx
String x;
Boolean f;
integer a,z,g;
x = readinput;
g = length of x;
z = g minus one;
a = zero
if g = 1 then
f = 1
else
f = 1;
while f=1 and a less than z
while character at a of x is one of
[whitespace,comma,semicolon, exclamation_mark, question_mark,
or full stop]
a++;
while character at z of x is one of
[whitespace,comma,semicolon, exclamation_mark, question_mark,
or full stop]
z--;
if the toUpperCase of character at a of x is identical to the
toUpperCase of character at z of x then
a++ and z--;
else
f=0;
if f=0 then
print out appropriate message
else
print out other appropriate message
Use descriptive variable names please! And indent.
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

University open days

  • Bournemouth University
    Midwifery Open Day at Portsmouth Campus Undergraduate
    Wed, 18 Dec '19
  • The University of Law
    Open Day – GDL and LPC - Chester campus Postgraduate
    Sat, 4 Jan '20
  • University of East Anglia
    Mini Open Day Undergraduate
    Mon, 6 Jan '20

Did you vote in the 2019 general election?

Yes (261)
48.24%
No (60)
11.09%
I'm not old enough (220)
40.67%

Watched Threads

View All