Pseudocode
Most programs are developed using programming languages. These languages have specific
syntax that must be used so that the program will run properly. Pseudocode is not a programming language, it is a simple way of describing a set of instructions that does not have to use specific syntax.
Best Practices For Writing Pseudocode
Pseudocode can make the coding process smoother and more efficient. Here are some tips for writing effective pseudocode:
- Define the problem clearly. Before writing pseudocode, thoroughly understand the problem. Outline the desired outcomes, inputs and necessary steps to reach the outcome.
- Keep it simple. Use straightforward and concise language that accurately depicts the steps of the algorithm. Avoid using complex terms or jargon that might confuse others.
- Use consistent language. To maintain clarity, use a consistent style throughout the pseudocode. This helps others better understand the thought process behind the approach.
- Write in a logical order. Each step should follow a logical sequence. Translating pseudocode into a programming language is easier that way.
- Review and refine. Upon completion, ensure the pseudocode is clear and logical. Look for areas that could use simplification or clarification.
Pseudocode doesn't have to be perfect. Its main purpose is to help conceptualize the algorithm and facilitate communication with others.
Most Common Pseudocode Notation
There is no strict set of standard notations for pseudocode, but some of the most widely recognised are:
- Begin with START or BEGIN and end with END to define algorithm boundaries. Use indentation to show operation hierarchy, similar to actual programming.
- INPUT – indicates a user will be inputting something.
- COMPUTE – indicates that a compute process of something.
- STORE or SET or ASSIGN – indicates a user store the the data from input or compute something.
- OUTPUT or SHOW or DISPLAY – indicates that an output will appear on the screen.
- WHILE – a loop ( iteration ) that has a condition at the beginning.
- FOR – a counting loop ( iteration ).
- REPEAT – UNTIL – a loop (iteration) that has a condition at the end.
- IF – THEN – ELSE – a decision ( selection ) in which a choice is made.
- CALL – Invokes a function.
- RETURN – Sends back a value from a function.
Function Pseudocode
FUNCTION <function-name> ( <argument or parameter name> )
<function body>
RETURN value
ENDFUNCTION
Logic If Pseudocode
IF <condition>
<body statement>
ELSEIF <condition>
<body statement>
ELSE
<body statement>
ENDIF
Repetition While Pseudocode
WHILE <condition>
<body statement>
ENDWHILE
Example Bubble Sort Program In Pseudocode
//Bubble sort program using function
START
DECLARE void bubble_sort
PROCEDURE int main
/* Declare variable for number container, number of elements, and looping index number */
DECLARE long array1[100], n, c
SHOW "Enter number of elements NEW LINE"
READ number of elements, SET TO variable n
SHOW "Enter <number elements> of integers", READ from n
// Loop each number input
FOR c = 0 TO c < n
READ number, SET to variable array1
ENDFOR
CALL bubble_sort(array1, n)
SHOW "Sorted list in ascending order:NEW LINE"
// Loop each number in variable array1
FOR c = 0 TO c < n
SHOW each number FROM array1 in each line
ENDFOR
RETURN 0
ENDPROCEDURE
PROCEDURE void bubble_sort(array list, n)
// Declare variable c, d, t AS long
DECLARE long c, d, t
// Loop through array of unsorted element
FOR c = 0 TO c < n -1
FOR d = 0 TO d < n - c - 1
IF list[d] > list[d+1]
// swapping
t = list[d];
list[d] = list[d+1];
list[d+1] = t;
ENDIF
ENDFOR
ENDFOR
ENDPROCEDURE
END
Example Bubble Sort Program In C Programming Language
//Bubble sort program in C language using function
// Compile using TDM GCC in Microsoft Windows
#include <stdio.h>
void bubble_sort(long [], long);
int main()
{
long array1[100], n, c;
printf("Enter number of elements\n");
scanf("%ld", &n);
printf("Enter %ld integers\n", n);
for (c = 0; c < n; c++) {
scanf("%ld", &array1[c]);
}
bubble_sort(array1, n);
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++) {
printf("%ld\n", array1[c]);
}
getchar();
system("pause");
return 0;
}
void bubble_sort(long list[], long n)
{
long c, d, t;
for (c = 0 ; c < n - 1; c++) {
for (d = 0 ; d < n - c - 1; d++) {
if (list[d] > list[d+1]) {
/* Swapping */
t = list[d];
list[d] = list[d+1];
list[d+1] = t;
}
}
}
}
< back to Study About C and C++ 1
Bibliography
https://www.bbc.co.uk/
https://www.calpoly.edu/ ( university )
https://www.codecademy.com/
https://www.niu.edu/ ( university )
https://www.techtarget.com/
Tidak ada komentar:
Posting Komentar