Emailing CSC102-1
Emailing CSC102-1
Basically, we would be using computers to resolve problems, it is significant to highlight the computer
data processing model. The model displayed in figure 1.0 assumes a single CPU.
A typical single CPU computer process data as displayed in fig. 1.0. Problems are solved
utilizing some type of user input such as keyboard, mouse or game control actions then
processing the input and generating some type of output examples images, test or sound.
However, there are times the incoming and outgoing information may be in the form of hard
drive or network devices. In regards to the problem solving, the above model would be applied
by assuming we are given some type of input data that need to be worked with in order to
generate some desire output solution. Though the aforementioned model is quite basic. For
bigger and more intricate problems, we need to restate the input /process/output phases several
times in sequence, generating intermediate results along the path that solve part of the problem
but not essentially the entire problem. In simple computations, the aforementioned model is
appropriate.
Let”s consider a simple example of how the input/process/output works on a basic problem.
1. Input: get all the scores…by typing them in the keyboard or copying them/reading
from a USB or hard drive.
3. Output: output the result to either the monitor, to the print, to the USB drive or
combination of any these devices.
As shown above, the problem is easily solved by basically getting the input, computing
something and generating the output. We are going to study the six steps to problems solving
within the context of the above example.
You need to ensure you understand the problem that you are attempting to solve. Listed below are the
things you need to know:
In the given example we know that input is a bunch of scores but we need to recognize the format of
the grades if the grade is in number from 0 to 100 or is in letter score from A+ to F. If it is in number the
grade maybe a whole integer such as 73 or it might be a real number such as 73.42.
Also, we need to understand what the output should look like. Should the output be a whole or real
number or a letter grade? We might need to show a pie chart with the average grade, all these depends
on our choice.
Step 2: Formulate a model
In this step we need to understand the processing section of the problem. Numerous problems break
down into smaller problems that need a type of elementary mathematical calculations to ensure the
information is processed. From the example, we are expected to calculate the average of the scores. To
proceed we have to know the model or formula for calculating the average of a set of numbers and if
there is no existing model, we need create one. In some situation, we may look up certain formulas in a
book or online if we are not too sure.
If the input information is a set of integers or real numbers x1, x2,……, xn denoting a grade percentage
then the following mathematical model can be applied:
(1)
On the other hand, this method will not work if the input information is a set of letter scores such as A+,
B-, B+, D, F, because the formula (addition and division) cannot be used on the letters. Hence, this step
must find a way to generate an average from this letter. To go further thinking, thinking is essential.
We can now decide to assign an integer number to the incoming letters as listed table 1.0:
A+ =12 B+ = 9 C+ = 6 D+ = 3 F=0
A = 11 B= 8 C=5 D=2
A- = 10 B- = 7 C -= 4 D- =1
Let these newly assigned grade numbers are y1, y2, y 3 ………………., yn ,then we can utilize the following
mathematical model:
(2)
When we want the output as a percentage, then we can use either Average1 directly or utilize
(Aaverage2/12), depending on the input we had initially. If we required a letter grade as result, then we
will have to use (Average1/100*12) or (Average 1*0.12) or Average 2 and then map that to some type of
table that permits us to see a grade letter according to a number from 0 to 12.
Step 3. Develop an Algorithm
Having a clear understanding of the problem and have formulated a model. Next to come up with a
precise plan, what we want the computer to perform.
To design an algorithm, we need to show the instructions in some way that is comprehensible to a
person who is trying to figure out the steps involved. Two frequently used illustrations for an algorithm
is by using (1) pseudo code, or (2) flow charts. Consider the following example of solving the problem
of a broken lighting bulb. To the right is an illustration of a flow chart, while to the left is an illustration
of pseudocode for solving the same problem as shown in figure 1.1.
Pseudocode is an informal method of programming description that does not involve any
stringent programming language syntax or underlying technology considerations. At the early
stage of learning to program, it is essential to write pseudocode because it helps to clearly know
the problem that you are trying to solve. It also assist to avoid getting bogged down with syntax
details such as spelling errors, when writing a program. However, there are some common
control structures (i.e.. features) that appear whenever we write pseudocode, which includes:
Sequence, condition, storage and jumping, which would be discussed in later chapter.
Considering our earlier example of finding the average of a set of n grades stored in a folder. What
would the pseuducode look like? Here is an example of what it may look like if we had the example of n
numeric grades x1 + x2+ xn that were loaded from a folder.
Algorithm
It would be sensible to run through the above algorithm with a real set of numbers. Each time we test
an algorithm with a fixed set of input data, this is known as a test case.
Most of the hard work has been done because precise set of steps for solving the problem have been
achieved. We have to convert the algorithm from step 3 into instructions that can be understood by the
computer.
Writing code or implementing an algorithm is also known as writing a program. So the code or source
code is really the program itself.
Find below a program that implements our algorithm for finding the average of a set of grades.
Observed that the code looks quite alike in structure still, the processing code is less readable and looks
somewhat more mathematical:
We shall not go further to deliberate the details of how to generate the above source code. The source
code varies depending on the programming language such as Java or VB.NET that was used. The
computer needs precise instructions in order to know what you are asking it to do.
Example, if one of the semi-colon characters (;) is removed from the program above, the computer
would be confused as to what you are doing because the (;) is what it knows to be end of an instruction.
Leaving one of them off would cause the program to produce what is known as compile error.
Compiling is the process of converting a program into instructions that can be understood by the
computer.
At this stage you need to make sure the program written that compiles, it can solve the problem that it
was intended to solve and, that the solutions are correct.
Running a program is the process of telling the computer to evaluate the compiled instructions. When
you run your program and if all is well you should see the right output but if there is a problem in your
program, it is known as bugs. Bugs are problems/errors with a program that cause it to stop working or
generate incorrect or unwanted outputs.
You should fix as many bugs in your program as you can find. To find bugs effectively, you should test
your program with many test cases known as test suite. It is also a good idea to have others test your
program because they may think up situations or input data that you may never thought of. The act of
finding and fixing errors in your code is known as debugging and it is usually a time consuming. If you
take your time to carefully follow problem solving steps 1 through 3, this should significantly reduce the
amount of bugs in your programs and it should make debugging much easier.
When your program generates a result that appears accurate, you need to re-deliberate on the initial
problem to confirm the answer is formatted into appropriate solution to the problem. It is usually the
situation that you recognize your program solution did not solve the problem the manner you wanted it
to do. You could recognize that more steps are required.
Just for instance, if the outcome of your program is a long list of numbers but your plan was to define a
pattern in the number or maybe to detect some element from the information, hence, basic generating
a list of numbers might not suffice. Perhaps, demonstrating the data in a manner that support you to
visualize or interpret the results with respect to the problem may be needed such as a chart or graph.
Now that you have seen the six steps you should follow in order to solve problems using computers. It is
important you should try to use this method for your entire problem solving throughout this course.
1.1 Algorithm
Since algorithm is a set of sequential steps often written in ordinary language to solve a given problem.
It can be likely to solve a problem in more than one ways resulting in more than one algorithm. The
option of numerous algorithms depends on the factors such as dependability, accuracy and easy to
modify. The key factor in the option of algorithm is the time needed to execute it, after writing code in
programing language with the support of a computer. The algorithm which would require the least time
when executed is considered the best.
Step 1. Identification of input: there are quantities to be provided for an algorithm known as input and
these are fed externally. The input is to be known first for any stated problem.
Step 3. The processing operation Identification: All computations to be undertaken in order to lead to
output from the input are to be known in organized manner.
Step 4. Processing definiteness: The instructions composing the algorithm must be clear and there
should not be any ambiguity in them.
Step 5. Processing finiteness: When we go through the algorithm, for cases, the algorithm should
terminate after a finite number of steps.
Step 6. Possessing Efficiency: The instructions in the algorithm must be adequately simple and in
practice, it can be executed easily.
Step 6: Stop.
Structural chart: it is a hierarchical demonstration of the top down design. The chart displays
the fundamental program modules and how they are connected.
Abstraction: is the process of reducing or factoring out details that are not essential in order to
describe an algorithm.
Desk check: represent working through the algorithm with a writing pad such as pen and paper.
By drawing each memory location and check the algorithm as if you are the computer.
Documentation: Is any form of data kept about a program for example remarks in a program,
information tables that explain variables and external documents such as user’s manual.
Divide and conquer: is the approach of breaking down the problem into smaller pieces
1.4 Flowchart
Flowchart as earlier said is used to solve a given problem and help a great deal to analyze the
problem and plan its solution in a systematic and orderly manner. A flowchart when translated in to
a proper computer language, results in a complete program.
Algorithm Flowchart
A method of representing the step- Flowchart is diagrammatic
by-step logical procedure for solving representation of an algorithm. It is
problem constructed using different types of
boxes and symbols
It contains step-by-step English The flowchart employs a series of
descriptions, each step representing a blocks and arrows, each of which
particular operation leading to represents a particular step in an
solution of problem algorithm
These are particularly useful for small These are useful for detailed
problems representations of complicated
programs
For complex programs, algorithms For complex programs, Flowcharts
prove to be Inadequate prove to be adequate
It can be demanding to draw a flowchart neatly, mostly when errors are made.
Pseudocode fits more easily on a page of paper
Pseudocode can be written in a manner that is very close to real program code making it simple
to write the program later.
Pseudocode takes less time to write than drawing a flowchart
Examples
ABC company plans to give a 8% year-end bonus to each of its employees earning ₦8,000 or more per
month, and a fixed ₦250/- - bonus to the remaining employees. Draw a flowchart for calculating the
bonus for an employee.
Start
Input salary of an
employee
Is salary
≤8000
O/p bonus
Stop
2.0 Unified Modeling Language Background
Unified Modeling Language (UML) is a standardized general purpose modeling language in the area of
object oriented software. UML includes a set of graphic notation methods to generate visual models of
object oriented software systems. It also combines methods from data modeling, object modeling and
component modeling. It could be utilized throughout the software development life-span and across
different implementation technologies.
Modeling, there is a different between UML and the set of diagrams of a system. A diagram is a partial
graphic representation of a system’s model. The model also contains documentation that drives the
model elements and diagrams such written use cases.
A. Visualizing
B. Specifying: The UML builds models that are precise, unambiguous and complete.
Structural diagram: This diagram highlights the static structure of the system using objects, attributes,
operation and relationships such as class diagram, composite structure diagram.
Behavioral diagram: This diagram highlights the dynamic behavior of the system by displaying
collaborations among objects and changes to the internal states of objects such as sequential diagram,
activity diagram and state machine diagram.
UML 2.2 has fourteen different (14) sorts of diagrams divided into several types as illustrated in figure
below.
Figure 2.0 UML version 2.2 diagram
From figure 2.0 you can see how the diagram is divided into two broad diagrams however, we are going
to discuss below class diagram, use case diagram and sequence diagram:
A. Class diagrams: shows the static structure of the model by displaying the system classes. Their
characteristics and the relationship among the classes.
C Sequence diagram: used to display how objects interact in terms of sequence of messages
Figure 2.3 Sequence diagram
The selection concept defines a condition for deciding between two or more alternative logical paths of
execution in a program. Several real life problems cannot actually be solved with only sequential
structure. Example of this problem is found in example 3.0 and 3.1 which requires more than a
sequence of steps that always execute because the steps to be followed is determined by the input
received.
The repetition structure determines how many times a block of codes is repeated based on a logical
condition as shown in example 3.2. In an idea world, problems that need repeating a section of the
solution a number of times abound such as a payroll program that must repeat the same operations for
each employee whose pay is to be calculated. The program segment under the control of the loop is
known as the body of the loop. A key element of every loop is a test to decide whether to repeat the
loop another time or leaving the loop.
There are usually three categories of loops based on the techniques to exit them: counter controlled,
sentinel tested and general exit. In the first technique, a counter variable controls the loop so that it
repeats a prearranged number of times. In the second technique, the loop is ‘exited’ when a special
value, called a sentinel value, is met. For example, a sentinel value of zero may be used to show the end
of input of a list of integers. A loop whose exit test involves other than a counter or a sentinel value is in
the third category. The decision whether to repeat the loop body or not may be made either before or
after performing the instructions making up the body of the loop.
If the choice is made at the start of the loop, it is a top-tested loop. If the choice to repeat or not is made
after implementing the body of the loop, it is a bottom-tested loop. It is possible for the body of a top-
tested loop not to be implemented at all. The body of a bottom-tested loop, on the other hand, will
always be implemented at least once. Counted loops are top tested.
Most current programming languages such as VB.NET, Java, C++ and others are produced around these
three structures. However, there are others structure such as jumping and storage. Jumping is being
able to jump to a specific step when needed while storage is all about to store data for use in
instructions further down the list
Examples 3.0
Example 3.1
Example 3.2
repeat
repeat 3 ties
unplug lamp