I've been given a task in which I need to write some pseudocode for a program that needs to be able to complete a given set of tasks, and then write it in a given language.
Problem is, I have no idea how on earth I should go about writing this magical "pseudocode".
Any advice? tips and tricks?
Should I make it colourful? In comic sans?
Could you give me some extracts of your psuedocode as... inspiration?!
I'm stuck.
Should I just copy and paste the actual code and remove everything but the skeleton of it and add a bit more commentary?!
Run through line by line and describe what it's doing? Because I already do that as comments anyway.
I was a bit confused with this in the beginning too, so here's what made it easier for me to grasp. Pseudocode isn't code, its like code and be as close or as far away from as code as you want. So this is the planning stage, so its purpose is to jot down the structure of your code close to what you'd be wanting to actually write but do it in a mix of English and some code thrown in
So lets say you wanted to write the following code, bear in mind you haven't gotten to this point yet so its just to show you what the pseudocode would look like for this
// ClearInputBuffer function definition
void ClearInputBuffer(istream &in)
{
char characterFromBuffer; // a char variable to hold input from the buffer
// if the in object has failed...
if(in.fail())
{
in.clear(); // clear the fail state of the object
characterFromBuffer = in.get(); // attempt to read a character
// while the character read is not new-line or not end-of-file
while (characterFromBuffer != '\n' && characterFromBuffer != EOF)
{
// therefore something was read from the buffer
// attempt to read another character
characterFromBuffer = in.get();
} // end of while
}// end of if
} // end of ClearInputBuffer
So having seen the code, now read the proposed pseudocode. Do note that this pseudocode would be what I may do in the very beginning stages of planning, and would modify this adding more code elements as I got ideas for what to do for each section while not being code yet. So it'd be english like with code elements, I'd then take that and code it once that planning stage is done
You could do the same or similar process, even starting out more code like and skipping that later editing if you feel.
Create a function called ClearInputBuffer
{
declare a variable, make it a char, call it characterFromBuffer
if in object is in failed state
{
clear the failed state
attempt to read the character and store it in variable ClearInputBuffer
while ClearInputBuffer doesn't equal new line and ClearInputBuffer doesn't equal end of file
{
attempt to read another character, store it in ClearInputBuffer
}
}
}
3 Likes
Unless your teacher has given you a formal pseudocode language to use... pseudocode just is like an outline for your program. It's not actual code. You just have to describe the procedure, without focusing on syntax.
Well thats one way but can it limit the degree to which it may be able to be translated to other languages. Can be very easy to spot though.
Use statements that are basic enough that anyone with basic english, maths or comp sci knowledge can understand. Best way to test is to hand it to a non-programmer to read over. If they understand what the code plan is then 'great success', if not rewrite it.
1 Like
Like this:
public void UsePseudoCode()
{
bool pseudoCode = true;
while(pseudoCode = true)
{
string doThis = "Continue pseudocode";
}
}
//food for thought ! :)
You could do, but that would defeat the purpose of the exercise. The idea of pseudocode is to plan out the logic and structure before you write the actual code. It's not supposed to be specific to any particular language so that when you do write the code you can do it in whatever language you need to.
Think of it as what a storyboard is to a film.
2 Likes