Learning Objectives: Csc128 - Fundamentals of Computer Problem Solving Chapter 1: Introduction
Learning Objectives: Csc128 - Fundamentals of Computer Problem Solving Chapter 1: Introduction
– FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 1: Introduction
Learning Objectives
At the end of this chapter, student should be able to:
9 Describe what a computer program is
9 Explain the importance of programming to computer use
9 Appreciate the importance of good programs
9 Explain the relationship between compilers, interpreters and programs
9 Recognize program errors
9 Become familiar with the program design process
________________________________________________________________________________________________________________
References: 1
Rosenah A. H., (2009) Introduction to Programming with C++, for Scientists and Engineers, UPENA UiTM Shah Alam
Norizan M., Mazidah, P., (2006), Problem Solving with C++, UPENA UiTM Shah Alam
CSC128 – FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 1: Introduction
A Brief History of C++ Language
• C++ (OOP) is built upon the foundation of C (structured programming language).
• C++ is a superset of C and C++ compilers can be used to compile C programs.
• C++ is an expanded and enhanced version of C that embodies the philosophy of object‐oriented
programming.
• C was invented and first implemented by Dennis Ritchie in 1970s, while C++ invented by Bjarne
Stroustrup in 1979 at Bell Laboratories in Murray Hill.
• The reason for invention of C++ is to cater the increases of complexity in computer problem
solving or programming.
Introduction to Programming
A. What is a computer program
1. Program
A program is a set of instructions that direct the computer to accomplish specific tasks. Another
term commonly used for computer programs is software, which describes all programs that are used
in particular installation together with the associated documentation.
2. Programming languages and its types.
Programming is also known as software engineering. It is the process of writing a program, or
software. The instructions that can be used to construct a program consist of statements written in
a programming language, the language that the computer can interpret or “understand” such as
C++ or Visual BASIC. First introduced in 1945, programming languages have evolved over the years.
The births of the generations are as follow.
• First generation, 1945 ‐ machine language
• Second generation, mid‐1950s – assembly language
• Third generation, early 1960s – high‐level languages: FORTRAN, COBOL, C, BASIC, C++, and
Pascal
________________________________________________________________________________________________________________
References: 2
Rosenah A. H., (2009) Introduction to Programming with C++, for Scientists and Engineers, UPENA UiTM Shah Alam
Norizan M., Mazidah, P., (2006), Problem Solving with C++, UPENA UiTM Shah Alam
CSC128 – FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 1: Introduction
a. First Generation: Machine Language (Low‐Level Language)
• The only programs that can be used to operate a computer.
• The lowest level of computer programming language.
• Machine language or also referred to as executable programs, or executables for short,
consist of a sequence of instructions composed of binary numbers (0’s and 1’s) such as:
10000000 00000001 00000010
11110000 00000010 00000011
which correspond to on and off of electrical states of the computer, which not
convenient for people to read and use.
• Each instruction in a machine language consists of two parts: instruction part and
address part. The instruction part is referred to as opcode (operation code), is usually
the leftmost set of bits in the instruction. It tells the computer operation to be
performed, such as add, subtract, multiply, etc., while the rightmost bits specify the
memory addresses of the data to be used.
• For example, assuming that the eight leftmost bits of the first instruction (i.e.
100000000) listed above is the opcode for add, and the next two groups of twelve bits
are the addresses of two operands to be added. This instruction would be a command
to “add the data in memory location 1 to the data in memory location 2.”
• Similarly, assuming that the opcode 11110000 means multiply, the next instruction is a
command to “multiply the data in memory location 3 by the data in location 4.
b. Second Generation: Assembly Language (Low‐Level Language)
• One of the first advances in programming was the substitution of word‐like symbols,
such as ADD, MUL, for the binary opcodes and both decimal numbers and label for
memory addresses.
• For example, the previous two machine language instruction can now be written as
ADD 1, 2
MUL 2, 3
• The particular programming language is called assembly language. Assembly language is
a low‐level language that allows a programmer to use abbreviations or easily
remembered words instead of numbers.
• Computer can only execute machine language programs, the set of instructions
contained within an assembly language program must be translated into machine
________________________________________________________________________________________________________________
References: 3
Rosenah A. H., (2009) Introduction to Programming with C++, for Scientists and Engineers, UPENA UiTM Shah Alam
Norizan M., Mazidah, P., (2006), Problem Solving with C++, UPENA UiTM Shah Alam
CSC128 – FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 1: Introduction
language. Translator programs that perform this function for assembly language
programs are known as assemblers.
Figure 1: Hierarchy of Computer Organization
3. Programming, programmers and users
The process of writing or coding programs s called programming, and the individuals who write
programs are called programmers. Users, on the other hand, are individuals who use the program.
________________________________________________________________________________________________________________
References: 4
Rosenah A. H., (2009) Introduction to Programming with C++, for Scientists and Engineers, UPENA UiTM Shah Alam
Norizan M., Mazidah, P., (2006), Problem Solving with C++, UPENA UiTM Shah Alam
CSC128 – FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 1: Introduction
B. Importance of computer programming
• A computer without a program is useless. A computer must have a program to accept input,
process the input and produce output.
• Before a program is written, a programmer analyzes the requirement and designs a correct
algorithm so that the program will produce the desired output. Therefore, it is very important
that programmers have a good understanding of computer, problem‐solving approaches and
compositions of the chosen programming languages.
C. Importance of good programs
In order for a program to be considered as a good program, it must have the following:
1. Reliability of output
A good program must be able to produce correct output. For that, a different set of input data is
used to ensure the reliability of the output.
2. Program’s efficiency
A good program must be designed to be efficient and reliable in the sense that it produces no
errors during execution process. Also, the program must achieve its purpose so that the final
result can be trusted. Thus, it is important that the program is outlined first using the
pseudocode or flowchart tool.
3. Interactivity
The interaction process between the user and the program must be well defined. The
interactivity is important so that the user knows the processing status. Program that are user‐
friendly allow the users to responds to instruction correctly and this will help the users to key in
input thus minimizing the errors resulted from invalid data.
________________________________________________________________________________________________________________
References: 5
Rosenah A. H., (2009) Introduction to Programming with C++, for Scientists and Engineers, UPENA UiTM Shah Alam
Norizan M., Mazidah, P., (2006), Problem Solving with C++, UPENA UiTM Shah Alam
CSC128 – FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 1: Introduction
Example of a bad program’s output screen
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.
| Blinking cursor. User does
not know what data to be
entered here.
Example of a good program’s output screen
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.
**This program adds two integers**
The instruction clearly tells
Please enter 2 integers: | user what to do.
4. Program readability
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.
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.
________________________________________________________________________________________________________________
References: 6
Rosenah A. H., (2009) Introduction to Programming with C++, for Scientists and Engineers, UPENA UiTM Shah Alam
Norizan M., Mazidah, P., (2006), Problem Solving with C++, UPENA UiTM Shah Alam
CSC128 – FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 1: Introduction
Table1: Example of indentation using if‐else, for and while statement.
Before indentation: After indentation:
if (score > 90) if (score > 90)
{ highest = highest + 1; { highest = highest + 1;
cout << “Highest scores!!”; } cout << “Highest scores!!”;
else if ( score < 40 ) }
{ lowest = lowest + 1; else if ( score < 40 )
cout << “Lowest scores!!”;} { lowest = lowest + 1;
for ( int bil = 1; bil < 5; bil++) cout << “Lowest scores!!”;
{ sumAllNum = sumAllNum + num; }
cout << “\n Enter num : ”;
cin >> num; } for ( int bil = 1; bil < 5; bil++)
while (y >= 2) { sumAllNum = sumAllNum + num;
{ x = x / 2; cout << “\n Enter num : ”;
If (x > 0 ) cin >> num;
y = 1 + y; }
else
y = x + y; while (y >= 2)
i++;} { x = x / 2;
if (x > 0 )
y = 1 + y;
else
y = x + y;
i++;
}
b. Comments
• Some explanatory notes or comments (sometimes referred to as internal
documentation) should be place in 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.
________________________________________________________________________________________________________________
References: 7
Rosenah A. H., (2009) Introduction to Programming with C++, for Scientists and Engineers, UPENA UiTM Shah Alam
Norizan M., Mazidah, P., (2006), Problem Solving with C++, UPENA UiTM Shah Alam
CSC128 – FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 1: Introduction
• C++ supports two types of comments:
i. Line comment
A line comment begins with two slashes ( // ) and continues to the end of line.
Example:
int main()
{ //declare variables
float perimeter = 0.0;
float length = 10.5;
float width = 8.6;
//calculate perimeter
Perimeter = (length + width) * 2;
return 0; //indicates program ended normally
ii. 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.
Example:
/* This program input user’s name
displays the name
and prints the greeting */
________________________________________________________________________________________________________________
References: 8
Rosenah A. H., (2009) Introduction to Programming with C++, for Scientists and Engineers, UPENA UiTM Shah Alam
Norizan M., Mazidah, P., (2006), Problem Solving with C++, UPENA UiTM Shah Alam
CSC128 – FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 1: Introduction
D. Relationship between compilers, interpreters, assemblers and programs
1. What are the compiler and interpreter?
• Compiler and interpreter is a language translator program that translates source code (human‐
readable code) into machine language.
Language Translator
Program
Compiler Interpreter
• Translate all of the instructions of high‐level • Converts high‐level language instruction
into machine language one at a time, in
language as a complete unit. succession.
• The original program that is written in high‐
level language is called source code. • Any of the instructions that contain error,
o Source code ‐ the original program interpreters provides immediate
written in assembly language or feedback.
high‐level languages. The file • Does not produce object code.
containing the source code is
called source file, and in C++, it is
uses the filename extension .cpp
(cpp stands for C plus plus)
• While attempting to translate each of the
statement, the compiler also detects error
message after translation is completed.
• Otherwise, the compiler successfully
translates the source code into machine
language, which in this case object code.
o Object code – created from the
compilation process in machine
readable form. The file containing
the object code is called the object
file, and it uses the file name
extension .obj (obj stands for
object)
• After compiler creates the object file, it
then invokes another program called a
linker. The linker combines the object file
with other machine code necessary for the
C++ program to run correctly. The linker
files then produces an executable file that
has an extension of .exe (exe stands for
executable).
________________________________________________________________________________________________________________
References: 9
Rosenah A. H., (2009) Introduction to Programming with C++, for Scientists and Engineers, UPENA UiTM Shah Alam
Norizan M., Mazidah, P., (2006), Problem Solving with C++, UPENA UiTM Shah Alam
CSC128 – FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 1: Introduction
2. Steps in running a program using compiler
Figure 2: Steps in running a program using compiler
3. Steps in running a program using interpreter
Figure 3: Steps in running a program using interpreter
4. Program errors
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.
There are different types of errors:
a. Run‐time errors
b. Syntax errors
c. Logic errors
________________________________________________________________________________________________________________
References: 10
Rosenah A. H., (2009) Introduction to Programming with C++, for Scientists and Engineers, UPENA UiTM Shah Alam
Norizan M., Mazidah, P., (2006), Problem Solving with C++, UPENA UiTM Shah Alam
CSC128 – FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 1: Introduction
a. Run‐time errors
Run‐time errors occur during the execution of a program and are detected by the compiler.
Example:
int a = 0 , b = 7;
cout << “The result is “ << b / a << endl;
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.
b. Syntax errors
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.
Example:
1 cout << “\n Ther are 5 syntax errors here!”
2 cot >> “ Correct them;
Explanation:
i. Invalid use of backslash (/) in line 1
ii. Missing semicolon (;) in line 1
iii. Keyword cout is misspelled in line 2
iv. Invalid use of insertion symbol (>>) in line 2
v. Missing a closing quote (“) in line 2
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.
________________________________________________________________________________________________________________
References: 11
Rosenah A. H., (2009) Introduction to Programming with C++, for Scientists and Engineers, UPENA UiTM Shah Alam
Norizan M., Mazidah, P., (2006), Problem Solving with C++, UPENA UiTM Shah Alam
CSC128 – FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 1: Introduction
c. Logic errors
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 the error.
The only way to detect it is by testing the program thoroughly and comparing its output
against manually calculated results.
Some indications of logic errors include:
i. No numerical output
This is caused by a missing numerical output in a cout statement.
Example:
cout << “Enter quantity of items purchased: ”;
cin >> qty;
cout << “\n Enter the price unit: RM ”;
cin >> price;
cout << “\n The total price is RM ”; // error here
cout << “\n Thank you.”;
The output for the above program statement is
Enter quantity of items purchased: 5
Enter the price unit: RM 50;
The total price is
Thank you.
ii. Unappealing or misaligned output
This either caused by an error in cout statement or the use of formatting output
statement (\n, \t).
Example:
cout << “Enter quantity:\t\t”;
cin >> qty;
cout << “Enter price: (RM) ”;
cin >> price;
cout << “The total price is RM ” << qty * price;
cout << “Thank you.”;
________________________________________________________________________________________________________________
References: 12
Rosenah A. H., (2009) Introduction to Programming with C++, for Scientists and Engineers, UPENA UiTM Shah Alam
Norizan M., Mazidah, P., (2006), Problem Solving with C++, UPENA UiTM Shah Alam
CSC128 – FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 1: Introduction
Sample output
Enter quantity: 5
Enter price : (RM) 10
The total price is RM 50Thank you
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:
void main()
{
char fnum = 10; // error here
int snum = 5;
int sum = fnum + snum;
.
.
.
Where fnum is of type character. The valid character to be assigned is
char fnum = ‘A’;
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:
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);
Explanation:
The logic errors are:
i. Assigning invalid values to variable of type integer in line 1
ii. Attempting to divide by zero in line 3
________________________________________________________________________________________________________________
References: 13
Rosenah A. H., (2009) Introduction to Programming with C++, for Scientists and Engineers, UPENA UiTM Shah Alam
Norizan M., Mazidah, P., (2006), Problem Solving with C++, UPENA UiTM Shah Alam
CSC128 – FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 1: Introduction
iii. Missing numerical output in line 4
iv. Taking the square root of a negative number in line 5
E. Program Development Life Cycle
• In designing a program, there is no complete set of rules or specific algorithm to follow.
However, software developers try to use a reasonably consistent problem solving approach for
constructing computer programs.
• The phases in the problem solving approach are outlined as follows:
Phase 1: Problem Definition (Analysis)
Phase 2: Algorithm design
Phase 3: Algorithm implementation
Phase 4: Program testing
Phase 5: Program maintenance
• The phases can be further divided into two:
a. Problem solving phases: consist of problem definition and algorithm design
b. Implementation phases: consist of algorithm implementation, program testing and
program maintenance phases.
________________________________________________________________________________________________________________
References: 14
Rosenah A. H., (2009) Introduction to Programming with C++, for Scientists and Engineers, UPENA UiTM Shah Alam
Norizan M., Mazidah, P., (2006), Problem Solving with C++, UPENA UiTM Shah Alam
CSC128 – FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 1: Introduction
1. Problem definition (Analysis)
Problem definition is also called the analysis phase. The problem is defined to obtain a clear
understanding of the problem requirement.
The following questions should be asked to get a complete problem specification:
a. What are the input data?
b. What are the output (desired) data?
c. What formula is to be used?
d. What other assumptions or constraints can be made?
e. What is the expected output screen?
2. Algorithm design
The specifications derived earlier in the analysis phase are translated into the algorithm. An
algorithm is a step‐by‐step sequence of precise instructions that must terminate and
describes how the data is to be processed to produce the desired outputs. The instruction
may be expressed in a human language.
An algorithm must satisfy some requirements:
a. Input and output
It must have zero or more input and must produce at least one output.
b. Unambiguous
Every step in algorithm must be clear as to what it is supposed to do and how many
times it is expected to be executed.
c. Correct and efficient
It must be correct and efficiently solve the problem for it is designed.
d. Finite
It must execute its instruction and terminate in a finite time.
An algorithm can be written or described using several tools:
a. Pseudocode
Use English‐like phrases to describe the processing process. It is not standardized since
every programmer has his or her own way of planning the algorithm.
________________________________________________________________________________________________________________
References: 15
Rosenah A. H., (2009) Introduction to Programming with C++, for Scientists and Engineers, UPENA UiTM Shah Alam
Norizan M., Mazidah, P., (2006), Problem Solving with C++, UPENA UiTM Shah Alam
CSC128 – FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 1: Introduction
Table 2: Pseudocode style
Style 1 Style 2 (Modular design)
Problem Problem
1. Start 1. Start
2. Subproblem 1 2. Subproblem 1
Task 1,1 3. Subproblem 2
Action 1,1,1 4. End
Action 1,1,2
3. Subproblem 2 2. Subproblem 1
Task 1,2 Task 1,1
Action 1,2,1 Action 1,1,1
Action 1,2,2 Action
4. End 1,1,2
3. Subproblem 2
Task 1,2
Action
1,2,1
Action
1,2,2
b. Flowchart
Use standardized symbol to show the steps the computer needs to take to accomplish
the program’s objective. Because flowcharts are cumbersome to revise, they have fallen
out of favour by professional programmers. Pseudocode, on the other hand, has gained
increasing acceptance.
Table 3: Flowchart symbols
Name Symbol Description
Lozenge Terminal (start / stop)
Parallelogram Data (input / output)
Rectangle Process
Diamond Decision / Selection
Rectangle Module (Pre‐defined process)
Arrow Flow line (flowchart)
________________________________________________________________________________________________________________
References: 16
Rosenah A. H., (2009) Introduction to Programming with C++, for Scientists and Engineers, UPENA UiTM Shah Alam
Norizan M., Mazidah, P., (2006), Problem Solving with C++, UPENA UiTM Shah Alam
CSC128 – FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 1: Introduction
Most computer algorithms consist of at least the following processes.
i. Get the input (data)
ii. Perform the computation (processing)
iii. Display the output (results)
3. Algorithm implementation
The algorithm is translated into a computer program by using a specific programming
language, for example C++. The process called coding, which involves editing, compiling and
debugging.
4. Program testing
Program testing requires testing the completed program to verify that it produces expected
output. A different set of testing data is normally used to verify that the program works
properly and that it is indeed solving the given problem.
5. Program maintenance
Often, there may be new requirements to be added into the current program. Making
revisions to meet the changing needs with ongoing correction of problem are the major
efforts in the program maintenance. As a result, the program codes may be modified, added
or deleted accordingly. Thus, it is very important that a program is well documented for
future development.
________________________________________________________________________________________________________________
References: 17
Rosenah A. H., (2009) Introduction to Programming with C++, for Scientists and Engineers, UPENA UiTM Shah Alam
Norizan M., Mazidah, P., (2006), Problem Solving with C++, UPENA UiTM Shah Alam
CSC128 – FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 1: Introduction
F. Example of problems that can be solved using computers
Problem 1: Calculate the sum and average of 2 integer numbers.
Step 1: Problem definition (analysis)
Input Processing Output
Input variable: Processing items: Output variable:
number_1, sum, average sum
number_2 average
Formula:
Constant: sum = number_1 + number_2
None average = sum / 2
Step / Solution algorithm:
Get input
Calculate sum
Calculate average
Display output
Step 2: Algorithm design (Pseudocode or flowchart)
a. Pseudocode
Simple desing Modular design
Calculate the sum and average Calculate the sum and average
1. Start 1. Start
2. Get input 2. get_Input ()
Read number_1 3. calculate_Sum ()
Read number_2 4. calculate_Average ()
3. Calculate sum 5. display_Output ()
sum = number_1 + number_2 6.End
4. Calculate average
average = sum / 2 2. get_Input ()
5. Display output Read number_1
Print sum Read number_2
Print average
3. calculate_Sum ()
sum = number_1 + number_2
4. calculate_Average ()
average = sum / 2
5. display_Output ()
Print sum
Print average
________________________________________________________________________________________________________________
References: 18
Rosenah A. H., (2009) Introduction to Programming with C++, for Scientists and Engineers, UPENA UiTM Shah Alam
Norizan M., Mazidah, P., (2006), Problem Solving with C++, UPENA UiTM Shah Alam
CSC128 – FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 1: Introduction
b. Flowchart
Simple design
Start
Read number_1, number_2
sum = number_1 + number_2
average = number_1 + number_2
Print sum, average
Stop
________________________________________________________________________________________________________________
References: 19
Rosenah A. H., (2009) Introduction to Programming with C++, for Scientists and Engineers, UPENA UiTM Shah Alam
Norizan M., Mazidah, P., (2006), Problem Solving with C++, UPENA UiTM Shah Alam
CSC128 – FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 1: Introduction
Modular design Sub‐module
Start get_Input
get_Input ()
Read Read
number_1 number_2
calculate_Sum ()
Return
calculate_Average ()
calculate_Sum ()
display_Output () sum = number_1 + number_2
Stop Return
calculate_Average ()
average = sum / 2
Return
display_Output ()
Print sum Print average
Return
________________________________________________________________________________________________________________
References: 20
Rosenah A. H., (2009) Introduction to Programming with C++, for Scientists and Engineers, UPENA UiTM Shah Alam
Norizan M., Mazidah, P., (2006), Problem Solving with C++, UPENA UiTM Shah Alam
CSC128 – FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 1: Introduction
Step 3: Algorithm Implementation (Coding)
//Implementation of Simple design
#include <iostream.h> //to handle output (cout) and input (cin)
#include <conio.h> //to handle screen and getch()
void main()
{ //declare input variable
int number_1, number_2;
//declare output variable
int sum, average;
cout << “Enter first number ”; //prompt user to enter first number
cin >> number_1; //read value for number_1
cout << “Enter second number ”; //prompt user to enter second number
cin >> number_2; //read value for number_2
//calculate sum
sum = number_1 + number_2;
//calculate average
average = sum / 2;
//Display output (sum and average)
cout << “The sum of ” << number_1 << “ and ” << number_2 << “ is “ << sum;
cout << “The average of ” << number_1 << “ and ” << number_2 << “ is “ << average;
getch(); //this is predefined function to get the current character and retain the
//screen while executing the program
} //end of main()
________________________________________________________________________________________________________________
References: 21
Rosenah A. H., (2009) Introduction to Programming with C++, for Scientists and Engineers, UPENA UiTM Shah Alam
Norizan M., Mazidah, P., (2006), Problem Solving with C++, UPENA UiTM Shah Alam
CSC128 – FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 1: Introduction
//implementation of modular design
//in C++, module also known as function
#include <iostream.h> //to handle output (cout) and input (cin)
#include <conio.h> //to handle screen and getch()
//declare input variable and its' datatype
int number_1, number_2;
//declare processing item and output variable and its's datatype
int sum, average;
//declare module or function
void get_Input();
void calculate_Sum();
void calculate_Average();
void display_Output();
void main()
{ get_Input(); //calling get_Input() module
calculate_Sum(); //calling calculate_Sum() module
calculate_Average(); //calling calculate_Average() module
display_Output(); //calling display_Output () module
getch();
}
void get_Input()
{ cout << "Enter first number ";
cin >> number_1;
cout << "Enter second number ";
cin >> number_2;
}
void calculate_Sum()
{ sum = number_1 + number_2;
}
void calculate_Average()
{ average = sum /2;
}
void display_Output()
{ cout << "The sum of " << number_1 << " and " << number_2 << " is " << sum;
cout << "\nThe average of " << number_1 << " and " << number_2 << " is " << average;
}
________________________________________________________________________________________________________________
References: 22
Rosenah A. H., (2009) Introduction to Programming with C++, for Scientists and Engineers, UPENA UiTM Shah Alam
Norizan M., Mazidah, P., (2006), Problem Solving with C++, UPENA UiTM Shah Alam