Unit 1
Unit 1
Planning the Computer Program: Concept of problem solving, Problem definition, Program design,
Debugging, Types of errors in programming, Documentation.
Planning the Computer Program
Problem Solving
Today, we use computers in every field for various purposes. But, we know that they cannot solve the problems all
by themselves. Furthermore, we have to give step by step instructions to the computer for solving the problem.
We can define problem-solving as a process of understanding the problem, designing an algorithm for it, and finally
implementing the solution to it.
Therefore, we can say that a successful problem-solving process depends on the following factors:
Understanding the problem and defining it precisely.
Designing a proper algorithm (solution) for it.
Implementing the algorithm successfully.
When the problems are easy we can easily search out a solution. Whereas, complex problems require step by step
process to solve. Hence, this means that we have to apply problem-solving techniques to solve the problem.
Furthermore, this starts with finding a precise definition of the problem and ends with a successful solution.
Here, we will study understanding the problem in detail.
Understanding the Problem
It is very obvious that before finding the solution we should understand the problem well. Moreover, if we fail to
understand the problem we may end up with a useless solution for it. Hence, a wrong solution will not solve our
purpose of problem-solving. Therefore, we need to read the problem carefully and decide the different functions which
the solution will contain.
Moreover, we need to understand that what is the required output and how we can generate it. Besides, for proper
output, we surely need an input. The input can be single or multiple as per the problem. Hence, it is quite important to
maintain the necessary relationship between the input and the output.
Furthermore, we need to keep in mind that there should be all the number of inputs required to produce the output. At
the same time, the programmer should make sure that the number of inputs should be limited. Moreover, any
irrelevant input will result in consuming more space and time. Hence, unnecessary inputs should be avoided.
Therefore, we can say that spotting the minimum number of input for the correct output is an important point in
understanding the problem.
Important points in Understanding the Problem
Some of the important points that we should keep in mind while understanding the problem are as follows:
Read the problem very carefully.
Identify the functions that the solution (algorithm) should have.
Identify the required output.
Find a way to produce the required output.
Draw a proper relationship between the input and output.
Take all the necessary number of inputs.
Avoid unnecessary inputs.
Identify the correct number of the required input.
Further steps in problem-solving
After understanding the problem, the further steps are as follows:
Designing an algorithm
After understanding the relationship between input and output and the functionalities required we have to design an
algorithm. Furthermore, the algorithm should contain all the necessary functions to solve the problem. Moreover, it
should produce a proper output for every input.
Implementing the algorithm
After designing the algorithm we should implement and design a program to solve the problem. We can develop the
program using any programming language.
Evaluation
After developing the program we should run and test if it produces the correct output.
Syntax errors are due to the violation of the rules of a specific programming language. Syntax errors are thrown by
the compilers.
Logical errors:
Due to mistakes in the algorithm or program design or wrong formula or wrong logic which are not easily
detected.
Logical errors are also called Semantic errors. The statement has no syntax errors, so it will compile and run
correctly. However, it will not give the desired output as the logic is not correct.
Let us take an example.(with C++ programming Language)
#include<iostream>
using namespace std;
int main()
{
int n = 5; // To compute factorial of n
int factorial;
int i = 1;
while (i <= n)
{
factorial = factorial * i;
i++;
}
cout << "The Factorial of " << n << " is " << factorial << endl;
return 0;
}
Output:
#include<iostream>
using namespace std;
int main()
{
int n = 5; // To compute factorial of n
int factorial=1; // Initialize the product to 1
int i = 1;
while (i <= n)
{
factorial = factorial * i;
i++;
}
cout << "The Factorial of " << n << " is " << factorial << endl;
return 0;
}
Output:
#include <stdio.h>
int main()
{
int i,sum=0;
int k=1;
for(i=1;i<=10;i++)
{
sum=sum+k;
k++;
}
printf("The value of sum is %d", sum);
return 0;
}
Output:
This error generated if and only if written code is not understandable format to the compiler.
Ex:
main()
{
int x, y, z;
x + y = z; //semantic error
}
Runtime errors
Runtime errors are errors that occur while executing the program.
This implies that the program has no syntax errors.
Some of the most common run time errors our program may encounter are −
Infinite loop
Division by '0'
Wrong value entered by user (say, string instead of integer) etc.
An example is writing a program that tries to access the sixth item in an array that only contains five items using C
programming Language.
#include <stdio.h>
int main()
{
int i;
int ary[]={37,42,80,55,40};
printf("Elements of the array are:\n");
for(i=1;i<=4;i++)
{
printf("%d\n",ary[i]);
}
printf(" the 6th Elements of the array is:\n");
printf("\n%d",ary[6]);
return 0;
}
Output
#include <stdio.h>
int main()
{
int a=2;
int b=2/0;
printf("The value of b is : %d", b);
return 0;
}
Linker error
Linker is a program that takes the object files generated by the compiler and combines them into a single
executable file. Linker errors are the errors encountered when the executable file of the code cannot be generated
even though the code gets compiled successfully.
This error is generated when a different object file is unable to link with the main object file.
This may be due to wrong function prototyping, incorrect header files.
Most frequent linkererror is writing Main() instead of a main() method.
Example
#include <stdio.h>
int Main()
{
int a=78;
printf("The value of a is : %d", a);
return 0;
}
These errors are generated after compilation we link the different object files with the main’s object .
Compilation Errors:
Compilation is the process of converting a high-level coding language into a lower-level language that can be
better understood by the computer. Compilation errors occur when the compiler isn’t able to properly transform
the code into the lower-level code. This prevents the software from being launched or tested.
Examples: syntax errors such as omitting a required semicolon, using an undeclared variable, using a keyword for
the name of a variable."
In our syntax error example, if we were compiling print('hello', the compiler would stop and tell us it doesn’t know
how to convert this into a lower-level language because it expected a ) after the '.
Compilation happens across all files of the project at the same time. If we’ve made lots of changes and see lots of
compiler warnings or errors, it can be very daunting. By running the compiler often, we will get the feedback we
need sooner, and we will more easily know where to address the issues.
6. Resource errors
Sometimes, a program can force the computer it’s running on to attempt to allocate more resources (processor
power, random access memory, disk space, etc.) than it has. This result in the program becoming bugged or even
causes the entire system to crash.
7. Interface Errors
Interface errors occur when there is a disconnection between how we meant our program to be used and how it is
actually used.
Most things in software follow standards. If input our program receives doesn’t conform to the standards, we
might get an interface error.
In python when database connection fails for some reason, MySQLdb will raise an InterfaceError. It is noted
that interface Error only get raise when there is internal problem in connection to the database, MySQLdb will
not raise Interface Error because of wrong database name or password.
For example, an interface error might happen if we have an API that requires specific parameters to be set but
those parameters are not set.
DOCUMENTATION:
It is the detailed description of a program’s algorithm, design, coding method, testing and proper usage.
Documentation usually includes the necessary information about the requirements of the program – the
operating system, and hardware requirements needed for the program to run.
Documentation also contains technical information such as where and who created the program, who contact
when there’s a problem with the program and instructions on the use and maintenance of the program.
Any written text, illustrations or video that describe a software or program to its users is called program or
software document.
User can be anyone from a programmer, system analyst and administrator to end user. At various stages of
development multiple documents may be created for different users. In fact, software documentation is a critical
process in the overall software development process.
In modular programming documentation becomes even more important because different modules of the software
are developed by different teams. If anyone other than the development team wants to or needs to understand a
module, good and detailed documentation will make the task easier.
These are some guidelines for creating the documents −
Documentation should be from the point of view of the reader
Document should be unambiguous
There should be no repetition
Industry standards should be used
Documents should always be updated
Any outdated document should be phased out after due recording of the phase out
Advantages of Documentation
These are some of the advantages of providing program documentation −
Keeps track of all parts of a program or software
Maintenance is easier
Programmers other than the developer can understand all aspects of program
Improves overall quality of the program
Assists in user training
Ensures knowledge de-centralization, cutting costs and effort if people leave the system unexpectedly
Types of Documents
Program or software can have many types of documents associated with it. Some of the important ones include −
User manual − It describes instructions and procedures for end users to use the different features of the
program.
Operational manual − It lists and describes all the operations being carried out and their inter-dependencies.
Design Document − It gives an overview of the software and describes design elements in detail. It
documents details like data flow diagrams, entity relationship diagrams, etc.
Requirements Document − It has a list of all the requirements of the system as well as an analysis of
viability of the requirements. It can have user cases, real life scenarios, etc.
Technical Documentation − It is a documentation of actual programming components like algorithms,
flowcharts, program codes, functional modules, etc.
Testing Document − It records test plan, test cases, validation plan, verification plan, test results, etc. Testing
is one phase of software development that needs intensive documentation.
List of Known Bugs − Every software has bugs or errors that cannot be removed because either they were
discovered very late or are harmless or will take more effort and time than necessary to rectify. These bugs
are listed with program documentation so that they may be removed at a later date. Also they help the users,
implementers and maintenance people if the bug is activated.
MAINTENANCE
The final stage in programming is maintaining or updating the program. This is the stage where the programmer is
tasked to keep the program running smoothly and updated with the developments and changes in the field where it
is used.
Program maintenance is the process of modifying a software or program after delivery to achieve any of these
outcomes −
Correct errors
Improve performance
Add functionalities
Remove obsolete portions
Despite the common perception that maintenance is required to fix errors that come up after the software goes live,
in reality most of the maintenance work involves adding minor or major capabilities to existing modules.
For example, some new data is added to a report, a new field added to entry forms, code to be modified to
incorporate changed government laws, etc.
Types of Maintenance
Maintenance activities can be categorized under four headings −
Corrective maintenance − Here errors that come up after on-site implementation are fixed. The errors may
be pointed out by the users themselves.
Preventive maintenance − Modifications done to avoid errors in future are called preventive maintenance.
Adaptive maintenance − Changes in the working environment sometimes require modifications in the
software. This is called adaptive maintenance. For example, if government education policy changes,
corresponding changes have to be made in student result processing module of school management software.
Perfective maintenance − Changes done in the existing software to incorporate new requirements from the
client is called perfective maintenance. Aim here is to be always be up-to-date with the latest technology.
Maintenance Tools
Software developers and programmers use many tools to assist them in software maintenance. Here are some of
the most widely used −
Program slicer − selects a part of the program that would be affected by the change
Data flow analyzer − tracks all possible flows of data in the software
Dynamic analyzer − traces program execution path
Static analyzer − allows general viewing and summarizing of the program
Dependency analyzer − assists in understanding and analyzing interdependence of different parts of the
program