0% found this document useful (0 votes)
16 views

CO1003 - Chapter 1 - Introduction To Computers and Programming

FindSmallestNumber(array numbers[], int n) Input: numbers[] - array of n positive real numbers Task: Find the smallest number from the given array Output: minNumber - smallest positive real number 1. minNumber ← numbers[0] 2. For i ← 1 to n If numbers[i] < minNumber minNumber ← numbers[i] 3. Return minNumber This pseudo code finds the smallest number from an array of positive numbers using a for loop to iterate through the array, compare each element to the current minimum, and update minNumber if a smaller number is found. It initializes minNumber as the first element, iterates from
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

CO1003 - Chapter 1 - Introduction To Computers and Programming

FindSmallestNumber(array numbers[], int n) Input: numbers[] - array of n positive real numbers Task: Find the smallest number from the given array Output: minNumber - smallest positive real number 1. minNumber ← numbers[0] 2. For i ← 1 to n If numbers[i] < minNumber minNumber ← numbers[i] 3. Return minNumber This pseudo code finds the smallest number from an array of positive numbers using a for loop to iterate through the array, compare each element to the current minimum, and update minNumber if a smaller number is found. It initializes minNumber as the first element, iterates from
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Ho Chi Minh City University of Technology

Faculty of Computer Science and Engineering

Chapter 1: Introduction to
Computers and Programming

Introduction to Computer Programming


(C language)

Phạm Hoàng Anh


([email protected])

2020 – 2021, Semester 1


Course Content
p C.1. Introduction to Computers and
Programming
p C.2. C Program Structure and its
Components
p C.3. Variables and Basic Data Types
p C.4. Selection Statements
p C.5. Repetition Statements
p C.6. Functions
p C.7. Arrays
p C.8. Pointers
p C.9. File Processing 2
References

p [1] “C: How to Program”, 7th Ed. – Paul


Deitel and Harvey Deitel, Prentice Hall, 2012.
p [2] “The C Programming Language”, 2nd Ed.
– Brian W. Kernighan and Dennis M. Ritchie,
Prentice Hall, 1988
p and others, especially those on the Internet

3
Content

p Introduction

p Computer Organization
p Programming Languages
p Programming Tasks
p Data and Algorithms
p Summary

4
Introduction
p Computer Programming
n Computer
à a device that can perform computations and make
logical decisions billions of times faster than human
beings can

n Programming
à The act of writing the programs executable on the
computers to produce intended results

n Program
à A sequence of instructions written in a programming
language to perform a specified task by the computer
5
Introduction

Programs and
Computers Programming their Results 6
Computer Organization
p Hardware: physical components of
computer (including peripherals)
n the keyboard, screen, mouse, hard disk,
memory, DVDs and processing units, …
p Software: a set of machine-readable
instructions that directs a computer's
processor to perform specific operations
[Wikipedia]
n Application softwares
n Operating system
n System softwares 7
Computer Organization -
Hardware

Computer Architecture

ALU = Arithmetic/logic gate unit: performing


8
arithmetic and logic operations on data
Computer Organization –
Software

9
Programming Languages
p Programming language: a formal language
for writing a computer program as a
sequence of instructions
n C, C++, C#, Java, PHP, Python, …
p Three general types
n Machine languages
n Assembly languages
n High-level languages
à Providing a sequence of instructions that directly
understandable by computers or requiring some
intermediate translation steps 10
Programming Languages –
Machine Languages

p First-generation language: strings of


numbers (ultimately reduced to 1s and 0s)
that instruct computers to perform their
most elementary operations one at a time
n Directly understandable by computers
n Machine-dependent

For example, instructions for


adding overtime pay to base
pay and then storing the
result in gross pay
11
Programming Languages –
Assembly Languages
p Second-generation language: a low-level
language used to interface with computer
hardware using English-like abbreviations
to represent elementary operations
n Less understandable by computers
n Need for translation steps to convert an
assembly language program to machine codes
p Translator = Assembler

For example, instructions for


adding overtime pay to base
pay and then storing the
result in gross pay 12
Programming Languages –
High-level Languages
p Third-generation language: written
instructions that look almost like everyday
English and contain commonly used
mathematical notations
n Less understandable by computers
n Translator program is called compiler.
n The C language is a high-level language that needs
a compiler.
n Scripting languages such as PHP and Perl need an
interpreter.
For example, instructions for adding overtime pay to base pay and
then storing the result in gross pay: grosspay = basepay + overpay. 13
Programming Languages –
High-level Languages

Program File Compiler Binary File CPU Result

C, C++, Java, …

Program File Interpreter CPU Result

PHP, Perl, …

A history of computer programming languages – Wikipedia


Graph of programming language history – www.levenez
14
Programming Languages –
The C language

p Evolved from B by Dennis Ritchie at Bell


Laboratories and originally implemented on
a DEC PDP-11 computer in 1972
p Using many of the important concepts of
BCPL and B while adding data typing and
other powerful features
p Used for many important application trends
n Developing new major operating systems: UNIX,
Linux, Android, …
n Developing programs in the embedded systems
in cars, medical machines, …
15
Programming Languages –
The C language

Ken Thompson (left) with Dennis Ritchie (right,


the inventor of the C programming language)
[Wikipedia]

p The development of the C language


n Dennis M. Ritchie
p Full history of the C language
n Wikipedia
16
Programming Tasks
Design of Library Library
program (Header: *.h) (Object code: *.lib; *.dll; *.so)

Editor Preprocessor Compiler Linker Executable


Program

Source code Enhanced source code Object code


*.h + *.c *.h + *.c (*.cpp) *.obj
(*.cpp)

gcc; g++
Integrated Development Environment (IDE):
Visual Studio; Eclipse; Qt Creator; Code block; Online tool; etc
17
Programming Tasks
p Editor: supports text editing feature for
writing source code
p Preprocessor: preprocesses the source code
with replacing macro, inserting library files
*.h, …
p Compiler: translates the source code into
target machine language
p Linker: links the object code to other library
files
18
Data and Algorithms –
Concepts

p Program

= A Sequence of Instructions Written in a


Programming Language to Perform a Specified
Task by the Computer
= Data and their Structures + Algorithms
Input/Output/… Process

Example 1: instructions for adding overtime pay to base pay and


then storing the result in gross pay: grosspay = basepay + overpay.

Example 2: given n positive numbers, find the smallest one.


19
Data and Algorithms –
Data
p Atomic data: int, double, char, ...
p Non-atomic data: array, struct, enum, …
p A strong relationship between the data
structures and the operations on the data in
the corresponding structures
Example 1: instructions for adding overtime pay to base pay and
then storing the result in gross pay: grosspay = basepay + overpay.
- Input Data: basepay and overpay are positive real numbers
(double).
- Output Data: grosspay is also a positive real number (double).

Example 2: given n positive numbers, find the smallest one.


- Input Data: n positive real numbers are treated individually OR as
a collection (double)
- Output Data: minNumber is a positive real number (double). 20
Data and Algorithms –
Algorithms
p Algorithm = a sequence of unambiguous
instructions for solving a problem, i.e. for
obtaining a required output for any
legitimate input in a finite amount of time
n Anany Levitin, Introduction to the Design and
Analysis of Algorithms, 2nd Edition, Addison
Wesley, 2007
p Algorithm representation
n Pseudo code
n Flowchart
n Real code in a high-level programming language
21
Data and Algorithms –
Algorithms
p Example 2: given n positive numbers, find the
smallest one.
p Task solution:
n 1. Suppose that the first number is the smallest one
(current one).
n 2. Check if the current smallest one is a real one as
compared to the next number.
p If yes then compared to the next number of the next one
like step 2 till all numbers are checked.
p Otherwise,
§ update the smallest one with the smaller one
§ And then move next to check with the next number of the
next number like step 2 till all numbers are checked. 22
Data and Algorithms –
Algorithms – Pseudo Code
p Header
n Algorithm name Header

n Input data and their data types


n Task purpose
Body
n Pre-conditions
n Post-conditions
n Output data and their data types
p Body
n (Numbered) (control) statements
n Comments 23
Data and Algorithms –
Algorithms – Pseudo Code
Algorithm findMinNumber
p Example - Input: positiveNumber[n] which is an array of n positive double values
- Output: minNumber which is the smallest one whose type is double
2: given - Purpose: find the smallest number in a collection
- Precondition: n data inputs are positive.
n positive Begin Algorithm
Check positiveNumber[n] contains only positive values
numbers, minNumber = positiveNumber[1]
iteration = 2
find the While (iteration <= n)
Begin While
smallest If (minNumber <= positiveNumber[iteration]) Then
iteration = iteration + 1
one.
Else
Begin
minNumber = positiveNumber[iteration]
iteration = iteration + 1
End
End While 24
End Algorithm
Data and Algorithms –
Algorithms – Flowchart

p Symbols used for drawing a flowchart

25
Data and Algorithms –
Algorithms - Flowchart

p Terminal: starting point or end point


p Input/Output: input data/output data of the
algorithm
p Flow line: shows a control flow of the
algorithm. Execution follows this part.
p Decision: allows a condition (expressed as
a boolean expression) to be checked
p Process: data processing block

26
Data and Algorithms –
Algorithms - Flowchart

p Predefined process: an existing data


processing block
p On-page connector: a gathering point of
the flow lines in a flowchart
p Off-page connector: a gathering point of
the flow lines from another page
p Preparation: preparation steps, setting for
initial conditions
p Annotation: comments
27
Data and Algorithms –
Algorithms - Flowchart

false
<Condition>

true

<Statements>

if Statement
28
Data and Algorithms –
Algorithms - Flowchart

false
<Condition>

true

<Statements T> <Statements F>

if-else Statement 29
Data and Algorithms –
Algorithms - Flowchart

false false false


<case 1> <case 2> <case N>
true true true

<Statements 1> <Statements 2> <Statements N> <Default>

switch-case Statement

30
Data and Algorithms –
Algorithms - Flowchart

<initialization>

false
<Condition>

true
<Iteration Value
<Statements>
Modification>

for Statement
31
Data and Algorithms –
Algorithms - Flowchart

false <Statements>
<Condition>

true
true
<Condition>
<Statements>
false

while Statement do while Statement

32
Flowchart vs. Pseudo Code?
p Example 2: minNumber = How to add the checking
positiveNumber[1] of n positive numbers?
given n
positive iteration = 2

numbers,
false
iteration <= n
find the
smallest true

one. minNumber<= true


iteration =
positiveNumber[iteration]
iteration + 1

false
minNumber =
positiveNumber[iteration]

33
Data and Algorithms –
Algorithms – Real code in C
void main() {
double positiveNumber[10] = {2, 1, 3, 10, 8, 3, 4, 5, 9, 12};
int n = 10;
How to add the checking
double minNumber = positiveNumber[0]; of n positive numbers?
int iteration = 1;
while (iteration < n) {
if (minNumber <= positiveNumber[iteration]) iteration = iteration + 1;
else {
minNumber = positiveNumber[iteration];
iteration = iteration + 1;
}
}
} Pseudo Code vs. Flowchart vs. Real Code in C? 34
Summary
p Concepts related to computer programming

p Short introduction to computers, programs,


programming, and programming languages

p Short introduction to the C language


p Preparation for computer programming
n Programming tasks

n Data and basic data types

n Algorithms and their representations 35


Chapter 1: Introduction to
Computers and Programming

36

You might also like