You are on page 1of 45

CSC128

FUNDAMENTALS OF
COMPUTER PROBLEM SOLVING

Topic 1:
Introduction
Topic Covered

 Introduction to Programming
 Definition of computer and computer program

 Importance of computer programming

 Importance of good programs.

 Relationship between compilers, interpreters, assemblers and


programs
 Program Development Life Cycle – Problem
analysis, design, implementation, testing,
maintenance (documentation)
Objectives

Introduction to Programming

 To introduce the elements of a computer program


 To explain the importance of programming
 To describe good programming styles
 To explain the relationships between compilers,
interpreters and programs
Objectives

Program Development Life Cycle

 To explain problem definition/analysis


 To define algorithm design
 To explain algorithm implementation/ coding
 To discover program testing and debugging
 To explain program documentation and
maintenance
Elements of a computer program

Introduction to computer

 Computer is an electronic device that


accept input, processes data, store data and
generate the output
Elements of a computer program (cont.)

Basic components of a computer systems

o Hardware
o Device that processes data to create information (ex: input,
output, storage and processing devices)

o Software
o Step-by-step instructions that tell the computer how to do its
work.
o It is also called a program.
o It’s purpose to convert data to useful information
Elements of a computer program (cont.)

Basic components of a computer systems (Cont…)

SOFTWARE

SYSTEM SOFTWARE
• provides an environment for user
to execute the application software. APPLICATION SOFTWARE
•Enables the application software
to interact with computer • end user software.
hardware. •A program that performs a
•Eg. Operating System common task to the user.
•Eg. Word, Excel.
Elements of a computer program (cont.)

Computer Program

o Program is a set of instructions that the computer needs to


follow to process the data into information.

o Programming Language contains set of instructions, data and


rules that are used to construct a program. Ex: C++, C, VB

o Programming is the act of designing/writing and


implementing computer programs using the programming
languages.

o Programmer is a person who writes a computer program.


Example of C++ program
Example of C++ program (output)
Generation of Programming Languages

o 1st generation
o Machine Language is the basic language of the computer,
representing data as 1’s and 0’s.

o 2nd generation
o Assembly language (low-level language) that allows a computer user
to write a program using simple word instead of numbers.

o 3rd generation
o High-level language resembles some human language (English) –
eg. COBOL, FORTRAN, BASIC.
o Very high-level language, much more user-oriented and allow
users to develop programs with fewer commands compared with
procedural languages – eg. SQL, NOMAD
o Natural Language uses human language to give people a more
natural connection with computers.
Elements of a computer program (cont.)

STORAGE
•Store the data or information for
future use (permanently or
INPUT temporarily)
•E.g. Hard disk, memory
•Whatever data that is PROCESSING
inserted into a computer
using input devices •Convert input data into
•E.g. Mouse, keyboard information.
•E.g. CPU OUTPUT
•Generate the useful
information using output
devices
•E.g. Monitor, printer
The Importance of Programming

o Easy to express tasks from a particular problem


domain.
o Allow user to instruct computer to do a specific task
in a certain instances.
o A skill required for a computer scientist and software
engineering.
o A skill required to create a successful computer
program.
The Importance of Good Programs

Programmer’s view VS User’s view

 Reliability of Output
 Good program should produce correct output
 During testing phase different set of input data is used to ensure the
reliability of output

 Program’s Efficiency
 Good program should be reliable and efficient in the sense that it is
produces no errors during execution process
 Program must achieve its purpose so that the final result can be
trusted
 Use of pseudocode or flowchart to outline the program
The Importance of Good Programs (cont.)

 Interactivity
 Interaction between user and the program is well defined
 Interactivity is important so that the user knows the
processing status
 User-friendly programs allow user to respond to the
instructions correctly and allow them key in valid input

 Program readability
 Readability is concerned with how other person views one’s
program
 Use of indention and comment increase the level of
readability
The Importance of Good Programs (cont.)

o Program readability - Spacing


 Each instruction begins with new line

o Program readability – Indent (tab)


 Each instruction has indention depends on its structure
 Makes the structure of the program clearer and easy to read
 A statement within a statement should be indented to show the
user which statement is subordinate of the other
The Importance of Good Programs (cont.)

o Program readability - Comments


 Help a human reader to understand the program
 Can be placed anywhere within a program
 Will not be executed by compiler
 C++ supports two types of comments
 Line Comment
 Block Comment

o Program readability - Naming


 Give an appropriate names to the variables, functions, etc.
The Importance of Good Programs (cont.)

 Program readability – Comments (cont.)


 Line comment
 Begin with two slashes (//)
 Ex: //This is Line comment

 Block Comment
 Begin with the symbol /* and end with the symbol */
 Use for statements that span across two or more lines
 Ex: /* This program calculates the salary of employees */
Language Translator

o A program that accepts a human-readable source


statements and produces machine-readable
instructions.
o There are :
1. Assembler
2. Compiler
3. Interpreter.
Program Translator (cont.)

SOURCE CODE • a program is a plain text written


using any text editor

INTERPRETER
ASSEMBLER COMPILER
• a program that • a program that translates • a program that translates
translates the assembly high-level language into source code of high-level
language program into machine language. language into machine
machine language. •Translate the source code language.
once and saves to be reused. •Each instruction is
•Requires less memory and interpreted into machine
runs faster. language.
•Eg. PASCAL, C++, COBOL •Program is easier to develop.
•Eg. BASIC.
Program Translator (cont.)

 Steps in running a program using compiler


Program Translator (cont.)

 Steps in running a program using interpreter


Try Yourself 1

Differentiate between C++ compilation and


Java compilation.
Compiling a C++ Program

Using C++ Editor to produce the Source code


source code

Using C++ Compiler to compile compiler


the source code

The compiled process produces Object code


object code

Link the object code with linker


additional codes

Produce result Executable


code
Program Errors

 Also known as bugs


 trivia – where did the word ‘bug’ come from? 

 Can be divided into three


1. Compile-Time / Syntax errors
2. Run-Time errors
3. Logic errors
Program Errors (cont.)

 Compile-Time : detect by compiler

 Syntax errors
 Syntax error is an error in the structure or spelling of a statement
 Can be detected by compiler

 Example
1 cout << “/n There are five syntax errors”
2 cot >> “Correct Them;

Invalid use of backslash (/) in Line 1


Missing semicolon (;) in Line 1
Keyword cout is misspelled in Line 2
Invalid use of symbol (>>) in Line 2
Missing a closing quote( ” ) in Line 2
Program Errors (cont.)

 Run-Time errors detected when the program is being


run.
 Example : Divide by zero.

 Logic errors
 Refer to unexpected or unintentional errors resulted from
some flaw in the program’s logic
 Compiler cannot detect logic error
 Some indication of logic errors
 No output
 Unappealing or misaligned output
 Incorrect numerical results
 Premature program termination
Program Errors (cont.)

Logic errors (Cont…)


Example

1 int radius = 4.562, num = -4, count = 0;


2 float total = 10.0, average;
3 average = total/count;
4 cout << “Average is “;
5 cout << “\n the square root of ” << num << “ is ” << sqrt(num);

Assigning invalid values to variable of type integer in Line 1


Attempting to divide by zero in Line 3
Missing numerical output in Line 4
Taking the square root of a negative number in Line 5
Program Tracing

 Writing down each variable as it is encountered in


the program and listing the value that should be
stored in the variable
 Example :
i i<5 sum
1 int sum = 0;
2 for (int i=0; i<5; i++) - - 0
3 sum = sum + i; 0 T 0
4 cout << “Sum : ” << sum ;
1 T 1
2 T 3
3 T 6
4 T 10
5 F 10
Structured Programming VS
Object Oriented Programming
 Structured Programming
 Is a disciplined approach to writing programs that are clearer
than unstructured programs, easier to test, debug and modify.
 Top-down approach to writing programs that breaks programs in
modular forms and users standard logic tolls called control
structures (Sequential, Selection, Case, Iteration).

 Object Oriented Programming


 Is a disciplined approach to programming that results in program
that are easier to read, understand and less likely to contain
errors.
 Data and instructions for processing the data are combined into
self-sufficient “object” that can be used in other program.
Principles of Structured Programming

 Use Standard logic tools


(Control structures) :-
 Sequential = one program statement follows another in
sequence order.
 Selection = represents a choice, offers two paths to follow
when decisions must be made by a program.
 Case = variation on selection. More than a single yes-or-no
decision.
 Iteration = process may be repeated as long as a certain
condition remains true.
Problem Solving Phases

 Programming is a process of problem solving


 Consists of FIVE (5) phases
1. Problem definition/ analysis
2. Algorithm design
3. Algorithm implementation/ coding
4. Program testing and debugging
5. Program maintenance and documentation
1. Problem Definition/ Analysis

 Problem is defined to obtain a clear understanding


of the problem requirement

 The following questions should be asked to get a


complete problem specification:
 What are the input data?
 What are the output data?
 What formula is to be used?
 What other assumptions or constraints can be made?
 What is the expected output screen?
2. Algorithm Design

 The specifications derived in the analysis phase are


translated into the algorithm

 An algorithm is a set of sequential instructions that


are followed to solve a problem

 An algorithm can be represented by a pseudocode or


flowchart
2. Algorithm Design (cont.)

 Pseudocode
 Expresses an algorithm in everyday English rather than
programming language.
 Describe instructions in your own word
2. Algorithm Design (cont.)

o Flowchart - a diagram to represent a flow of an


algorithm :

Rectangle represents processing or


taking an action.

Diamond represents making a


decision.

Parallelogram represents an Input


and output.

Begin and end


2. Algorithm Design (cont.)

 Example

Problem : To calculate the average of three numbers

Algorithm of the problem using pseudocode

1. Input num1, num2, num3


2. Calculate average by adding the numbers and dividing the sum
by 3
• Sum = num1 + num2 + num3
• Average = Sum/3
3. Display the average
2. Algorithm Design (cont.)
Algorithm of the problem using flowchart

Start

Input num1,
num2,num3

Calculate the sum and average

Display the Average

End
3. Algorithm Implementation/ Coding

 Algorithm pseudocode or flowchart is converted into


program code, using the vocabulary or rules of usage
of the programming language that you are using.
4. Program Testing and Debugging

 To provide input to make sure the result is correct


or as needed by the user.

 Involves various testing stages :-


 Alpha testing = a desk-check, read through the program
to make sure there are no syntax and logic errors.
 Debug the program = to detect, locate and remove all
errors in a computer program, by entering some of the
input data.
 Beta testing = runs the real-world data to test the
program to make sure it works correctly (variety of live
data).
5. Program Maintenance and
Documentation

 Maintenance
 Any activities to keep the program in good working
condition, error free and up-to-date.
 Involves any amendments, additional coding or repairs to
the programs.

 Documentation
 Written description of a program, what it does and how to
use.
 Purpose for reference to other programmers if any
maintenance to be taken.
Try Yourself 2

Algorithm of the problem using pseudocode and


flowchart to determine odd or even number for 3
integer inputs.
Try Yourself 2 (hint)
Try Yourself 3

 From the given code, apply the good


program characteristic:
1. Spacing
2. Indent
3. Comments
4. Naming
Thank You

END

You might also like