Section 2: Problem-solving and program design Flashcards

1
Q

The steps in problem solving

A
  1. Definition of the problem
  2. Propose and evaluate solutions
  3. Determination of the most efficient solution
  4. Develop and represent algorithm
  5. Test and validate the solution
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Definition of the problem

A

Defining the problem is the first step towards solving a problem. It is the most important step since it leads to a clearer understanding of what is given and what is required. If the programmer does not fully understand what is required, then he/she cannot produce the desired solution. This step involves decomposing the problem into three key components:

  1. Inputs: what is given (source data)
  2. Outputs: the expected results
  3. Processing: the tasks/actions that must be performed

To do this, a defining diagram is used. A defining diagram is a table with three columns labeled to represent the components.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Inputs

A

Inputs can be identified by the keywords that precede them. These are: GIVEN, ENTER, READ, or ACCEPT.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Outputs

A

Outputs can be identified by the keywords: PRINT, DISPLAY, FIND, PRODUCE, or OUTPUT.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Processing

A

Processing can be determined by asking;
“What do I have to do with the inputs in order to produce the desired output?” The actions/tasks determined must be listed in a logical sequential order. Eg. Read two numbers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Proposing and Evaluating Solutions

A

The second step in solving a problem is ‘Proposing and Evaluating Solutions’. After defining the problem, you would know what needs to be done. In this step, you figure out how to do it, bearing in mind that a problem can have many different solutions. Initially, go through each step of the solution manually (by hand) using sample input data to see if the solution provides the desired outcome. Then review it to see how you can make it more efficient. After completing the manual solution to the problem the next step is to write the solution as a sequence of instructions.

Example:
Start
Read first number, call it num1
Read second number, call it num2
Multiply num1 by num2
Print product
Stop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Variables

A

In processing data, values that are manipulated need to be stored in a memory location. Because of the large number of storage location in memory, we need to have an identifier to label each location. Depending on if the value changes during the execution of the instructions, it is called a variable. If the value does not change it is called a constant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Choosing Identifier Names

A

Choose names that reflect the kind of data that is being stored. It helps any reader to understand the solution better, if the identifier reflects what they store. For example, the variable name product indicates that the value stored in that memory location is a product. If instead, X was used, this does not convey the contents of that memory location to the reader of the solution, and it will make debugging and program maintenance more difficult. Most programming languages have strict rules regarding identifier names, for example, in Pascal, they must begin with a letter or underscore; they can be a combination of letters and digits or underscore and the length cannot exceed 31 characters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Data Types

A
  1. Integer: Positive and negative whole numbers including zero
  2. Real: All numbers including fractions
  3. Character: Any key on the keyboard
  4. String: Characters put together
  5. Boolean: True or False
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Evaluating solutions

A

There are many ways to solve some problems. Usually, the initial solution may not be the most efficient solution. As such, in solving any problem you must explore alternative solutions to come up with the best (most efficient) solution.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Initializing variables

A

This means giving the variable an initial or starting value. Sometimes it is necessary to give variables a starting value. This ensures that any data previously stored in that memory location, from a previous operation, is erased and not used mistakenly in a new operation.

Initialization is also necessary whenever a value needs to be incremented (added to by a value, usually one). For example in adding the numbers entered in the solution above. The statement “Add number to sum” is translated to the computer as “sum = sum + num” therefore if the variable sum has a value stored from a previous operation, the resultant value of sum would be incorrect.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Determining the Most Efficient Solution

A

After evaluating and refining the solution, the next step is to choose the most efficient solution. Use the following attributes to determine the most efficient solution:

  • It should be maintainable (i.e. easy to read and upgrade, if necessary).
  • Should use memory efficiently
  • Should be robust (be able to check for invalid input data)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Algorithm

A

An algorithm is a sequence of precise instructions which, if followed, produces a solution to a given problem in a finite amount of time.

Algorithms can be represented using pseudocode or a flowchart. It cannot be executed by the computer. Pseudocode uses English–like statements that models or resembles a programming language. A flowchart uses geometrical objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Algorithmic Structure

A

Terminator: Start of statement(s)
Declaration: Initialization of variables, if necessary
Body: Sequence of steps
Terminator: End of statement(s)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Control Structures

A

In programming, control structures are used to represent logic. There are different types of control structures: sequential, selection and loop (repetition). Just like a program, the body of an algorithm is made up of various control structures.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Input statements

A

These accept data entered into the computer and store the value in the location with the given variable name

17
Q

Output statements

A

These display/output data that is in the computer’s memory

18
Q

Assignment statements

A

Gives the value on the right of the assignment operator (equal sign) to the variable on the left of the assignment operator

19
Q

Calculation statements

A

Uses the values on the right of the assignment operator and performs mathematical operations, then assigns the result of the calculation to the variable on the left of assignment operator

20
Q

Prompting statements

A

These are used with input statements to request or notify the user to enter data into the computer. These statements are displayed on the screen. Prompting statements precede input instructions.

21
Q

Selection structures

A

Selection structures include the IF-THEN or IF-THEN-ELSE statements: They perform comparisons between values and make a decision based on the result of the comparison

22
Q

Loop structures

A

Loop structures include the FOR loop, WHILE loop or REPEAT loop: They allow statements to be repeated a fixed number of times or until a condition becomes TRUE or FALSE