Amir A.
asked 11/22/16design a pseudo-code that counts the number of its inputs which are posetive,negative, and zero.
design a pseudo-code that counts the number of its inputs which are posetive,negative, and zero.
More
1 Expert Answer
David W. answered 11/22/16
Tutor
4.7
(90)
Experienced Prof
It is good that this problem said, "design," rather than "blindly start coding."
The use of the words, "input" and "count" are significant. In terms of Input, Process, Output, this means:
Input: a number [note that a number is one of either positive, negative, or zero; or an error]
Process: count [initialize three counters; increment the appropriate counter]
Output: counts [the cumulative counters, or the final values, are to be displayed]
Now, you may proceed with assigning variables -- either individual names, or an array, or data base, other data structure (depending on future possible capabilities). Let's use the simple names.
The logic flow is to:
(100) Initialize the three counters
(200) Proceed until terminated
(300) Accept input [and validate it]
(400) Increment the one appropriate counter
(500) Display the three counters.
(600) Continue
[Note: statement numbers are arbitrary; these come from old BASIC conventions; indentation indicates the body of a loop]
Now, PLZ realize that the words used in those six steps already constitute a "pseudo-code" because they are clear, unambiguous, and translatable to actual computer language statements. Also, note that step [500] is designed as a continual display, rather than a display at the end; step [300] waits until a valid number is entered; and "initialize," "proceed," "terminated," "accept," validate," "increment," "display," and "continue" are TBD (that is, "To-Be-Determined") by the specific computer language being used. The use of verbs for process steps is a very important concept in implementing any algorithm.
You may now use what is termed, "progressive elaboration" (that is, expand summary statements into their parts). What does, "Increment the one appropriate counter" mean?
If (value is positive) then increment Positive
If (value is negative) then increment Negative
If (value is zero) then increment Zero
This is an excellent place to be sure that those three variables were properly initialized in step [1]:
Initialize Positive
Initialize Negative
Initialize Zero
[again, note that some languages allow initialization of multiple variables as one statement]
Now, realize that the order of checking may or may not affect the efficiency of the program. Also, realize that only one value should be incremented, so protection should be included to prevent the possible error of incrementing none or more than one variable (note: a CASE statement does this effectively).
While you are designing an algorithm, consider the possible future refinements and the possible future uses of the program. For example, you may someday wish to accept positive, negative, and zero as input and to produce only positive or zero (as numbers not counters) as output (that is, an Absolute Value sieve).
For now, simple pseudo-code will do (notice that the statement numbers are retained):
(100) Comment: Initialize the three counters
(110) Initialize Positive
(120) Initialize Negative
(130) Initialize Zero
(200) Comment: Proceed until terminated
(200) Comment: Proceed until terminated
(210) WHILE (TRUE) LOOP
(300) Comment: Accept input [and validate it]
(300) Comment: Accept input [and validate it]
(310) GET (value) ; [Validate is later refinement]
(400) Comment: Increment the one appropriate counter
(400) Comment: Increment the one appropriate counter
(410) Comment: may become CASE statement someday; order may matter
(420) If ( Value > 0 ) THEN Increment Positive
(430) If ( Value < 0 ) THEN Increment Negative
(440) If ( Value = 0 ) THEN Increment Zero
(500) Comment: Display the three counters.
(500) Comment: Display the three counters.
(510) Display (Positive)
(520) Display (Negative)
(530) Display (Zero)
(600) Continue
(600) Continue
Now, PLZ note how pseudo-code becomes comments as it is elaborated and replaced with more specific programming language statements.
Still looking for help? Get the right answer, fast.
Ask a question for free
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Find an Online Tutor Now
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Kenneth S.
11/22/16