The Student Room Group

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

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
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.
(edited 7 years ago)
Reply 2
it's supposed to be checking that the string has a certain property.
It's looking at a string, character by character - what's it doing to them?
(edited 7 years ago)
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.
Reply 5
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.
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.
Original post by Truthteller10

if g = 1 then
f = 1
else
f = 1;


You've typed something wrong?
Checks for white space and punctuation characters in a weird way.
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.
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.
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.
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.

Quick Reply

Latest

Trending

Trending