CHAPTER ONE: INTRODUCTION TO COMPUTER PROGRAMS
Upon completion of this chapter, each student must understand the concepts of: Objective1#
What is computer programming?
Objective2#
Why programming is very important for human life?
Objective3#
The importance of good programs
Objective4#
Important terminology
Objective5#
Program Development Life Cycle PDLC
1.1
What is computer programming?
A computer is a device capable of performing computations and
making logical decisions at speeds millions and even billions of
times faster than human beings can.
A computer program is a sequence of steps or instructions that are
needed to accomplish a task or a problem. Or sets of instructions to
perform certain tasks.
These computer programs guide the computer through orderly sets
of actions specified by people called computer programmers.
Programmer is a person who writes computer programs to be used
by end-user according to certain specifications.
End user use the computer application of software packages,
they do not need to know how to write the program.
A good skill in programming is very important for all programmers.
They should be able to write programs using any languages.
Programmer
must
be
able
to
conquer
the
concepts
of
programming.
There are many examples of programming languages such as C++,
BASIC, Fortran, COBOL, ADA, Prolog and etceteras.
Normally, programmers are familiar with language that they
frequently used, example like Basic or C++. But when they with the
new language they have to learn the new features (if exist) and this
may take about 1 month to understand and remember some of the
syntax provided in the language. Anyway the logic for all languages
are almost the same.
1.2
Why programming is very important for human life?
"Most of us don't know (and don't want to know) the first thing about
computer languages. Yet they are the catalysts of change in computer
technology. Mainframe computers weren't of much use in the workaday
world until Cobol came along and made it easy to write accounting and
inventory programs. IBM's Fortran language made it possible to
program engineering workstations and supercomputers for scientific
analysis. It was Basic--a version of which was Microsoft's very first
product--that let hobbyist hackers program early personal computers.
Another language known as C++ streamlined development of pointand-click graphical programs like the ones we use on Windows PCs
and Macs."
"Java looks like the language best suited to Internet computing
not just because it doesn't favor or discriminate against specific
machines but because it is inherently virus-proof--the language was
designed so applets can't alter data in your computer's files or on its
hard disk. (McNealy puckishly calls using Java programs the equivalent
of `practicing safe computing.')"
Extracted from : http://www.cs.waikato.ac.nz/~marku/313/why_important.html
May be in some cases programming able to solve problems
immediately.
In most cases programming is the best way of producing valuable
information.
1.3
The importance of good programs
Good programming not only depends on skills but also involved
some kinds of disciplinary in writing the programs. Most of
programmers must motivate their selves to emphasize with the
following checklists:
Proper documentation.
Giving the standard name and understandable for variables,
files and etc.
Indentation to get the clearer sequence of programming.
Comments
Eg : 3
// Author : Abu Hurairah bt Uwais
// Date Written : 10 September 2014
// The purpose of this program is to get two integers number
// from user and display the summation of two numbers.
#include <iostream>
void main()
{
int a, b, sum;
cout << \n Enter the first number;
cin >> a;
cout << \n Enter the second number;
cin >> b;
sum = a+b;
cout << The summation of << a << and << b << is << sum;
}
Every program you write that you intend to keep around for more
than a couple of hours ought to have documentation in it. Don't talk
yourself into putting off the documentation. A program that is
perfectly clear today is clear only because you just wrote it. Put it
away for a few months, and it will most like take you a while to
figure out what it does and how it does it. If it takes you a while to
figure it out, how long would it take someone else to figure it out?
Programming style is a term used to describe the effort a
programmer should take to make his or her code easy to read and
easy to understand. Good organization of the code and meaningful
variable names help readability, and liberal use of comments can
help the reader understand what the program does and why.
Good Programming Practices
1.
2.
3.
4.
5.
Start with a good design. Update the design documents
regularly. Create additional design documents before
adding major new features or functionality.
The program under development should be at all times
functioning. The development process consists of adding
new functionality without breaking existing functionality.
Work has to be divided into small incremental steps that
can be typically accomplished and code-reviewed in one
day. Even large-scale rewrites should be made
incremental.
Every line of code written or modified undergoes peer
review. The smallest team must contain at least two
programmers so that they can code-review each others
changes.
Always attempt to work top-down in:
Designstart with high level objects.
Implementationcreate top-level objects using
low-level stubs.
Modificationchange the top-level objects and
the overall flow of control first. If necessary, use
stubs, or fake new functionality using old
implementation.
For more information about good programming practices please
read from the following web sites:1. http://dogbert.comsc.ucok.edu/~mccann/style_c.html
2. http://www.joot.com/articles/practices.html
3. http://www.oreillynet.com/pub/wlg/2083
4. http://www.bolthole.com/solaris/ksh-paranoia.html
1.4
Important terminology
Compilers translates high-level program into machine language.
(object code)
Interpreters directly execute high-level language programs
without the need for compiling those programs into machine
language.
Source files program which written in high level programming
language.
5
Object file the translated version of source file.
Algorithm sequence of procedures or processes in solving a
problem.
Syntax errors error due to violation of programming languages.
Logic errors the real result is not the same as with the expected
result. Or wrong output in a syntactically correct program
Program the set of instructions can solve a specific task written in
a programming language.
Something that you should know about programming.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
This page contains a number of important programming
truths that every budding programmer should know about.
These truths are self-evident, and need no explanations.
If it compiles, it works.
If it compiles, it's correct.
If it runs, it doesn't have any bugs.
If it doesn't have any immediately obvious bugs, it's perfect.
If a bug doesn't show, it doesn't exist.
If it seems to work, it works.
Doing something right is easy. Avoiding errors only takes a
bit of concentration.
The shorter the source code, the faster the program.
It's obvious how to optimize a program.
Programmers don't make mistakes.
Run-time errors don't occur.
Users don't make mistakes.
I don't make mistakes.
Errors of any kind are rare.
Error handling can be done in version 2.
It's OK to crash on bad input.
It's OK to give incorrect output on bad input.
Portability isn't useful.
All the world's a VAX. Or, these days, an MS-DOS box
The length of the feature list is important.
Speed is good, features are better.
Slowness can be fixed in hardware.
The bigger a program is, the better it is.
Testing takes only a short while.
Finding bugs is easy. Fixing bugs is trivial.
Bug-fixes don't need to be tested.
Trivial changes of any kind don't need to be tested.
The first approach, idea, or version is always the best.
6
30.
31.
32.
33.
34.
1.5
Comments are meant for people other than the original
author of the code.
Undocumented features are fun and useful.
It can always be fixed in the next version.
Surprised users are happy users.
Demonstrating for clients is the best debugging method.
Programming Development Life Cycle (PDLC)
PDLC - the phases are :
o
Analyze study current system and suggest the best
solution, identify the requirement and related information
needed for the project.
Design design the database, how output should be
displayed, algorithm, etc.
Coding concerning on writing programs.
Testing avoid errors and debug the program, concepts of
syntax and logic errors.
Maintenance maintain system, non-stop improve the
system.
For CSC425, we focus on the first phase of PDLC only. This phase
is consists of several tasks :o
Understand the situation of problem, defined the problem.
Put it in program specification.
Write or produce algorithm (either pseudo-code or flowchart).
Problem 1 (e.g):
Calculate the product of two numbers.
i.
Defined the above problem.
Key in any two numbers then multiply both numbers.
ii.
Program specification.
Input : first number, second number
Process : multiply the first number with the second number
Output : Display the result of multiplication.
iii.
Algorithm (Pseudo-code)
1. Input first number
2. Input second number
3. result = first number * second number
4. Output The result of multiplication for both numbers result
Problem 2 (e.g) :
Calculate the average of 5 marks
i.
Defined the problem.
Key in 5 marks and calculate total marks for all 5 marks,
then divide by 5 to get the average.
ii.
Program Specification.
Input : mark1, mark2, mark3, mark4, mark5
Process : average = (mark1 + mark2 + mark3 + mark4 +
mark5) / 5
Output : Display the average
iii.
Algorithm
1. Input mark1, mark2, mark3, mark4, mark5
2. average = (mark1 + mark2 + mark3 + mark4 + mark5) / 5
3. Output The average for 5 numbers average
First Sample of Non Procedural
#include <iostream>
Programming Concepts
int main()
{
// Author : Norlis Othman
// Date : 18 May 2014
int a, b, result;
// Program to get the multiplication of
// two numbers
cout << "\n Enter the first number : ";
cin >> a;
int multiply (int, int);
cout << "\n Enter the second number
int main()
: ";
cin >> b;
int a, b;
result = a * b;
cout << "\n Result of " << a << " * "
cout << "\n Enter the first number : ";
<< b << " is " << result;
cin >> a;
return 0;
cout << "\n Enter the second number
: ";
cin >> b;
cout << "\n Result of " << a << " * "
<< b << " is " << multiply(a,b);
First
Sample
of
Procedureal
Programming Concepts
// Author : Norlis Othman
return 0;
}
// Date : 18 May 2014
// Program to get the multiplication of
int multiply (int x, int y)
// two numbers
{
return x * y;
#include <iostream>
First Sample of Object Oriented Programming Concepts
// Program :
file
// Author : Norlis Othman
// Date : 18 May 2014
// Program to get the multiplication of
private :
int a,b, result;
public :
// two numbers
void getdata (int, int);
#include <iostream>
int result1();
void displayresult();
class multiply {
~multiply();
};
{
cout << "\n end ";
void multiply::getdata(int x, int y)
{
a = x;
// Program : file .cpp
b = y;
#include <iostream.h>
#include "multiply.h"
void main()
int multiply::result1()
int a, b;
result = a * b;
multiply t;
return result;
cout << "\n Enter the first number : ";
cin >> a;
cout << "\n Enter the second number
void multiply::displayresult()
: ";
cin >> b;
cout
<<
"\n
The
result
after
t.getdata(a,b);
multiplication of two numbers : "<<
t.result1();
result;
t.displayresult();
t.~multiply();
}
multiply::~multiply()
Second Sample of Non Procedural Programming Concepts
// Author : Norlis Othman
// Date : 18 May 2014
//Program to get the average of 5 marks
#include <iostream>
void main()
10
{
float mark1, mark2, mark3, mark4, mark5, average;
cout << "\n Please enter the first mark : ";
cin >> mark1;
cout << "\n Please enter the second mark : ";
cin >> mark2;
cout << "\n Please enter the third mark : ";
cin >> mark3;
cout << "\n Please enter the fourth mark : ";
cin >> mark4;
cout << "\n Please enter the fifth mark : ";
cin >> mark5;
average = (mark1 + mark2 + mark3 + mark4 + mark5)/5;
cout << "\n The average mark is : " << average;
}
Second Sample of Procedural Programming Concepts
// Author : Norlis Othman
float mark1, mark2, mark3, mark4,
// Date : 18 May 2014
mark5, ave;
//Program to get the average of 5
void input(void);
marks
float average(void);
#include <iostream>
void main()
{
11
input();
cin >> mark3;
cout << "The average of 5 marks : "
cout << "\n Please enter the fourth
<< average();
mark : ";
cin >> mark4;
cout << "\n Please enter the fifth
void input()
mark : ";
cin >> mark5;
cout << "\n Please enter the first
mark : ";
cin >> mark1;
float average()
cout << "\n Please enter the second
mark : ";
ave = (mark1 + mark2 + mark3 +
cin >> mark2;
mark4 + mark5)/5;
cout << "\n Please enter the third
return ave;
mark : ";
12