LECTURE 01 : Week 01
An Overview of Programming
Languages and Problem Solving
C
S
C
-1
10
C
om
pu
tin
g
F
un
d
am
en
ta
ls
2
MOBILE ALERT
Kindly
Switch Silent/Off
your Mobile
Phones
C
S
C
-1
10
C
om
pu
tin
g
F
un
d
am
en
ta
ls
3
Marks Distribution
Quiz 10
Assignment 20
Mid term Exam 20
Final Exam 50
Attendance 75%
Examination Hall 5 min before
C
S
C
-1
10
C
om
pu
tin
g
F
un
d
am
en
ta
ls
4
Lecture material/ Announcement
C
S
C
-1
10
C
om
pu
tin
g
F
un
d
am
en
ta
ls
5
Programming Languages
Various programming languages enable
people to tell computers what to do
C
S
C
-1
10
C
om
pu
tin
g
F
un
d
am
en
ta
ls
6
The Language Translation Process
How are Programs Understood by the Computer?
Program written in programming
language
(source code)
Translator program
Assembler
Compiler
Interpreter
Program written in machine language (object code)
C
S
C
-1
10
C
om
pu
tin
g
F
un
d
am
en
ta
ls
7
Programming Languages
Machine Language
(first generation of programming languages)
The computer’s ‘native language’
Composed of binary digits (0s, 1s)
The only language that computers understand
Assembly Language
(second generation of programming languages)
One-to-one correspondence to machine language
Somewhat more user-friendly than machine language
(mnemonic rather than binary digits)
C
S
C
-1
10
C
om
pu
tin
g
F
un
d
am
en
ta
ls
8
Programming Languages
Procedural Languages (High Level Languages)
(third generation languages)
One instruction translates into many machine language
instructions
Programs describe the computer’s processing
step-by-step
Closer to natural language; uses common words rather
than abbreviated mnemonics
Examples:
C, Fortran, QuickBasic
Compiler - translates the entire program into machine
language
CSC-110 Computing Fundamentals 9
Assembly language
Can be written as follows:
LOAD rate MULT hour
STOR wages
Programming Languages
Represent the equation wages = rate · hours to calculate the weekly wages
Machine language
100100 stands for load
100110 stands for multiplication
100010 stands for store
Can be written as follows:
100100 010001 100110 010010 100010 010011
High level language
C
S
C
-1
10
C
om
pu
tin
g
F
un
d
am
en
ta
ls
10
Programming Languages
Nonprocedural Language
(fourth generation languages)
Allows the user to specify the desired result without
having to specify the detailed procedures needed for
achieving the result
Example:
data base query language - SQL
Can be used by non technical users
Natural Language Programming Languages
(fifth generation (intelligent) languages)
Translates natural languages into a structured,
machine-readable form
C
S
C
-1
10
C
om
pu
tin
g
F
un
d
am
en
ta
ls
11
C
S
C
-1
10
C
om
pu
tin
g
F
un
d
am
en
ta
ls
12
Current Programming Languages
Visual Programming Languages
Used within a graphical environment
Example: Visual Basic and Visual C++
Popular to non technical users
Hypertext Markup Language (HTML)
standard language used in World Wide Web
contains text, images, and other types of information such as data files, audio, video, and executable computer programs
Componentware
Software components that may be assembled by developer as needed
C
S
C
-1
10
C
om
pu
tin
g
F
un
d
am
en
ta
ls
13
Current Programming Languages
Extensible Markup Language (XML)
Improved on web document functionality
Object-Oriented Programming Languages (OOP)
based on objects – packaging data and the
instructions about what to do with that data
together
Examples: Java, C++
CSC-110 Computing Fundamentals 14 Processing A High Level Language Program
Steps to execute a program written in C++:
1. Use an editor to create a program. (source program)
2. Compiler translate the program in to an equivalent machine language. (object program)
3. The programs that you write in a high-level language are developed using a software development kit (SDK), which contains many programs that are useful in creating your program. This prewritten code resides in a place called the
library.
4. The next step is to load the executable program into the main memory for execution and a program called loader accomplishes this.
C
S
C
-1
10
C
om
pu
tin
g
F
un
d
am
en
ta
ls
15
Step in Program Development
Programming can be defined as the development of a solution to an identified problem, and the setting up of a related series of instructions that will produce the desired results.
Define the Problem
To help with initial analysis, the problem should be divided into three separate components:
the inputs
the outputs
the processing steps to produce the required outputs
Outline the Solution
This initial outline is usually a rough draft of the solution and may include:
The major processing steps involved
The major subtasks (if any)
The user interface (if any)
The major control structures (e.g. repetition loops)
The major variables and record structures
C
S
C
-1
10
C
om
pu
tin
g
F
un
d
am
en
ta
ls
16
Step in Program Development
Develop the Outline into an Algorithm
The solution outline developed in Step 2 is expanded into an algorithm: a set of precise steps that describe exactly the tasks to be performed and the order in which they are to be carried out.
Test the Algorithm for Correctness
Most importance in the development of a program
To identify major logic errors early, so that they may be easily corrected.
Code the Algorithm into a Specific Programming Language
Only after all design considerations have been met
Run the program on the computer
This step uses a program compiler and programmer-designed test data to machine test the code for syntax errors and logic errors.
Document and Maintain the program
It is an ongoing task from the initial definition of the problem to the final test result.
Documentation involves both external and internal documentation that may have been coded in the program.
C
S
C
-1
10
C
om
pu
tin
g
F
un
d
am
en
ta
ls
17
Programming With The Problem
Analysis – Coding – Execution Cycle
Problem solving process
First step
Define the problem.
Outline the solution.
Design an algorithm.
Test the algorithm for correctness.
Second step
Implement the algorithm in programming language, such as C++.
Run the program on the computer
Last step
C
S
C
-1
10
C
om
pu
tin
g
F
un
d
am
en
ta
ls
18
Procedural vs. Object-Oriented
Programming
Procedural programming
is based on a structured, top-down approach to writing effective programs.
concentrates on ‘what’ a program has to do and involves
identifying and organizing the ‘processes’ in the program solution
Decomposed into separate tasks or functions and includes
top-down development broken down problem into more detailed steps
modular design grouping task together because they all perform the same function; connected to top-down development
Object-Oriented Programming
OOP is also based on decomposing the problem.
CSC-110 Computing Fundamentals 19
Procedural vs. Object-Oriented Programming
Procedural programming
C
S
C
-1
10
C
om
pu
tin
g
F
un
d
am
en
ta
ls
20
Top-Down Development
If we look at a problem as a whole, it may seem
impossible to solve because it is so complex.
Examples:
writing a tax computation program
Writing a library system
Writing a Bank System
Complex problems can be solved using top-down design,
also known as stepwise refinement, where
We break the problem into parts
Then break the parts into smaller parts
C
S
C
-1
10
C
om
pu
tin
g
F
un
d
am
en
ta
ls
21
Advantages of Top-Down
Development
Breaking the problem into parts helps us to clarify what needs to be done.
At each step of refinement, the new parts become less complicated and, therefore, easier to figure out.
Parts of the solution may turn out to be reusable.
Breaking the problem into parts allows more than one person to work on the solution.
Problem:
C
S
C
-1
10
C
om
pu
tin
g
F
un
d
am
en
ta
ls
22
The Top Level
Get the customer list from a file.
Sort the list according to zip code.
Make a new file of only the customers with the zip code
43100 from the sorted customer list.
Print an envelope for each of these customers.
Main
C
S
C
-1
10
C
om
pu
tin
g
F
un
d
am
en
ta
ls
23
Another Level?
Should any of these steps be broken down further? Possibly!
How do I know? Ask yourself whether or not you could easily write the algorithm for the step. If not, break it down again.
When you are comfortable with the breakdown, write the pseudocode for each of the steps (modules) in the hierarchy.
Typically, each module will be coded as a separate function.
Another Example:
A Top-down analysis of a simple cooking task
Cook Breakfast
Bring out cook ware
Get Coffee Pot
Get
Frying pan Get fork
Get Serving dishes Take out
C
S
C
-1
10
C
om
pu
tin
g
F
un
d
am
en
ta
ls
24
What is an Algorithm?
A step-by-step problem solving procedure, especially an
establish, recursive computational procedure for solving a
problem in a finite number of steps.
There are many models supporting the development of
the code:
Pseudocode
Structure Diagram
and finally in C++
Flowcharts
An algorithm has to be clear
have a finite length stop in finite time
CSC-110 Computing Fundamentals 25
Input Processing Output
Names for our cells
5, 10 15
1) Declare variables input_1
input_2 sum
2) Assign values input_1 = 5
input_2 = 10
3) Process
sum = input_1 + input_2
The computer (and so C) provides basic arithmetic operations. If the operation
you want to use is not provided, you have to compose it.
CSC-110 Computing Fundamentals 26
PROGRAM Add Two Numbers READ two numbers ADD the numbers WRITE the sum END PROGRAM
Version 1:
PROGRAM Add Two Numbers READ First
READ Second
Sum = First + Second WRITE Sum
END PROGRAM
Version 2:
Pseudocode
Write a program calculating the sum of two numbers
CSC-110 Computing Fundamentals 27
Version 1: PROGRAM
Add Two Numbers
Version 2:
READ
Two Numbers
ADD
Two Numbers
WRITE The Sum
PROGRAM
Add Two Numbers
READ
Two Numbers
ADD
Two Numbers
WRITE The Sum
Sum = Input_1 +
Input_2
Take note:
We develop software iteratively (meaning version by version), but the code itself
is broken in top-down manner!!
Structure Diagram
Write a program calculating the sum of two numbersREAD Input_1
CSC-110 Computing Fundamentals 28
START
STOP READ First READ Second
Sum = First + Second
WRITE Sum
Flowcharts
Write a program calculating the sum of two numbers
Flowcharting symbols
Input/Output (used for all I/O operations)
Processing (used for all arithmetic and data transfer operations).
Terminal (used to indicate the beginning and end of a program or module).
Decision (used to test for a condition).
Connector (used to indicate the point at which a transfer of control operation occurs).
Predefined (used to indicate the name process of a module to be executed).
CSC-110 Computing Fundamentals 29
Pseudocode are more closely to the programming language
and for the advanced programmer.
Structure Diagrams are helpful to break the algorithm into more manageable pieces.
Flowcharts show the workflow of the algorithm and stress on structured programming.
Every model has its advantages and disadvantages. But all try to help you to structure your code in a top-down style and this is the way you should implement your algorithm.
Modular Design
Write a program calculating the sum of two numbersModular Design
We don’t break according to the sequence
in which the parts are following, but rather in its frequency of use and
CSC-110 Computing Fundamentals 30
/* Addition of 2 numbers */
#include <iostream> using namespace std;
int main() {
int first, second, sum;
cin>>first; cin>>second;
sum = first + second; cout<<sum<<endl;
return 0; }
/* Addition of 2 numbers */
#include <iostream> using namespace std;
int main() {
int first, second, sum;
cin>>first; cin>>second;
sum = first + second; cout<<sum<<endl;
return 0; }
Where do you find now in the code the steps we have developed?
1. Declaration of the structure
2. Reading the input
3. Processing the data
4. Writing the output
C
S
C
-1
10
C
om
pu
tin
g
F
un
d
am
en
ta
ls
31
Programming Guideline
C
S
C
-1
10
C
om
pu
tin
g
F
un
d
am
en
ta
ls
32
Programming Guideline
Rule 1: Think about your strategy twice! Choose a strategy possibly coming out with less program lines.
Rule 2: You cannot program, what you cannot do with paper and pencil.
It is worth to rethink your strategy. - Do I really have chosen the best? - Can I reformulate my problem to make it more simple?
Even after having an algorithm ready in mind.
Play with your data. Play to be the computer. You are only allowed to perform actions a computer can. Can you solve your problem this way?
C
S
C
-1
10
C
om
pu
tin
g
F
un
d
am
en
ta
ls
33
Programming Guideline
Rule 3: The compiler must be easy and in short time accessible.
Rule 4: (Data) Structure before functionality!
First declare all the variables you will need. (Later you will use more advanced structures like
objects, trees, graphs,..)
But in all cases: The data structure you need has to be ready first.
You can still use old DOS-shells, if you want. But open at least two windows, one for editing, one for compiling the
code. Prepare your programming
C S C -1 10 C om pu tin g F un d am en ta ls 34
Programming Guideline
Rule 5: Name your variables appropriate and format your code in a way, that the structure is easily detectable.
Rule 6: Write code using a divide and conquer approach. (Top-down
)
while (counter < 100) {
if (no > 1) {
... }
}
while (counter < 100){ if (no > 1)
{ ... } }
Cycle Test Express PROGRAM Add Two NumbersC S C -1 10 C om pu tin g F un d am en ta ls 35
Programming Guideline
Rule 7: Compile every three lines. Write code in such a way, that it compiles and executes at every moment of the code development!
Rule 8: Do your best in writing the documentation, learn from your code. Make sure you do not do the same mistakes again.
Cycle Test
Express
Whatever we do, our code always compiles and executes!!
Compile very often! Detect a mistake as soon as possible!
Comment clearly
/* Addition of 2 numbers */
#include <iostream> using namespace std;
int main() {
/* The input numbers */ int first, second;
/* The output number */ int sum;
....
/* Addition of 2 numbers */
#include <iostream> using namespace std;
int main() {
/* The input numbers */
int first, second;
/* The output number */
int sum;