• 0

Pseudocode help.


Question

Okay, so I've been doing practice with pseudocode in order to get ready for the exams at school but I'm stuck on one of the practice tasks and am scared that if I can't pass this I wont be able to even do the actual exam itself.... keep in mind I'm very new to pseudocode.

 

 

Okay so the task is to:

 

 

Task


Basic requirements: You are to write the pseudocode for a random quotes program. Quotes stored in an array are to be randomly chosen and displayed in the output window. You must use at least three functions:

  • One that gets a random quote

  • One that displays the quote

  • The draw function from which other, but possibly not all,  functions are called.


Here is a link to a video clip that shows a possible more advanced version of the coded up program, displays inspirational quotes: Possible program


As can be seen a random inspirational quote is displayed every half-second or thereabouts from a range of set quotes. The font is chosen randomly from a range of fonts, and a font size is likewise randomly chosen from a range of sizes. In addition they are in different colours chosen at random from a range of colours, and they all fit within the output window.


Note: It is more likely that you will get a better grade if you choose to go beyond the basic requirements of the program.



 

 

 

This is what I've gotten so far: 

 

 

 

// When the mouse is clicked this program displays a random quote from a list of arrays. The quotes are displayed on the output window// 
 
Input:
     mouse presses
     array of quotes
 
Output:
     quotes displayed randomly on the screen
 
Variables:
        arrays: quotes, array of strings, predictions
        number: quote numbers
        color: textColor
        Boolean: mouseIsPressed
 
Functions:
     // This function displays a quote at centreX and centreY//
     FUNCTION drawQuote (Parameters: centerX, centerY, quote)
     set textColor (random color)
     set textSize (5)
     draw the quote at centerX/centerY with textSize
     
     END FUNCTION
     
     // This function gets a quote from the quotes array//
     FUNCTION getQuote (Parameters: none)
     set textColor (random color)
     set textSize (5)
     draw the quote at centerX/centerY with textSize
     
     END FUNCTION
     
     
     Functions: 
     
FUNCTION getPrediction(Parameters: none)
     
      Set index to a random number between 0 and the length of the predictions array minus 1
 
 
Set quote to the element in the predictions array at index position
 
RETURN prediction
 
END FUNCTION
 
     FUNCTION drawText(parameters: prediction)
     
     Set fill color to white
     Set text size to 5 
     Set text align to center
     Use text function to draw
     Prediction
 
END FUNCTION
 
 
// This function draws the background//
 
FUNCTION drawBackground(Parameters: none)
 
Draw black background
 
Set fill colour to black
 
END FUNCTION
 
 
FUNCTION mousePressed(Parameters: none)
 
Set prediction to CALL getPrediction()
 
CALL drawQuote(prediction)
 
END FUNCTION
 
 
 
Processing:
 
Start when mouse is pressed
 
 
 
Test items:
 
When mouse clicked a random quote is displayed in the output window.
 
Over time different ones are displayed.
 
When any key pressed nothing happens.
 
 
 
 
 
 
 
 
Pretty much I need to polish it up. Please help guys!
Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

You seem to be approaching it as if it were some sort of specification (design) document with the way you're listing things like input/output/variables/etc under headings. Is this what you've been told to do or are you just guessing? From my point of view, the task seems to just be expecting code. Make sure to check with your teacher exactly what they and the exam board are expecting for a question like this, if you don't already know. Time is precious in an exam, and you don't want to be wasting any on stuff that they're not asking for and which is not going to get you any marks.

 

To start with, get your code back to the very basics of what they're asking for here. Here's an example of what they might be after:

1) A function that gets a random quote:

 

FUNCTION GetRandomQuote()

    Get random number between zero and length of quote array minus one
    Use the random number as an array index to retrieve the selected quote
    Return the selected quote

END FUNCTION
 

2) A function that displays the selected quote:

Based on your post above, I assume that we're pretending that we have a gui based application, with the quote being displayed in some textbox or label control. So,

 

FUNCTION DisplayQuote(quote)

    Replace text displayed in label control used for displaying quote with text of new quote that was passed as a parameter

END FUNCTION
 

3) A "draw function from which other, but possibly not all,  functions are called"

 

FUNCTION DrawNewRandomQuote()

    Call GetRandomQuote() function to get new random quote
    Call DisplayQuote(quote) function, passing it a copy of the new quote, to get new quote displayed

END FUNCTION
Pseudo code is entirely flexible about the level of detail you go into, so check with your teacher as to whether this is what they're expecting of you if you're going to get top marks, or if not, exactly what to do differently.

 

Now obviously we have no code actually causing the quote to be changed upon a given event. I think some sort of trigger mechanism is definitely called for based on what it says in the task - the description of an example more advanced program and the offer of more marks for going beyond the basics. So, what will be your trigger mechanism be? Having it change on a timer, on every mouse click (like you already have), or upon clicking a certain button are some examples.

 

On every mouse click:

FUNCTION MouseClickHandler()

    Call DrawNewRandomQuote() function

END FUNCTION
But this function isn't going to magically get run when the user clicks the mouse, just because we've named it "MouseClickHandler". We need to set it up to be run on such an event. Perhaps you only need to explain this with a simple note alongside your code, or perhaps you need something like the following:

 

// Program entry point
FUNCTION Main()

    Register MouseClickHandler() function as an event handler for mouse clicks

END FUNCTION
 

Again, ask your teacher.

This is easily adaptable to handle clicking a specific button or whatever in the program's interface instead.

If you're not familiar with the idea of a main() function, replace with a form-load handler or whatever. If you're confused about what I'm on about here, ask and we'll try to explain.

 

For a timed event:

You could try an infinite loop that only ends when a flag (boolean) is set to a particular value, with an application close event handler that changes that flag, allowing the program to end. But, there are issues here. You might be locking the program up preventing it from doing anything outside of the loop, even handling the user clicking on the close button, depending on how you write the code. I'm trying to imagine that you're learning to program with something like vb.net, and I'm trying to keep things as simple as possible, so perhaps you can use the vb.net concept of a timer control here, rather than a loop and the sleep function, and thus avoid all of these issue (only because the vb.net timer control and event handler mechanisms abstract and hide such stuff from you).

 

FUNCTION Main()

    Create/initialise 10 second timer control
    Register TimerEventHandler() function as an event handler for this timer control

END FUNCTION

FUNCTION TimerEventHandler()

    Call DrawNewRandomQuote() function

END FUNCTION
 

Again, replace main() with form_load() or whatever as necessary.

 

If you want to extend the program to add random font/background colour changes for the quote - beware that certain colour combinations are going to be no good, e.g. black font with black background, so it may be wise to come up with a small set (array) of font/background colour combinations. Then you just need code to pick a random colour combination from this two-dimensional array and set the text and background colours for the label/textbox. I'll leave that for you to come up with the pseudo code for.

  • Like 2
Link to comment
Share on other sites

  • 0

You could improve it by loading the quotes from a user editable file, or possibly from the web. That way, the content is much more dynamic. Say a quote per line in a file. So start with a function to load the dynamic quotes:

 

result
function load_quotes ( source [FILE/URL], quotes [ARRAY OF STRINGS], quote_count )
{
     if ( NULL == ( f = file_open ( source, readonly ) )
         return failure;

     c = count_lines ( f );
     p = a = allocate_array ( c, string );

     while ( EOF != ( line = file_read_line ( f ) ) )
         *p++ = line;           

     file_close ( f );

     quotes      = a;
     quote_count = c;

     result = success;
} 
There you have a dynamic set of quotes which can be edited at any time without the need to recompile the program.

As to the rest of the program, it should be pretty straight forward. The entry point function should contain a single loop which intermittently renders a new quote.

It's probably worth describing an input parameter for the program whereby the user can optionally supply an interval for the next quote to appear.

 

function main ( program_args )
{
   interval = get_arg ( quote_interval );

   if ( failure == load_quotes ( "./quotes", quotes, quote_count ) )
      return EXIT_FAILURE;

   while ( true ) {

      i = get_random_index ( quote_count );
      render_quote ( quotes[i] );

      if ( false == sleep_for ( interval ) )
         return EXIT_SUCCESS;
   }
}
The sleep_for function should employ a loop with a small wait value supplied to the OS provided sleep() function. Perhaps 100 / 200 milliseconds. Then loop for ( interval * 1000 / yield ) times:

const yield = 200

bool
function sleep_for ( interval ) {

   for ( i = 0; i < ( interval * 1000 / yield ); i++ ) {
      if ( quit_keypress () )
          return false;

      sleep ( yield );
   }

   return true;
}
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.