0% found this document useful (0 votes)
30 views9 pages

CHAPTER 1 ML FUNDAMENTALS OF PROGRAMMING With C++ HANDOUT v2 3

Programming course

Uploaded by

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

CHAPTER 1 ML FUNDAMENTALS OF PROGRAMMING With C++ HANDOUT v2 3

Programming course

Uploaded by

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

Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC)

COVER PAGE
TABLE OF CONTENTS
TABLE OF CONTENTS ......................................................................................................................................... 1
PART ONE........................................................................................................................................................... 4
CHAPTER ONE – PRELIMINARY OF PROGRAMMING ............................................................................. 5
General Introduction to Computer System ....................................................................................... 5
What is a program?.......................................................................................................................... 6
Why Study C++? .................................................................................................................................... 11
Fundamentals of Programming with C++ Our First C++ Program .......................................................................................................................... 12
1.1.1. The Program Development Cycle ................................................................................................ 13
- Handout [ COCS-1013 ]
Step 1: De ne the Problem .............................................................................................................. 13
Step 2: Design the Program .............................................................................................................. 13
Step 3: Code the Program ................................................................................................................. 14
Step 4: Compile and Link the Program ............................................................................................. 14
Step 5: Execute the Program ............................................................................................................ 15
Step 6: Test and Debug the Program ................................................................................................ 15
1.1.2. Errors in the Cycle ....................................................................................................................... 15
CHAPTER TWO – INTRODUCTION TO C++ PROGRAMMING ................................................................. 17
The Origins of C++ .......................................................................................................................... 17
How Compilation of C++ program works ........................................................................................ 17
Structure of C++ Program ............................................................................................................... 18
Variable ......................................................................................................................................... 19
Identifiers ...................................................................................................................................... 20
Fundamental Data Types ............................................................................................................... 21
Constants ...................................................................................................................................... 22
1. Literal constants ............................................................................................................................... 22
2. De ned constants ............................................................................................................................. 22
3. Symbolic Constants........................................................................................................................... 23
Comments ..................................................................................................................................... 23
Simple Input Output ............................................................................................................................. 24
Modifying the output ........................................................................................................................... 25
Expressions .................................................................................................................................... 26
Eyasu G.Z. ( MSc, MA ) 1. Arithme c Operators ....................................................................................................................... 27
@joshkiyakoo : telegram
[email protected] : email 2. Rela onal Operators ........................................................................................................................ 28
3. Logical Operators ............................................................................................................................. 29

Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 1 of 68
Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC) Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC)

4. Increment/Decrement Operators .................................................................................................... 30 Default Arguments ........................................................................................................................ 59


5. Assignment Operator ....................................................................................................................... 31 Variable Number of Arguments / Overloaded functions ................................................................. 60
5.1 Simple assignment: ..................................................................................................................... 31 Inline Functions ............................................................................................................................. 61
5. 2 Compound assignment: ............................................................................................................. 32 Recursion ...................................................................................................................................... 62
6. Condi onal Operator ....................................................................................................................... 33 CHAPTER FOUR - EXERCISES ........................................................................................................... 64
7. The sizeof Operator .......................................................................................................................... 34 PART TWO........................................................................................................................................................ 67
Operator Precedence ..................................................................................................................... 34
CHAPTER TWO - EXERCISES ............................................................................................................ 36
CHAPTER THREE – STATEMENTS ........................................................................................................ 37
STATEMENTS (PROGRAM CONTROL STRUCTURE STATEMENTS) ..................................................... 37
1. Simple statement.............................................................................................................................. 37
2. Compound Statement ...................................................................................................................... 38
3. Selec on / Condi onal Statement ................................................................................................... 38
3.1 Two-way selec on ...................................................................................................................... 38
3.2 Mul ple-way Selec on ............................................................................................................... 40
4. Itera ve / Looping Statements ........................................................................................................ 42
4.1 The for loop ................................................................................................................................. 42
4.2 The while statement ................................................................................................................... 44
4.3 The do...while statement ........................................................................................................... 46
4.4 Nested Loop Statements ............................................................................................................ 47
4. Jump statements .............................................................................................................................. 48
4.5.1 Break statement ...................................................................................................................... 48
4.5.2 The con nue statement .......................................................................................................... 49
4.5.3 The goto statement ................................................................................................................. 49
4.5.4 The return statement .............................................................................................................. 50
CHAPTER THREE - EXERCISES .......................................................................................................... 51
CHAPTER FOUR – FUNCTIONS ............................................................................................................ 53
FUNCTIONS (PROGRAM BLOCK OF STATEMENTS) ........................................................................... 53
Function Declaration / Prototype Declaration ................................................................................ 53
Function Definition ........................................................................................................................ 53
Calling functions ............................................................................................................................ 54
A Simple Func on ................................................................................................................................. 55
Parameters and Arguments ........................................................................................................... 56
Global and Local Scope .................................................................................................................. 58
Scope Operator.............................................................................................................................. 59

Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 2 of 68 Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 3 of 68
Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC) Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC)

CHAPTER ONE – PRELIMINARY OF PROGRAMMING

General Introduction to Computer System

A Computer System, in programming refers to a complex structure that integrates hardware and so ware
components to perform a variety of tasks. It is designed to manage, process, and store informa on.

1. Hardware:

2. So ware:

In terms of programming, a computer system becomes a pla orm where programmers write and execute
their code. They use programming languages to instruct the computer to perform speci c tasks. These tasks
can range from simple calcula ons to complex opera ons like running an en re business's data management.

Computer System:

PART ONE 1.
2.
Computer So ware
Computer Hardware

1. Computer Hardware

This is the physical component of a computer system, which includes the computer itself and its peripherals
such as the monitor, keyboard, mouse, printer, etc. The hardware also includes internal components like the
CPU (Central Processing Unit), memory (RAM), and storage devices (HDD or SSD).

2. Computer So ware:

So ware is a generic term given for an organized collec on of computer data and instruc on. It is a nite set
of instruc ons (steps) that the computer follows to perform a given job. The set of instruc on is also known
as programs. So ware is a collec on of programs and rou nes that support the opera ons of performing a
task using a computer. So ware also includes documenta ons, rules and opera onal procedures. So ware
makes the interface between the user and the electronic components of the computer.

This refers to the programs or instruc ons that tell the hardware what to do. Mainly so ware can be divided
into two main types: System so ware and Applica on so ware. System so ware includes opera ng systems
like Windows, Linux, or MacOS which manage hardware resources and provide services for other so ware
to run on. Applica on so ware includes programs that perform speci c tasks for users, such as word
processors, web browsers, and games.

In further Computer So ware is divided in to three:

1. System So ware
2. Applica on So ware
3. U lity So ware
1. System So ware:
1. Opera ng System (OS)
2. Language So ware

Opera ng System (OS)

- First layer of so ware loaded into the computer working memory.

Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 4 of 68 Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 5 of 68
Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC) Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC)

- It is a program that acts as an interface between the user, the computer so ware and the hardware - The assembly-language program must be translated into machine code by a separate program called an
resources. assembler.

Basic func ons of OS - It is machine dependent language

o Process Management Example (instruc ons described using assembly language)


o Memory Management
Consists of symbolic instruc ons
o Device Management
o File Management Eg. MOVE, INT, ADD, SUB, etc..
o Security Management
3. High-Level Language
o User interfacing
o Coordina ng of communica on on the network - Allows users to write in a familiar nota on rather than numbers or symbolic nota ons
Language So ware
- They are closer to human languages
• Enables to develop an applica on or even the OS so ware. - They provide a much higher degree of abstrac on from the physical hardware
• Also called program translator
• Program Translator or Language So ware High level languages can be further classi ed as:
• Translates a program wri en in assembly or high-level language to a machine code. – 3rd genera on languages
Programming Language – 4th genera on languages
- allow users to develop programs with fewer commands
• A tool used by a programmer to write a computer program. - Rapid applica on development (RAD)
E.g. C++, Java, C, COBOL, BASIC, PASCAL, VISUAL BASIC etc. – 5th genera on languages
- programming languages that use human languages to write a computer program
What is a program?
Program Translators
- Sequence of instruc ons which a computer follows to do something task.
Translate a program wri en in assembly or high-level languages to a machine code
- When a computer executes an instruc on of a program, it does something.
Types of program translators:
A programming language can be divided in to 3 Main parts:
- Assemblers
1. Machine Language
- Compilers
- The only languages understood by computers.
- Interpreters
- The lowest possible level at which you can program a computer is in its own na ve machine code, consis ng
of strings of 1's and 0's.

The main problems with using machine code directly are: Assemblers

– it is very easy to make a mistake - Translate the assembly language program into machine code
– very hard to nd and correct errors

Compilers

2. Assembly Language - Converts the en re program of a high-level language into machine code before the computer executes the
program.
- Is nothing more than a symbolic representa on of machine code
- The programming instruc ons of a high-level language are called the source code.
- Assembly languages are very close to machine languages

- The computer cannot understand instruc ons wri en in assembly language.

Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 6 of 68 Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 7 of 68
Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC) Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC)

Interpreters
Compare the search key with the element of the list one by one starting from first element until a match is
- Converts each high-level language statement into machine language and execute it immediately statement
found or the list is exhausted.
by statement.
Expressing Algorithm
• The advantage of a compiler is that once you have obtained the object code, the program executes There are two ways by which you can express or describe your algorithm.
faster.
• The advantage of an interpreter, on the other hand, is that programs are easier to develop. A. Using Pseudo code
• It represents an algorithm in an English line text.
• Somewhere between formal English and a programming language.
Programming (Knowledge Transfer) • Must be language independent.
• Knowledge can be transferred from Human-to-human and Human-to-Computer
• Knowledge transfer from human to human is much simpler than knowledge transfer from human to Example:
computer. Why? To get the smallest number from the list for above example.
o All humans have the same internal working mechanism Start
o All humans have the ability to think Let min = the first element in the list
• Programming: is the system of transferring human knowledge from human to computers. Until the list is not exhausted
• Programmer: is the one who can transfer knowledge from human to computers. Do
• Program: is a human knowledge represented in computer understandable format. Or in professional Compare min with the next element
meaning, a program is a list of instruction that the computer must follow in order to process data into If min is greater than the compared number
information. min = the compared number
• What kinds of knowledge can be transferred from human to computer? Else
Answer: A knowledge that can be expressed algorithmically. Do nothing
• How can this knowledge be transferred? End Do
Answer: Using problem solving steps. End Until
• Algorithm: is a sequence of logical steps expressed informally that can accomplish a given task. Display min
• A Program: is a sequence of logical steps expressed formally that can accomplish a given task. Stop

Example: B. Using Flowchart


1. Given the following list, develop an algorithm that finds the smallest number. • Represent an algorithm graphically.
• Enforce programmer to put steps precisely.
13, -9, 6, 23, -3, 12, -12, 2 • Makes an algorithm easier to convert into program.
• More readable
• Can be easily hand traced.
a. Assume the first number in the list is the smallest number.
• It uses standard symbols.
b. Compare it with the next number and take the smallest.
c. Repeat comparing until the list is exhausted. Flow of Data |
Start | Stop Input | Output Process Decision
Instruction
Step 1: set 13 => Smallest Number
Step 2: take -9 => compare -9 < 13 ? True: -9 = New Smallest number
Step 3: take 6 => compare 6 < -9 ? False: No change
Step 4: take 23 => compare 23 < -9 ? False: No change
Step 5: take -3 => compare -3 < -9 ? False: No change
Step 6: take 12 => compare 12 < -9 ? False: No change Example: To get the smallest number form the list for the example provided above
Step 7: take -12 => compare -12 < -9 ? True: -12 = New Smallest number a = the ARREY list of the number
Step 8: take 2 => compare 2 < -12 ? False: No change i = a’s ith value in the list
n = list count
The list is exhausted and we have -12 as smallest number MIN = the smallest number found from the list

2. Given following list, develop an algorithm which searches a number in the list. a1 a2 a3 a4 a5 a6 a7 a8
13 -9 6 23 -3 12 -12 2
12, 4, -9, 8, -40, 24, 33, 5, -7 SN=11
Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 8 of 68 Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 9 of 68
Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC) Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC)

ENVIRONMENT SETUP
START Try it Option Online
You really do not need to set up your own environment to start learning C++ programming language. You
can compile and execute all the available examples online at the same time when you are doing your theory
work. This gives you confidence in what you are reading and to check the result with different options. Feel
ENTER ( a1, a2, a3, …an )
free to modify any example and execute it online.
Try the following example using online compiler option available at https://onecompiler.com/cpp

Code-Snippet-1
MIN = a1 and i = 2 #include <iostream>
using namespace std;
int main ( ) {
cout << "Hello World";
return 0;
T }
i >= n Display MIN STOP
Local Environment Setup
If you are still willing to set up your environment for C++, you need to have the following two software on
F your computer.

T Copy ai to MIN Text Editor


ai < min This will be used to type your program. Examples of few editors include Windows Notepad, OS Edit
MIN = ai command, Brief, Epsilon, EMACS, and vim or vi.
Name and version of text editor can vary on different operating systems. For example, Notepad will be used
F
on Windows and vim or vi can be used on windows as well as Linux, or UNIX.

i=i+1 The files you create with your editor are called source files and for C++ they typically are named with the
extension .cpp, .cp, or .c.
A text editor should be in place to start your C++ programming.

C++ Compiler
This is an actual C++ compiler, which will be used to compile your source code into final executable program.
Most C++ compilers don't care what extension you give to your source code, but if you don't specify
otherwise, many will use .cpp by default.
Most frequently used and free available compiler is GNU C/C++ compiler, otherwise you can have compilers
either from HP or Solaris if you have the respective Operating Systems.

Why Study C++?

• C++, pronounced "C-plus-plus," is one of the most popular programming languages used to develop
computer applications. Many corporate computing departments have adopted C++ as the language in which
to develop in-house applications. Many software vendors use C++ to write their application software. Why
is C++ so popular and why is learning it important?
• C++ is a "middle-level" language. Programming languages can be categorized as high-level or low-
level. Assembly languages are low-level languages. An assembly language allows the programmer maximum
control over the computer system. The programmer can directly interface with and control the computer's
hardware and operating system. This control comes at a price, however. Assembly languages are hard to
learn because they are highly symbolic and the programmer must take care of all the details of the program.
Assembly-language programs are also difficult to maintain and debug. Perhaps most important, assembly-

Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 10 of 68 Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 11 of 68
Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC) Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC)

language programs are not portable. An assembly-language program is specific to the hardware and 1.1.1. The Program Development Cycle
operating system of the computer platform on which it is developed. Thus, you cannot directly execute a PC You must take several steps each me you develop a program. The following six steps are typical when
assembly language program on a Macintosh. developing a C++ program. See Figure 1.1.

• By contrast, high-level languages, such as COBOL, Visual Basic, and Java, are easier to learn, maintain,
and debug than assembly languages. The program statements in such languages use English words and
familiar mathematical symbols to express instructions to the computer. Typically, each such instruction
replaces several equivalent assembly-language instructions. Thus, using a high-level language is easier than
using an assembly language. However, the price for this ease of use is the loss of direct control over the
computer system that you have in an assembly language.
• C++, although it is a middle-level language (and, therefore easier to learn and use than an assembly
language), contains many of the low-level capabilities of an assembly language. Thus, C++ is sometimes
called a "middle-level" language. It is possible through C++ to attain almost as much control over the
computer as you can achieve with an assembly language.
• C++ is portable. Most high-level languages are portable in the sense that a program written in a high-
level language should execute, with minor modifications, on several different computer systems. C++ is one
of the most portable of all the high-level languages. Properly written, a C++ program can run on several
computer systems with no modifications. Portability is important because it is now common for a business
to have several different types of computer systems (a mainframe, minicomputers, workstations, PCs, and
Macintoshes). A properly written application in C++ should be portable to most, if not all, these systems.
Thus, portability eliminates the cost of developing the application several times for different platforms.
• C++ is small. The C++ language does not contain many of the built-in features present in other Step 1: De ne the Problem
programming languages (such as in Visual Basic), which further enhances its portability. For example, Visual It is very important that you understand the problem you are trying to solve. Make sure you read the problem
Basic has about 150 keywords. (A keyword is a word that has special meaning in the language.) C++, on the specifications carefully. Know what data is to be input into the program and what form that data should be
other hand, has about 60 keywords. Visual Basic includes extensive capabilities to help the programmer in. Also, know what processing has to be done to that data. Finally, determine what the expected output is
write programs. However, this greatly increases the complexity of the Visual Basic language. In contrast, the supposed to be and what form the output must take.
designers of C++ tried to keep the language as simple as possible by not building things into the language.
Instead, the programmer must use programmer-defined and built-in functions and classes. This gives the For example, suppose you are asked to write a simple program that simulates a calculator. The program is to
C++ programmer great flexibility and allows the language to be highly portable. ask the user (the person running the program) to enter two numbers, then the program is to output the sum
• C++ is an object-oriented extension of the C programming language. Dennis Ritchie developed C at of the numbers. Before you start doing anything, you have to de ne this problem more completely.
Bell Laboratories in 1972 to help write the UNIX operating system for the PDP-11 minicomputer. Today many For example, should the program ask…the user to enter the numbers? If so, how should the program ask?
mainframes and most minicomputers, including Reduced Instruction Set Computing (RISC) workstations, and - What kind of numbers are to be input?
many Intel-based PCs and servers use the UNIX or Linux operating system. All versions of UNIX/Linux contain - Must they be whole numbers or can they be decimal numbers?
a version of C as their native language. In other words, UNIX/Linux speaks C. - Should the numbers be input one number per line, or can they both be input on one line?
- A er the numbers are input and added, how should the result be displayed?
Why is this important? - Do you want the input numbers output along with the sum, or do you just want to output the sum?
• Before C, a systems programmer on such a computer system needed to use assembly language to - If you want the input numbers as well as the sum to be output, how do you want it done in column
directly communicate with the computer. Now, along with the applications programmer, the systems format or on one line?
programmer can use C. In the early 1980s, Bjarne Stroustrup of AT&T Bell Labs developed C++, which is an As you can see, there are many questions to ask even in this simple problem and we did not ask any questions
object-oriented extension of C. about the processing, because adding is a simple calculation. Imagine how complex it would be to calculate
• C is an example of a procedure-oriented programming language. Thus, to develop a C program to a person's income tax!
accomplish a task, the programmer breaks down the task into a series of steps. On the other hand, to develop
an object-oriented C++ program, the programmer views the problem as a set of interac ng objects, each The important point here is to ensure that you solve the correct problem. Make sure that you understand
with its own proper es and behaviors. exactly what you have to do before you start the next step in the development cycle. If you are not sure
what you need to do, ask the person who wants you to develop the program. If you are writing the program
Our First C++ Program for yourself, then you have to make all the decisions. Do not waste time solving the wrong problem.
One of the best ways to learn about the basic features of a programming language is to study a small program
written in that language. In this section, we discuss a simple C++ program that displays a two-line message Step 2: Design the Program
on the computer screen. Before we do so, we discuss some general ideas about how to develop a program. There are several ways of describing program design. Two common methods are by owcharts and
pseudocode. A owchart is a graphic descrip on of a program design that uses combina ons of various

Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 12 of 68 Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 13 of 68
Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC) Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC)

standard owchart symbols. Figure 1.2 shows some standard symbols and the opera ons they represent. C++ compiler provides for use by all system users. In either case, you must use a program called a linker to
We shall use owcharts to represent only the basic program control structures. resolve these external references. The linker finds the other programs and makes them part of your program.
We call this process linking. The linker resolves external references and combines the object code with these
Pseudocode (literally, "false code") describes program design using English. Pseudocode avoids the syntax of other programs to produce the executable code. It is the executable code that the computer executes.
a programming language and, instead, emphasizes the design of the problem solu on. There is no standard
form of pseudocode you make up your own rules. The only guideline is that the meaning of the pseudocode The steps necessary for you to compile and link a program depend on the computer system and compiler on
should be clear to anyone reading it. We shall include a descrip on of the pseudocode of each program which you develop the program, and the IDE you use. Some IDEs simply require selecting the compile and
control structure that we introduce in the text. link options from pull-down menus. Some IDEs require that you create a project in which to run your
program. Other IDEs require executing one or two statements at command level to compile and link a
When you design a program, it is important to check its logic before you start to write the program code program. Consult your program IDE's manual or online help for instructions on the procedure you must
itself. If possible, have a friend or coworker check your program design. The program designer is frequently follow.
too close to his or her design to see some errors.
Step 5: Execute the Program
Step 3: Code the Program Once you have compiled and linked the program, you are ready to execute the program. Execu ng the
Note that in some cases, you do steps 3, 4, 5, and 6 (which follow) in an Integrated Development program is usually quite simple. In some IDEs, all you need to do is select the execute op on from a menu or
Environment (IDE). Examples of IDEs are Visual Studio, Code Block, DevC++ and Borland Enterprise Studio click an icon. On other systems, you need to execute the program at command level. Consult your program
for C++ for the Windows platform, and Code Forge for the Linux environment and KDevelop for the UNIX development environment user's guide for the procedure you must follow.
environment. All the steps are performed selecting from pull-down menus or clicking icons. All compiler
error messages are listed in a window separate from the edit window (see step 4). When you execute your Step 6: Test and Debug the Program
program (see step 5), it executes in a separate window. At no time do you have to leave the IDE. Even if a program executes, it does not mean that the program is correct. Even if a program gives correct
results using some data, it does not mean that the program will give correct results using another set of data.
A er you complete and check the program design, code the program. To code a program means to translate Make sure that you test your program under as many conditions as possible to try to catch errors.
the design from step 2 into a computer programming language (C++, in our case). Code the program by
entering it into your IDE's text editor, or any text editor on your computer system. Make sure that you save 1.1.2. Errors in the Cycle
the program to disk before proceeding to the next step. The six steps in developing a program form a cycle the program development cycle. An error at any step can
In the next step of the program development cycle, you will submit the program code, called the source force you to go back to an earlier step and begin over. Following are some errors that can occur in the cycle.
program or the source code, to the computer for compilation.
Errors in Step 1: An error at the problem definition stage can affect the entire program; make sure that you
Step 4: Compile and Link the Program understand the problem you are supposed to solve. Errors can arise in most of the following steps because
The only language a given computer understands is its own machine language. A machine language of a faulty understanding of the problem.
instruction is a binary-coded instruction (consisting of zeros (0’s) and ones (1’s)) that commands the
computer to do one specific task, such as add 1 to a register. Each computer has its own machine language. Errors in Step 2: After the initial program design, you or a friend might discover a flaw in the design. If this is
The machine language for a Sun workstation is different from the machine language of a Macintosh, which the case, you have to redesign part or the entire program.
is different from the machine language of an IBM PC, and so on. Because machine language is in binary, it is
difficult to write and find errors in machine language programs. Errors in Step 3: Errors in step 3, coding the program, can be caught by the IDE. Many IDEs color code
different syntactic elements of a program. This makes it easier to catch obvious mistakes in your coding.
Because a computer understands only its own machine language, it cannot execute high-level or middle- Make sure that you save your program after each correction that you make. Some coding errors, however,
level language instructions directly. The computer, using a program called a compiler, must translate middle- are not highlighted by the IDE. Therefore, after you have entered your program in the IDE's text editor, read
level and high-level language programs into equivalent machine language programs. A compiler is a the entire program. It is also a good idea to check a printed version of the program.
specialist. It translates only one high-level or middle-level language into a specific machine language. A PC-
based C++ compiler translates C++ into PC machine language. The compiler cannot translate a different Errors in Step 4: If the compiler finds any fatal errors in your program, it will not complete the translation of
language (such as Java) and cannot be used on a different type of computer (such as a Macintosh.) your program into machine language. You will receive a list of error messages detailing the type and location
of each error the compiler found. Then, you must go back to step 3, correct your errors, and start the process
A compiler has two main functions. First it checks the source program for syntax errors. Syntax errors are over. Besides fatal errors, the compiler also may advise you of what it thinks are mistakes. The compiler
errors in the grammar of the programming language. If there are any fatal syntax errors (that is, errors so usually calls these warnings. Warnings are not serious enough to stop the translation, but the compiler thinks
severe that the compiler does not understand the statement and cannot translate the statement into you should be made aware of them. If the compiler issues warnings, the compilation will take place.
machine language), the compiler will stop and notify you of the errors it found. If the compiler finds no fatal
errors, it translates each high-level language instruction into one or more machine language instructions. However, you should try to correct the source of these warnings before you execute the program unless you
This machine language version of the source program is the object program. coded the source of the warning on purpose. Statements that cause the compiler to issue warnings can cause
some very subtle errors in program output when you execute the program. In short, do not execute your
Although it is in machine language, the object program may not be ready to execute. The program may program until you have removed all syntax errors.
contain references, called external references, to other programs that a user may have written or that the

Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 14 of 68 Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 15 of 68
Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC)

A er a successful compile, there can be an error in the linking process. Usually, a linker error is caused by the
inability of the linker to resolve an external reference. The most common cause of this error is the misspelling
of the name of an external program. The linker cannot nd the misspelled program because it does not exist.
Whatever the cause of the linker error, you must nd and correct it.

Errors in Step 5: Assuming the compilation and linking are successful, you can execute your program. Two
types of errors can now occur. First, the program may end during execution because it cannot execute a
particular instruction. This is a run-time error. When a program abnormally ends, the computer displays a
message showing the type of error that occurred. You must now locate the cause of the error (which could
be in the data processed by your program, not in the program itself) and make the necessary changes. Many
compilers have techniques that you can use to locate run-time errors. These techniques include step-by-step
tracing of program instructions and the ability to stop the program at a predetermined place to inspect the
values of variables. Consult your compiler's user's guide or on-line help for instructions on how to locate run-
time errors.

Errors in Step 6: While testing your program with different sets of data, errors can occur that are difficult to
detect. In fact, they could go unnoticed. Although a program executes, produces output, and ends
successfully, it is not necessarily correct. Check your output to see if it is unreasonable or if it contains
obvious errors. If so, you must find the cause of the error. At this point in the program development cycle,
the error is probably a logic/logical error. The compiler checks the program for syntax errors, but it cannot
check for errors in logic. Faulty program design causes most logic errors. If your program contains a logic
error, you must find its cause (which is sometimes very difficult), go back to step 2, redesign, recode, reenter,
recompile, relink, and retest the program (and hope that you do not introduce new errors along the way).

Occasionally, faulty coding causes logic errors. Faulty coding is code that is syntactically correct but
incorrectly translates the program design into program code. If your program contains faulty coding, locate
and correct the faulty code, and then reenter, recompile, relink, and retest the program. As we work through
this book, we shall point out some common errors caused by faulty coding.

The general rule of program development is catching errors early. A mistake in step 2 might not show until
step 6. If this happens, you must go back to step 2 and start the cycle again. This wastes your time, computer
time, and money. Be cautious, and work slowly and deliberately. If you are careful at the beginning of
program development, your efforts will pay off in the end.

Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 16 of 68

You might also like