0% found this document useful (0 votes)
20 views17 pages

Emailing CSC102-1

Uploaded by

judeanyachebelu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views17 pages

Emailing CSC102-1

Uploaded by

judeanyachebelu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Nnamdi Azikiwe Universiy Awka Nigeria

Department of Computer Science

Course code: csc102

Course Title: Introduction to Computer Programming


1.0 Problem solving Strategies
When you are faced with a problem, whether it is from any real-world problem or perhaps even from
the abstract world, how do you answer it? First thing to do before finding a solution to the problem, the
problem must first be clearly identified. Thereafter, one of numerous problem solving approaches can
be applied such as computer programming, confidently resulting in a solution. It is vital to note
computer science is all about solving problems with computers, irrespective of the field of study.

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.

Figure 1.0 Computer Data Processing Model

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.

 Problem solving approach is a plan of action utilized to find a solution.


 Problem solving is the sequential process of analyzing data related to a given situation
and producing appropriate response options.

The following steps are need in order to solve a problem.

1. Understand the problem


2. Formulate a model
3. Develop an algorithm
4. Write the program
5. Test the program
6. Evaluate the solution

Let”s consider a simple example of how the input/process/output works on a basic problem.

Example: Compute the average scores for all student in a class.

1. Input: get all the scores…by typing them in the keyboard or copying them/reading
from a USB or hard drive.

2. Process: add them all up and calculate the average grade.

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.

Step 1 Understand the Problem

You need to ensure you understand the problem that you are attempting to solve. Listed below are the
things you need to know:

 What information is available?


 What does it signify?
 What format is it in?
 Do I have all that is required?
 What output data am I attempting to generate?
 What do I want the result to appear like e.g text, picture or graph?
 What am I going to have to compute?

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)

Where a number from 0 to 100 will be the result.

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:

Table 1.0 Assigning integer number to letters

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)

Where, the result would be a number from 0 to 12.

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.

 Algorithm is a set of instructions designed to perform a specific task

However, more complex algorithms may be considered randomized algorithms or non-deterministic


algorithms where the instructions are not necessarily in set and in may not even have a finite number of
instructions.

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.

Figure 1.1 Solving problem in pseudocode and flowchart

 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.

 Flowchart is a graphical representation of an algorithm or process consisting of boxes or nodes


linked by directed edges. Programmers frequently use it as a program planning tool to solve a
problem. It makes use of symbols which are connected among them to show the flow of
information.

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

1. Set the sum of the grade value to 0


2. Load all grades x1 + x2+ xn from folder
3. Repeat n times {
4. get grades x1
5. add x1 to the sum
}
6. Calculate the average to be sum / n.
7. Print the average

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.

Step 4. Write the Program

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.

Step 5. Test the program

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.

Step 6. Evaluate the Solution

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.

1.2 Steps Included in Algorithm Development


The following are the steps involved to develop an algorithm.

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 2: Identification of output:

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.

1.2.1 Properties of an Algorithm


 Finiteness: An algorithm must terminate in a finite number of steps
 Definiteness: Each step of the algorithm must be exactly and clearly stated.
 Effectiveness: Each step must be effective, in the sense that it should be primitive easily
convertible into program statement.
 Generally: The algorithm must be complete in itself so that it could be used to solve problems of
precise sort for any input information.
 Input/output: Each algorithm must take zero, one or more quantities as input data generate
one or more output values.

1.2.2 Problem solving examples in Algorithm


1. Write an algorithm to find the area of the triangle.
Let b, c be the sides of the triangle ABC and A the included angle between the given sides.
Step 1: Input the given elements of the triangle namely sides b, c and angle between the sides A.
Step 2: Area = (1/2) *b*C* sin A
Step 3: Output the Area
Step 4: Stop.

2. Write an algorithm to find the largest of three numbers X, Y,Z.


Step 1: Read the numbers X,Y,Z.
Step 2: if (X > Y)
Big = X
else BIG = Y
Step 3: if (BIG < Z)
Step 4: Big = Z

Step 5: Print the largest number i.e. Big

Step 6: Stop.

1.3 PROBLEM SOLVING TERMINOLOGIES


There are some basic terms frequently used to define certain concepts and processes which are
associated to problem solving and some of them have already been mentioned above. However, find
some other terminologies described as follow.

 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.

1.4.1 Advantages of Flowcharts


 The flowchart shows the logic of a problem displayed in pictorial fashion which felicitates
easier checking of an algorithm.
 The Flowchart is good means of communication to other users. It is also a compact means of
recording an algorithm solution to a problem.
 The flowchart allows the problem solver to break the problem into parts. These parts can
be connected to make master chart.
 The flowchart is a permanent record of the solution which can be consulted at a later time.

1.4.2 Differences between Flowchart and Algorithm

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

1.4.3 Symbols used in Flow-Charts


Flowchart utilizes some normally accepted symbols connected together with direct arrows to signify
algorithm. Some of the most often used standard symbols while drawing is displayed below:
Name Symbol

Oval: Rectangle with rounded sides is


used to indicate either START/ STOP of
the program.

Input and output indicators:


Parallelograms are used to represent
input and output operations.
Statements such as INPUT, READ and
PRINT are represented in these
parallelograms.

Process Indicators: Rectangle is used


to indicate any set of processing
operation such as for storing
arithmetic operations.

Decision Makers: The diamond is used


for indicating the step of decision
making and therefore known as
decision box. Decision boxes are used
to test the conditions or ask questions
and depending upon the answers, the
appropriate actions are taken by the
computer. The decision box symbol is
Flow Lines: Flow lines indicate the
direction being followed in the
flowchart. In a Flowchart, every line
must have an arrow on it to indicate
the direction. The arrows may be in
any direction
On Page connectors: Circles are used
to join the different parts of a
flowchart and these circles are called
on-page connectors. The uses of these
connectors give a neat shape to the
flowcharts. Ina complicated problems,
a flowchart may run in to several
pages. The parts of the flowchart on
different pages are to be joined with
each other. The parts to be joined are
indicated by the circle.
1.4.4 Why Flowchart is not Preferred to Pseudocode
Flowcharts can be pictorial attractive, pseudocode is often the ideal choice for algorithm development
for the following reasons:

 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

Bonus = 250 Bonus = 0.08 *Salary

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.

UML is a language for visualizing, specifying, constructing and documenting.

A. Visualizing

 Communicating conceptual models to others is prone to mistake unless everyone involved


speaks the same language.
 There are items about a software system one cannot comprehend except you build models.
 A plain model facilitates communication

B. Specifying: The UML builds models that are precise, unambiguous and complete.

C. Language for constructing

 UML models can be directly linked to a variety of programming languages.


 Maps to java, c++, visual basic and so on
 Tables in Relational Database Management System (RDBMS) or persistent store in an Object-
Oriented Database Management System (OODBMS)

D. Language for documenting: The UML addresses documentation of system architecture,


requirements, tests, project planning and release management

2.1 UML Diagrams


The UML diagrams represent two different diagrams of a system model:

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.

Figure 2.1 Class diagram


B. Use case diagram: Describes the functionality provided by the system in terms of actors, their
goals represented as use cases and any dependencies among those use cases. It contains the
following diagrams:
 Use case: shows a set of use cases and actors and their relationships
 Activity: demonstrations the flow of events within a use case
 Sequence: Demonstrations how a use case would be implemented in terms of
collaborating objects

Figure 2.2 Use case diagram

C Sequence diagram: used to display how objects interact in terms of sequence of messages
Figure 2.3 Sequence diagram

3.0 Basic Control Structures: Sequence, Selection and Repetition


It is vital that we know the basic control or logic structures upon which programs are developed. These
are sequence, selection and repetition. Statements in a program are executed one after the other in the
order in which they are written this is called Sequential execution. This structure enable us capture the
solution to a problem that is just in the form of series of steps without separating or duplications.

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

1. Make sure switch is turned on


2. Check if lamp is plugged in
3. Check if bulb is burned out
4. ……

Example 3.1

cout << "Welcome ";

cout << "to ";

cout << "C++";

Example 3.2

repeat

get a new light bulb

put it in the lamp

until lamp works or no more bulbs left

repeat 3 ties

unplug lamp

plug into different socket

You might also like