QA.TechInterviews.com - your tech questions answered
Help Writing Pseudocode?
This is a class exercise and I did try it, but my competence in programming is shocking . I did a previous exercise where I had to write an algorithm for a game and now I have to turn it into pseudocode using the keywords from the six basic computer operations (eg. display, initialize, dowhile)
Would anyone please help me?
This is the algorithm that I wrote (it is probally quite bad, though it was the best that I could do)

Move fish close shark 10 pixels away. Fish shows alarm in face. Shark tries to attack fish. User tries to move fish away. If fish moves more than 20 pixels away from shark user earns 1 point. User wins when score 10 points. Game ends if shark eats fish.
If fish is close to shark by 10 pixels
then show fish shows alarm in face
else shark tries to attack fish
else if fish is more then 20 pixels away then shark
do until user wins 10 points
increase user points by 1
if shark eats fish then end of game

<?php
// Define all variables at the top;
$fishPositon = 1;
$sharkPostion = 0;
$xLoopPoints = 0;
$alarmFace = "face";
$intEndTries = 10; // End Integer Shark Attack Tries
$intStartTries = 1; // Start Integer Shark Attack Tries
$yLoop = 0;
$randomAttacks = rand() * $intEndTries;

if($fishPosition == (1 - $sharkPosition) ){ // then
exit;
// end of game
}else if($fishPosition > $sharkPosition){
do while($sharkPosition > $fishPosition){
$xLoopPoints++;
print($showAlarmFace);
}
}else{
do while($intStartTries < $intEndTries){
if($sharkPostion != ($fishPostion -1)){
$xLoopPoints += 10; // 10 points
}else{
break; // fish cought
exit; // end of game
}
}
}
?>
If fish is close to shark by 10 pixels
then show fish shows alarm in face
else shark tries to attack fish
else if fish is more then 20 pixels away then shark
do until user wins 10 points
increase user points by 1
if shark eats fish then end of game

<?php
// Define all variables at the top;
$fishPositon = 1;
$sharkPostion = 0;
$xLoopPoints = 0;
$alarmFace = "face";
$intEndTries = 10; // End Integer Shark Attack Tries
$intStartTries = 1; // Start Integer Shark Attack Tries
$yLoop = 0;
$randomAttacks = rand() * $intEndTries;

if($fishPosition == (1 - $sharkPosition) ){ // then
exit;
// end of game
}else if($fishPosition > $sharkPosition){
do while($sharkPosition > $fishPosition){
$xLoopPoints++;
print($showAlarmFace);
}
}else{
do while($intStartTries < $intEndTries){
if($sharkPostion != ($fishPostion -1)){
$xLoopPoints += 10; // 10 points
}else{
break; // fish cought
exit; // end of game
}
}
}
?>
Pseudocode (derived from pseudo and code) is a description of a computer programming algorithm that uses the structural conventions of programming languages, but omits detailed subroutines or language-specific syntax. It can also refer to a high level 'language' whose aim is to generalise the logic and program flow of a computer program

In the context of the Short Code language, pseudocoding refer to the use of codes to represent assembly instructions, even though such codes could not be automatically compiled into an executable program. This usage has mostly fallen out of use.

Flowcharts can be thought of as a graphical form of pseudocode.

Description
As the name suggests, pseudocode generally does not actually use the syntax of any particular language; there is no systematic standard form, although any particular writer will generally borrow the appearance of a particular language. Popular sources include PASCAL, C, Lisp, and ALGOL.

Details not relevant to the algorithm (such as memory management code) are usually omitted, and the programming language will be augmented with natural language where convenient (for example, for trivial operations such as swapping two variables). Depending on the writer, pseudocode may therefore vary widely in style, from a near-exact imitation of a real programming language at one extreme, to a description approaching formatted prose at the other.

[edit]
Applications
Computer science textbooks often use pseudocode in their examples so that all programmers can understand them, even if they do not all know the same programming languages. There is usually an accompanying introduction explaining the particular conventions in use. The level of detail of such languages may in some cases approach that of general-purpose languages—for example, Knuth's seminal textbook The Art of Computer Programming describes algorithms in a fully-specified assembly language for a non-existent microprocessor.

A programmer who needs to implement a specific algorithm, especially an unfamiliar one, will often start with a pseudocode description, and then simply "translate" that description into the target programming language and modify it to interact correctly with the rest of the program.

[edit]
Examples of pseudocode
An example of how pseudocode differs from regular code is below.

Regular code (written in PHP):

<?php
if (is_valid($cc_number)) {
execute_transaction($cc_number, $order);
} else {
show_failure();
}
?>
Pseudocode:

if credit card number is valid
execute transaction based on number and order
else
show a generic failure message
end if
[edit]
Compilation
It is often suggested that future programming languages will be more similar to pseudocode or natural language than to present-day languages; the idea is that increasing computer speeds and advances in compiler technology will permit computers to create programs from descriptions of algorithms, instead of requiring the details to be implemented by a human. Various attempts to bring this about have produced programming languages such as Visual Basic and AppleScript; in practice, however, the similarity to natural language is usually more cosmetic than genuine, and any increased simplicity in use is due to powerful libraries, not to any intelligence on the part of the compiler. Python programming language, quickly becoming the language of choice for many programmers, is one of the successful pseudocode-like languages.[citation needed]

Back to QA. TechInterviews.com. Powered by Yahoo! Answers and TechInterviews.com community.