0% found this document useful (0 votes)
88 views28 pages

Chapter 1 Part 2

The document discusses the characteristics of good computer programs. It states that for a program to be considered good, it must have reliability of output, interactivity, and readability. Reliability of output means the program produces correct output when tested with different input data. Interactivity refers to clear interaction between the user and program. Readability involves proper indentation and comments to help others understand the code. The document also discusses common types of programming errors like runtime errors, syntax errors, and logic errors.

Uploaded by

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

Chapter 1 Part 2

The document discusses the characteristics of good computer programs. It states that for a program to be considered good, it must have reliability of output, interactivity, and readability. Reliability of output means the program produces correct output when tested with different input data. Interactivity refers to clear interaction between the user and program. Readability involves proper indentation and comments to help others understand the code. The document also discusses common types of programming errors like runtime errors, syntax errors, and logic errors.

Uploaded by

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

CSC126 FUNDAMENTALS OF ALGORITHMS AND

COMPUTER PROBLEM SOLVING

CSC126 FUNDAMENTALS OF ALGORITHMS AND COMPUTER PROBLEM SOLVING 1


LESSON OUTCOMES
Upon completion of this chapter, students should be able to:

 Describe what a computer program is


 Explain the importance of programming to computer use
 Appreciate the importance of good programs
 Recognize program errors
 Become familiar with the program design process

CSC126 FUNDAMENTALS OF ALGORITHMS AND COMPUTER PROBLEM SOLVING 2


In order for a program to be considered as a good program, it must have
the following:
RELIABILITY OF OUTPUT

1  A good program must be able to produce correct output.


 For that, during testing phase, a different set of input data is
used to ensure the reliability of the output.

CSC126 FUNDAMENTALS OF ALGORITHMS AND COMPUTER PROBLEM SOLVING 3


In order for a program to be considered as a good program, it must have
the following:

I N T E R AC T I V I T Y
 The interaction process between the user and the program

2 must be well defined.


 The interactivity is important so that the user knows the
processing status.
 User‐friendly programs allow the users to responds to
instruction correctly and allow users to key in valid input
thus minimizing the errors resulted from invalid data.

CSC126 FUNDAMENTALS OF ALGORITHMS AND COMPUTER PROBLEM SOLVING 4


 E xa m p l e o f a BA D p ro g ra m’ s o u t p u t s c re e n
This output produces the blinking cursor that expects the
user to enter some data. However, the kind of data to be
entered is unknown to the user.

CSC126 FUNDAMENTALS OF ALGORITHMS AND COMPUTER PROBLEM SOLVING 5


 E xa m p l e o f a G O O D p ro g ra m’ s o u t p u t s c re e n
This output screen tells the user what will be achieved when
entering the input data. The instruction helps the user to
enter data to be correctly processed by the program.

CSC126 FUNDAMENTALS OF ALGORITHMS AND COMPUTER PROBLEM SOLVING 6


In order for a program to be considered as a good program, it must have
the following:
P RO G R A M R EA DA B I L I T Y
 Readability is concerned with how other person views one’s
program. For programmers, the use of indention and
comments are common to improve the program’s readability.

3 a. Indentation
 Indentation helps in making the structure of the program
clearer and easier to read. A statement within a statement
should be indented to show the user which statements are
subordinated of the other.
 In C++, particularly, if‐else, while and do‐while statements
should be indented. Embedded braces { } are also indented
to make it easier to find the matching pairs.
CSC126 FUNDAMENTALS OF ALGORITHMS AND COMPUTER PROBLEM SOLVING 7
3

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING 8


In order for a program to be considered as a good program, it must have
the following:

a. Comments
 Some explanatory notes or comments (sometimes
referred to as internal documentation) should be place in

3 the program coding to improve its readability. In other


words, comments are there for the convenience of anyone
reading the program.
 Comments can be placed anywhere within in a program
and they will NOT be executed by the compiler.
Commenting out a section of code is very useful in a
debugging process.

CSC126 FUNDAMENTALS OF ALGORITHMS AND COMPUTER PROBLEM SOLVING 9


In order for a program to be considered as a good program, it must have
the following:

a. Comments
 Some explanatory notes or comments (sometimes
referred to as internal documentation) should be place in

3 the program coding to improve its readability. In other


words, comments are there for the convenience of anyone
reading the program.
 Comments can be placed anywhere within in a program
and they will NOT be executed by the compiler.
Commenting out a section of code is very useful in a
debugging process.

CSC126 FUNDAMENTALS OF ALGORITHMS AND COMPUTER PROBLEM SOLVING 10


 C++ supports two types of comments:
 Line comment
 A line comment begins with two slashes (//) and
continues to the end of line.

CSC126 FUNDAMENTALS OF ALGORITHMS AND COMPUTER PROBLEM SOLVING 11


 Block comment
 Block comment begins with the symbol /* and end with
symbol */. Block comments are conveniently used for
statements that span across two or more lines.

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING 12


Although the principle in programming is for efficiently produce
readable and error‐free programs that work correctly, errors or
sometimes are called bugs can occur at any time.
TYPES OF
PROGRAM
ERRORS
2. SYNTAX
p

Errors

1.RUN-TIME 3.LOGIC
Errors Errors

CSC126 FUNDAMENTALS OF ALGORITHMS AND COMPUTER PROBLEM SOLVING 13


1
RU N - T I M E E R RO RS
Run‐time errors occur during the execution of a program and
are detected by the compiler.

CSC126 FUNDAMENTALS OF ALGORITHMS AND COMPUTER PROBLEM SOLVING 14


1 RU N - T I M E E R RO RS

Explanation:
Attempting to divide by zero in the above statement causes a
run‐time error such as “Floating point division by error” or
divide‐error exception”. The method for detecting errors
after a program has been executed is called debugging process.
A debugger program is available in C++ for detecting errors
while a program is being executed.

CSC126 FUNDAMENTALS OF ALGORITHMS AND COMPUTER PROBLEM SOLVING 15


SY N TA X E R RO RS

2 A syntax error is an error in the structure or spelling of a


statement. This error can be detected by the compiler during
compilation of the program.

CSC126 FUNDAMENTALS OF ALGORITHMS AND COMPUTER PROBLEM SOLVING 16


2 SY N TA X E R RO RS

Explanation:
i. Invalid use of backslash (/) in line 6
ii. Missing semicolon (;) in line 6
iii.Keyword cout is misspelled in line 7
iv. Invalid use of insertion symbol (>>) in line 7
v. Missing a closing quote (“) in line 7

A C++ statement with correct syntax will be accepted by the


compiler without any error messages. Nevertheless, a statement
or program can be syntactically correct but still be logically
incorrect. Consequently, incorrect result would be produced.

CSC126 FUNDAMENTALS OF ALGORITHMS AND COMPUTER PROBLEM SOLVING 17


3 LO G I C E R RO RS

 Logic errors refer to the unexpected or unintentional


errors resulted from some flaw in the program’s logic.
 Logic error is very difficult to detect since compiler cannot
detect this error.
 The only way to detect it is by testing the program
thoroughly and comparing its output against manually
calculated results.

CSC126 FUNDAMENTALS OF ALGORITHMS AND COMPUTER PROBLEM SOLVING 18


Some indications of logic errors include:
i. No numerical output
This is caused by a missing numerical output in a cout
statement.
Example:

CSC126 FUNDAMENTALS OF ALGORITHMS AND COMPUTER PROBLEM SOLVING 19


The output for the above program statement is

CSC126 FUNDAMENTALS OF ALGORITHMS AND COMPUTER PROBLEM SOLVING 20


ii. Unappealing or misaligned output
This either caused by an error in cout statement or
the use of formatting output statement (\n, \t).
Example:

CSC126 FUNDAMENTALS OF ALGORITHMS AND COMPUTER PROBLEM SOLVING 21


The output for the above program statement is

CSC126 FUNDAMENTALS OF ALGORITHMS AND COMPUTER PROBLEM SOLVING 22


iii. Incorrect numerical results
This is caused by either incorrect values or data types
assigned to the variables used in an expression, incorrect
arithmetic expression, an omission of a statement,
round off error, or improper use sequence of statement.

Example :

CSC126 FUNDAMENTALS OF ALGORITHMS AND COMPUTER PROBLEM SOLVING 23


Where fnum is of type character. The
3 valid character to be assigned is
char fnum = ‘A’;

CSC126 FUNDAMENTALS OF ALGORITHMS AND COMPUTER PROBLEM SOLVING 24


iv. Premature program termination
If the error is detected during the program execution, a
run‐time error occurs that results in an error message being
generated or abnormal and premature program
termination.

Example:

CSC126 FUNDAMENTALS OF ALGORITHMS AND COMPUTER PROBLEM SOLVING 25


Explanation:
The logic errors are:
i. Assigning invalid values to variable of type integer in

3
line 1
ii. Attempting to divide by zero in line 3
iii. Missing numerical output in line 4
iv. Taking the square root of a negative number in line 5

CSC126 FUNDAMENTALS OF ALGORITHMS AND COMPUTER PROBLEM SOLVING 26


Kahoot Quiz

CSC126 FUNDAMENTALS OF ALGORITHMS AND COMPUTER PROBLEM SOLVING 27


 Describe any TWO (2) characteristics of good programs.
 What is syntax error?
 What is logic error?
 What is the purpose of comments in a program?

CSC126 FUNDAMENTALS OF ALGORITHMS AND COMPUTER PROBLEM SOLVING 28

You might also like