Showing posts with label PseudoCode. Show all posts
Showing posts with label PseudoCode. Show all posts

Saturday, January 21, 2012

Using the PRINT statement

---------------------------------------------------------------------------

If I say:

X = 5;
PRINT X;


Then the value printed out is

> 5

---------------------------------------------------------------------------

If I say:

X = 5;
PRINT "X";


Then the value printed out is

> X

---------------------------------------------------------------------------

If I say:

X = 5;
PRINT X "is the Biggest";


Then the value printed out is

> 5 is the Biggest

---------------------------------------------------------------------------

If I say:

X = 5;
PRINT "X is the Biggest";


Then the value printed out is

> X is the Biggest

---------------------------------------------------------------------------

Assigning a value to a Variable

---------------------------------------------------------------------------

If I want to assign a vaule to a variable, I do the following

X = 5;

we say this as "X gets the value 5".

---------------------------------------------------------------------------

If I want to assign the value of X to a new variable Biggest, I do the following

Biggest = X;

we say this as "Biggest gets the value of X".

---------------------------------------------------------------------------

If I want to add two numbers and print out the result, I do the following

X = 5;
Y = 4;
SumOfNums = X + Y;

The following is incorrect:

X + Y = SumOfNums;

---------------------------------------------------------------------------

Sunday, January 15, 2012

BubbleSort Pseudocode

---------------------------------------------------------------------------

PROGRAM BubbleSort:

/*===================
DECLARING VARIABLES
===================*/

int ARRAY[N];
int innerloop = 0;
int outerloop = 0;

/*-------------------*/

WHILE (outerloop < N)

    DO WHILE (innerloop < N)

        DO  IF (A[innerloop] > A[innerloop + 1])

                THEN Swap (A[innerloop], A[innerloop + 1]);

            END IF;

            innerloop++;

        END WHILE;

        innerloop = 0;

        outerloop++;

END WHILE;

/*-------------------*/

END.


---------------------------------------------------------------------------
---------------------------------------------------------------------------


PROGRAM Swap (int First, int Second):

/*===================
DECLARING VARIABLES
===================*/

int Temp;

/*-------------------*/
Temp = First;
First = Second;
Second = Temp;

END.

---------------------------------------------------------------------------

Friday, October 28, 2011

Sample Pseudocode

Hi all,

just to let you know, in terms of Pseudocode, here's an example:

---------------------------------------------------------------------------

PROGRAM ExampleProgram:

/*===================
DECLARING VARIABLES
===================*/

int X;
char Y;

/*-------------------*/

int ARRAY[N];
char ARRAY[N];

/*-------------------*/

int ARRAY[N,M];
char ARRAY[N,M];


/*===================
SELECTION
===================*/

IF (condition)
    THEN statements;
    ELSE statements;
ENDIF;

/*-------------------*/

CASE (Expression)
    Condition1: statements;
    Condition2: statements;
    Condition3: statements;
    Condition4: statements;
            :
            :
    ConditionN: statements;
ENDCASE;


/*===================
ITERATION
===================*/

WHILE (condition)
    DO statements;
ENDWHILE;

/*-------------------*/

FOR (X = 1 TO 10)
    DO statements;
ENDFOR;

/*-------------------*/

END.

---------------------------------------------------------------------------

Any of these are perfectly fine, and really any other command you like - I can't really just list all the commands that are permitted, otherwise I'd be kinda giving you the answer.

And don't forget to use CamelNotation (aka CamelCase):

Example flowchart for FOR loop

Example flowchart for FOR loop:


This is the same as:


FOR (A = 1 TO 5)
    DO statements;
ENDFOR;