0% found this document useful (0 votes)
4 views

Unit 02 - Problem Solving - Programming

The document outlines the concept of problem-solving, defining it as the process of transforming a problem description into a solution using knowledge and appropriate strategies. It details the software development process in stages, including formulating requirements, determining specifications, designing algorithms, implementing, testing, and maintaining programs. Additionally, it explains algorithms and flowcharts as tools for representing problem-solving steps, providing examples and exercises for practice.

Uploaded by

Twaf creative
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Unit 02 - Problem Solving - Programming

The document outlines the concept of problem-solving, defining it as the process of transforming a problem description into a solution using knowledge and appropriate strategies. It details the software development process in stages, including formulating requirements, determining specifications, designing algorithms, implementing, testing, and maintaining programs. Additionally, it explains algorithms and flowcharts as tools for representing problem-solving steps, providing examples and exercises for practice.

Uploaded by

Twaf creative
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

MicroLink Information Technology College

Chapter TWO

What is a Problem?
A problem is a question or situation that presents uncertainty, perplexity, or difficulty. Real life is full of easy and
difficult problems; so is every professional field of endeavor. To succeed in life, as well as in our professions, we
should develop the ability to solve problems.

Problem Solving
Problem solving is the process of transforming the description of a problem into the solution of that problem by
using our knowledge of the problem domain and by relying on our ability to select and use appropriate problem-
solving strategies, techniques, and tools.

Problems can be solved using different techniques as well as different tools. We will consider those problems that
can be solved by using computers as a tool in this course. Computers can be programmed to do many complicated
tasks at very high speeds. Also they can store large quantities of information. Therefore we can use computers as a
tool in problem solving if one or more of the following conditions for a problem hold true:

1. It has extensive input


2. It has extensive output
3. If a method of solution is too complicated to implement manually
4. If done manually, it takes an excessively long time to solve
5. We expect to use the same method of solution often in the future to solve the same problem with
different inputs


Planning a Program Is Like Planning a House


The Software Development Process
The process of creating a program is often broken down into stages according to the information that is produced
in each phase. In a nutshell, here’s what you should do.
1. Formulate Requirements: Find out exactly what the problem to be solved is. Try to understand as much
as possible about it. Until you really know what the problem is, you cannot begin to solve it.
2. Determine Specifications: Describe exactly what your program will do. At this point, you should not
worry about how your program will work, but rather with deciding exactly what it will accomplish. For
simple programs this involves carefully describing what the inputs and outputs of the program will be and
how they relate to each other.
3. Create a Design: Formulate the overall structure of the program. This is where the how of the program
gets worked out. The main task is to design the algorithm(s) that will meet the specifications. We use

Prepared by Faniel
Page 1
MicroLink Information Technology College

algorithms to create a design. Algorithms can be represented by using flowcharts or pseudo code. We will
see algorithms in the next sub unit.
4. Implement the Design: Translate the design into a computer language and write it into the computer. In
this course, we will be implementing our algorithms as C++ programs.
5. Test/Debug the Program: Try out your program and see if it works as expected. If there are any errors
(often called bugs), then you should go back and fix them. The process of locating and fixing errors is
called debugging a program.
6. Maintain the Program: Continue developing the program in response to the needs of your users. Most
programs are never really finished; they keep evolving over years of use.

Example: Let’s go through the steps of the software development process with a simple real-world example.
Hagos has a problem. Hagos is an Applied Geology student spending a year studying in USA. He has no problems
with programming language, as he is fluent in many including C++. His problem is that he has a hard time figuring
out the temperature in the morning so that he knows how to dress for the day. He listens to the weather report
each morning, but the temperatures are given in degrees Fahrenheit, and he is used to Celsius. Fortunately, Hagos
has an idea to solve the problem. He thinks it might be possible that a computer program could help him out.
Hagos begins with the requirements of his problem. In this case, the problem is pretty clear: the radio announcer
gives temperatures in degrees Fahrenheit, but Hagos only comprehends temperatures that are in degrees Celsius.
That’s the crux of the problem.

Next, Hagos considers the specifications of a program that might help him out. What should the input be? He
decides that his program will allow him to type in the temperature in degrees Fahrenheit. The program will display
the temperature converted into degrees Celsius. Now he needs to specify the exact relationship of the output to
the input. Hagos does some quick figuring to derive the formula
C=
That seems an adequate specification. Notice that this describes one of many possible programs that could solve
this problem. If Hagos had background in the field of Artificial Intelligence (AI), he might consider writing a
program that would actually listen to the radio announcer to get the current temperature using speech recognition
algorithms. The purpose of specification is to decide exactly what this particular program will do to solve a
problem. Hagos knows better than to just dive in and start writing a program without first having a clear idea of
what he is trying to build.
Hagos is now ready to design an algorithm for his problem. He immediately realizes that this is a simple algorithm
that follows a standard pattern: Input, Process, and Output (IPO). His program will prompt the user for some input
information (the Fahrenheit temperature), process it to convert to a Celsius temperature, and then output the
result by displaying it on the computer screen.
Hagos could write his algorithm down in a computer language. However, the precision of writing it out formally
tends to stifle the creative process of developing the algorithm. Instead, he writes his algorithm using pseudocode.
It is meant to communicate algorithms without all the extra mental overhead of getting the details right in any
particular programming language.
Here is Hagos’s completed algorithm:

Input the temperature in degrees Fahrenheit (call it fah)


Calculate Celsius as 5/9(Fah - 32)
Output Celsius

Prepared by Faniel
Page 2
MicroLink Information Technology College

The next step is to translate this design into a C++ program. This is straightforward, as each line of the algorithm
turns into a corresponding line of C++ code.
// A program to convert Fahrenheit temps to Celsius
#include <iostream>
using namespace std;
int main(){
float cels,fah;
cout<<”What is the Fahrenheit temperature: ”;
cin>>fah;
cel = 5.0/9.0*(fah - 32);
cout<<"The temperature is "<<cels<<" degrees Celsius.";
return 0;
}
See if you can figure out what each line of this program does. Don’t worry if some parts are a bit confusing. They
will be discussed in detail in the forthcoming units.
After completing his program, Hagos tests it to see how well it works. He uses some inputs for which he knows the
correct answers. Here is the output from two of his tests.
What is the Fahrenheit temperature? 32
The temperature is 0 degrees Celsius.

What is the Fahrenheit temperature? 212


The temperature is 100 degrees Celsius.
You can see that Hagos used the values of 32 and 212 to test his program. It looks pretty good, and he is satisfied
with his solution. Apparently, no debugging is necessary.

Algorithm
An algorithm is a sequence of a finite number of steps arranged in a specific logical order, which when executed
produces the solution for a problem. We can consider it as a procedure that takes a set of values as input and
transforms them into a set of values as output. An algorithm is used to design a program. Obviously, not any
arbitrary constructed series of steps can be regarded as an algorithm. An algorithm must satisfy some
requirements:

1. Unambiguousness: The algorithm must not be ambiguous. In most cases we develop an algorithm so that
it can be translated into a program to be executed by computers. Computers cannot cope with
ambiguities. Therefore, every step in an algorithm must be clear as to what it’s supposed to do and how
many times it’s expected to be executed.
2. Generality: All possible circumstances are handled.
3. Correctness: It must be correct and solve the problem for which it is designed.
4. Finiteness: It must execute its step and terminate in finite time. An algorithm that never terminates is
unacceptable.

Flowchart
An algorithm can be represented by using a pseudocode or flowchart. Pseudocode is a semiformal, English-like
language with a limited vocabulary that can be used to design and describe algorithms. The algorithm that was
used to solve the problem in the previous example of software development process was represented by using
pseudocode.

Prepared by Faniel
Page 3
MicroLink Information Technology College

As an alternative to pseudocode we can also use flowcharts. A flowchart is a diagram that uses graphic symbols to
depict the nature and flow of steps in a process. The geometrical shapes in a flowchart represent the types of
statements in an algorithm. The details of statements are written inside the shapes. The flow lines show the order
in which the statements of an algorithm are executed.

The following symbols are used in a flowchart

Symbol Meaning Explanation


Start Process It is used as the first symbol that indicates an algorithm has
Start started.

End Process It is used as the last symbol that indicates an algorithm has
Stop ended.

Process Step It represents processes such as computations, assignments and


initializations.

Decision It defines two courses of action depending on the outcome of a


YES condition. A condition is an expression that when evaluated
Condition
computes to either yes or no.
NO

Flow line It is an arrow that flows from one step to the next step.
Connector It is used to connect two flow lines.
Input/output It is a special process that is used to get input or to display
output.

Examples

1. Design a program that receives temperature in degree Fahrenheit and converts it to degree Celsius using
a flowchart.

Start

Print Enter the temperature in degree Fahrenheit

Input temperature in Fahrenheit

Celsius =

Print The temperature is Celsius ○C

Prepared
End by Faniel
Page 4
MicroLink Information Technology College

2. Design a program that receives two numbers and prints the smallest using a flowchart

Print Enter two numbers

Input num1, num2

num1 < num2? No


Yes

Print num1 Print num2

End

3. Design a program that outputs the numbers between 1 and 10,000.

Start

counter = 1

YES No
counter <= 10000?

Print counter

counter = counter + 1

End

Prepared by Faniel
Page 5
MicroLink Information Technology College

Exercises
1. Design a program that reads three numbers and prints the smallest number.
2. Design a program that keeps on reading numbers until the user enters a zero value.
3. Design a program that keeps on reading numbers until the user enters -1 and then determines
and outputs the largest number.
4. Design a program that reads five numbers and computes and outputs their arithmetic average.
Then generalize your algorithm so that it can handle an arbitrary number of numbers.
5. Design a program that accepts a positive integer n as input and computes and outputs the
factorial of the number, i.e. the product of all integers from 1 to n.
6. Design a program that reads a positive integer and then computes and outputs all the positive
factors of that integer. For example, the positive factors of 33 are 1, 3, 11, and 33, which are
integers that divide 33 evenly.

Prepared by Faniel
Page 6

You might also like