0% found this document useful (0 votes)
121 views60 pages

CPE 112-Chapter1 - Updated2021

This document provides an overview and syllabus for a Computer Programming course. The main topics covered include: - An introduction to computer programming and algorithms - Using C/C++ as the programming language to learn programming constructs and solve problems - Evaluation includes quizzes, a midterm, and a final exam - What a computer program is and how programs work - Common types of programming errors - Recommended integrated development environments (IDEs) for writing C/C++ programs on different operating systems - File extensions for C/C++ programs - What an algorithm is

Uploaded by

xatabi6022
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)
121 views60 pages

CPE 112-Chapter1 - Updated2021

This document provides an overview and syllabus for a Computer Programming course. The main topics covered include: - An introduction to computer programming and algorithms - Using C/C++ as the programming language to learn programming constructs and solve problems - Evaluation includes quizzes, a midterm, and a final exam - What a computer program is and how programs work - Common types of programming errors - Recommended integrated development environments (IDEs) for writing C/C++ programs on different operating systems - File extensions for C/C++ programs - What an algorithm is

Uploaded by

xatabi6022
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/ 60

CMPE 112

CMPE112,CTPR112,ITEC112,MISY112,IT112,COMP112,CPE112

Computer Programming

Chapter 1 – An Introduction to Computer Programming and


Computer Algorithms.
What is this course?
Why am I taking this course?
 This course introduces fundamentals of programming
constructs through using the C/C++ programming language.

 The main focus of this course is to learn the essential


programming constructs and methods in order to generate
algorithmic solutions to business and mathematical problems.

 Upon completion of the course, students will be able to use


programming constructs to solve business level and
engineering problems.

 The whole idea behind this course is to improve your


computational thinking skills rather than hardcore
programming.
Course Evaluation
 This course includes 2 Quizzes, 1 Midterm and 1 Final
exam.
 Grading Policy:
 Quiz 1(15%)
 Midterm ( 30% )
 Quiz 2 (15%)
 Final (40 % )
What is a Computer program?
 A computer program is a sequence of instructions
specifically written to tell a device to perform a specified or
a series of task(s).

 Any computer system requires programs to function,


typically executing the program's instructions in a central
processor.

 Computer programs are often referred as software where


as computer programs on digital devices (such as on
smartphones) are often referred as applications (also
called apps).

 Software may be ranked along functional lines:


system software and application software
System Software VS
Application Software
 System Software
 Software that relies only on hardware to run. This
type of software does not require any other
software to function and can run on the top of
hardware directly.

 Application Software
 Software that relies only on other software to run.
This type of software usually needs a System
Software to run.
 The C/C++ programs you are going to write within this
course are all in application Software.
Programming
 Programming is the task of writing high or medium level
codes in order to tell the computer what to do under
certain circumstances.

 Computer programing is a skill, it is not a body of


knowledge. Thus, programming is best learned by
practicing similar to that of driving a car or swimming.
How does a program work?
Compiler Assembler
Source Object Executable
Code code Code

 Source Code is a computer program written in human


understandable form.

 Object Code is a computer program that is in machine


understandable form.

 Compiler is the specific software that gets the whole


source code and translates it into object code.

 Assembler is the specific software that gets the whole


object code and translates it into executable code.
Some Stereotype students in
computer programming class
 Attends to class once a week for 3 hours and expects to understand everything.

 Thinks computer programming is difficult, so procrastinates all the time.

 Tell him/her – self “I will do it later”

 will not take notes and will just listen to the teacher.

 will not write programs on the computer, will just note programs on the
notebook.

 Will constantly try to take pictures of the white board and even slides

 Copies programs from the board and when it doesn’t run, calls to the teacher:
“the program doesn’t work, can you fix it?”

BASED ON EXPERIENCE, THESE STUDENTS USUALLY FAIL OR


GET A BARE PASS FROM THE COURSE
Is programming difficult?
 Learning programming is hard.

 Studies show that people’s attitude has a great effect in


learning programming.

 This course requires a lot of practice at home, so do not


expect to grasp everything in the class with 3 hours. You
will need to do individual learning at home/library in order
to practice programming.
Programming errors
 Majority of programmers spend 25% of their programming
time to code and they spend the rest 75% for debugging
errors.
Making mistakes and
encountering errors
 More often than not, you will encounter errors and make
mistakes while you write computer programs.

 In such cases, calling your teacher and telling him/her “this


program does not work, it gives me errors” is a terrible and an
unprofessional approach.

 When you encounter errors, read the compiler message


instead. What does it tell you? Double click the compiler
message given and try to identify where the error exists.

 Please do not let errors discourage you. IT IS OK TO MAKE


MISTAKES! Remember, if you never make mistakes, you
actually never learn.
Types of errors you will encounter

 Syntax Errors are generated in the case of violation of a


syntax rules in a programming language.
Effect  compiler finds it in Syntax Check Phase.

 Semantics Errors are generated in the case of doing


logical mistakes in source code.
Effect  compilers cannot notice them, but during the
execution, they causes unexpected results.

 Run-Time Errors occurs during the program execution.


Effect  it occurs on run time and may crash the
program execution.
Which programming language
are we going to learn?

 In this course, you will learn C programming style through C++


Syntax. There are many reasons why we select C/C++ as an
introductory programming language which includes but not
limited to a)how fast the language is; b) platform
independency; c) the difficulty and the global use of the
language.

 The programs you are going to write are essentially going to be


C++ programs. However, you are not going to use C++ specific
features such as header files, classes or other features.

 In other words, you will not do object oriented programming


but instead learn structured programming.
What does it mean to
code in C style in C++?
 It might sound peculiar but what you are going to do is to use
C++ statements in order to write C programs.

 C++ is an extension and upgraded version of C. A lot of C


statements such as printf and scanf are quite old fashioned
and difficult nowadays.

 Instead of the old fashioned C statements that require


different data types to execute, we will use more generic and
easy statements of C++.

 As an example, we will use cout<< instead of printf and cin>>


instead of scanf in this course. Many of the libraries we are
going to use will be C++ libraries but would work in a similar
pattern as a C library.
Which software are we
going to use to write
C/C++ programs?
 The software that is used to write C/C++ programs are
called Integrated Development Environments simply
referred as IDE.

 As C/C++ is a platform independent programming


language, you will need to select an IDE according to the
operating system you use.
Windows Users
 Dev C++ is a development environment that is free and
available to programmers who want to write C/C++
programs in Windows Environment.

 Dev C++ is completely free – which means it is a


freeware. You can simply Google and download it on
your computer.

 Please be aware that


this software runs only on Windows.
Mac Users
 Mac Users can use Xcode to write their C/C++ programs.

 XCode is a free software that support wide range of


programming languages. It is free and available on Apple
App store so you can directly search and download it on
your Mac.

 Xcode does not run on Windows Operating System.


Linux Users
 CodeBlocks is a reliable IDE that works on Linux platform. While
there are wide range of IDEs available on Linux platforms for
writing C/C++ programs, CodeBlocks is probably one of the top
choices you can select from.

 Similar to DevC++ and Xcode, CodeBlocks is free.

 CodeBlocks is also available for Windows and Mac, however


particularly the Mac Version of it is not reliable.
Can I use other IDEs?
 Yes, of course you can!

 In our lecture and lab sessions, we will mostly stick


on using Dev C++ as we work on a Windows
Environment and this IDE is already available on
the computers.

 However, at your home you are not obligated to


use this software. You can use any IDE that
supports C/C++ Compiler and the codes we are
going to write in the class would work on any
platform and any IDE.
Extensions of C/C++
programs
File Extension Description
.c C Source file, created using a text editor and
contains source program statements.
.cpp C++ Source file, created using a text editor,
contains source program statements.
.obj Object file, intermediate file generated by the
compiler
.exe Windows executable format for files.
.dmg Mac Specific executable format for files.
In Linux, almost every file is regarded as a
executable file.
What is a computer
algorithm?
A computer algorithm is a list of steps that you can follow to finish
a specific task.

In fact, this does not necessary need to be related to computer


or mathematics.

You can apply computer algorithms even to your daily life to


organize your activities.
Computer algorithms
 To make a computer do anything, you have to write a
computer program.

 To write a computer program, you have to tell the


computer, step by step, exactly what you want it to do.

 The computer then "executes" the program, following


each step mechanically, to accomplish the end goal.

 When you are telling the computer what to do, you also
get to choose how it's going to do it.

 That's where computer algorithms come in...


Computer algorithms e.g.
 Let's follow an example to help get an understanding of
the algorithm concept.

 Let's say that you have a friend arriving at the airport, and
your friend needs to get from the airport to your home.
Computer algorithms e.g.
 Here are four different algorithms that you might give your friend for
getting to your home:
 The taxi algorithm:
• Go to the taxi stand.
• Get in a taxi.
• Give the driver my address.
 The call-me algorithm:
• When your plane arrives, call my cell phone.
• Meet me outside baggage claim.
 The rent-a-car algorithm:
• Rent a car.
• Follow the directions to get to my house.
 The bus algorithm:
• Outside baggage claim, catch the airport bus shuttle service
• Get off on Nicosia Town Centre (e.g. Dereboyu)
• Walk two blocks north to my house.
Computer algorithms e.g.
 All four of these algorithms
• accomplish exactly the same goal
• does it in completely different way
• has a different cost and a different travel time.
Taking a taxi, for example, is probably the fastest way, but
also the most expensive. Taking the bus is definitely less
expensive, but a whole lot slower. You choose the algorithm
based on the circumstances.

 Computer algorithms and programming are similar to this.


There is often more than one possible solution to problems.

 In this case, you will need to evaluate the most efficient


solution for the given circumstances. Programming is the
same! You will often have more than one approach to a
problem and you will have to find the most suitable one!
Types of algorithms
 Algorithm is the method of solutions of a given problem.
Two types of algorithm;

 Pseudocode (text based algorithm)

 Flowchart (diagram based algorithm)


Variables
 A variable is a storage location which contains some
known or unknown quantity or information, particularly a
value generated during the run-time of the program.

 A variable is usually a non-constant value that changes


during the program according to the actions of the users.

 In Computer Algorithms, a variable is nothing more than a


name identifier.
Variable names
 There are some rules about the name identifiers you are
going to use
 Cannot start with number or dash
 Cannot have space or dash between the characters
 Cannot be reserved words or symbols
 Cannot include special characters other than $,_

Valid identifier Invalid identifier


ali74 74Ali
monthly_Salary Monthly salary
firstNameOfCustomer first-name-of-customer
multiply *
Operations in algorithms

 There are two types of operations you are going to deal with
in algorithms and also in programming.

 1) Arithmetic Operations
 These are mathematical operations. Although the operations use
the math logic, the symbols used in programming/computer
algorithms are quite different than regular math symbols.

 2) Logical Operations
 These are decision making you will need to do across the
program/computer algorithms. They usually including comparing
or matching values.
Arithmetic operations
Programming is not MATH! So you will need to use the correct
symbol for arithmetic's.

Precedence Operation Arithmetic Algebraic C expression


order operator expression

1 Multiplication * bm b*m
2 Division / x/y x/y
3 Modulus % r mod s r%s
4 Addition + f+7 f+7
5 Subtraction - p–c p-c
Logical operations
Algebraic C /C++ C /C++ Meaning of C
operator operator expression /C++ expression

Equality = == x == y x is equal to y
Operators
 != x != y x is not equal to y

Relational > > x>y x is greater than y


Operators
< < x<y x is less than y

≥ >= x >= y x is greater than


or equal to y

≤ <= x <= y x is less than or


equal to y
Types of computer algorithms
 As Mentioned before, there are two types of Computer
Algorithms

 Pseudocode
 Uses instruction level English based
commands to do input, output, arithmetic
and logical operations.
 Flowchart
 Uses graphical diagrams to display input,
output, arithmetic and logical operations.
IBM’s Flowchart Template

 There are many different flowchart symbols and templates


being used to represents programming constructs or steps.

 As an example, different Flowchart templates uses different


symbols for input and output

 In this course we use IBM’s Flowchart Template and


symbols.
Flowgorithm
 When a student first learns to program, they often use one
of the text-based programming languages. Depending on
the language, this can either be easy or frustratingly
difficult. Many languages require you to write lines of
confusing code just to display the text "Hello, world!".

 Flowgorithm is a free application that allows you to create


programs using graphical flowcharts. So, you can
concentrate on the algorithm's logic rather than all the
nuances of a typical programming language. You can also
execute your programs directly in Flowgorithm.
Flowgorithm
 Once you understand programming logic, it is
easy for you to learn any programming language

 Flowgorithm is free and available :


http://www.flowgorithm.org/
Structure of algorithms
Description Symbols of Flowchart Words of Pseudocode
Start / Begin Start or Begin
Stop/End Stop or End
Connector Connects to a
previous point or to
multiple points
Declare
Declare a name
identifier
Input Input
Enter
Read
Output Display
Show

Processing Calculate
(Assignment & Set
Calculation)
Example statements
Statements Symbols of Flowchart Words of Pseudocode
input r from the user
Input enter r
r
statement read r

Assignment set 3.14 to pi


pi3.14
statement

calculate pi times r times r and


set the result to area
Arithmetic statement area  pi * r* r

calculate pi * r * r and set the


result to area

Output area
display area
statement
 Or =
Which one to use?
 Notice that  and = signs are interchangeably used in
flowcharts.

 Essentially,

 area  PI * r *r and area= PI*r*r are the same thing.

 You can use any of these as long as you follow the same
standard within your flowcharts
Example of Statements:
declare
 This statement declares a name identifier (i.e. variable) of
a particular data type.

Declares a name identifier X


of Integer type
Integer x

Integer : natural numbers


There are 4 main data types:
Real : real numbers
String : characters
Integer, Real, String, Boolean
Boolean : true or false
Example statements

Symbols of Flowchart Words of Words of


Pseudocode Pseudocode
ii+1 add 1 to i increment i by 1

ii+2 add 2 to i increment i by 2

ii-1 subtract 1 from i decrement i by 2

ii-5 subtract 5 from i decrement i by 5


Exercise 1:
 Draw the flowchart and write the pseudo-code for the
following problem; Display the sentence “Hello World”.

start
Note: START
Each symbol must
follow the other with DISPLAY “Hello World”
an arrow sign: usually
“Hello World” top to down or left to STOP
right sequence.

stop
Exercise 2:
 Draw the flowchart and write the pseudo-code for the following problem:
 Get two numbers from the user, and calculate and display the average of
numbers.

start start

START
Integer num1,num2 Integer num1,num2
Integer sum Real avg
Real avg DECLARE Integer num1,num2

DECLARE Real avg


num1 num1, num2

INPUT num1, num2


avg(num1+num2)/2
num2 SET (num1+num2)/2 TO avg

sumnum1+num2 DISPLAY avg


avg

STOP
avgsum/2
end

avg
Note:
Each step can only be presented with one symbol. However, you can
end use the same symbol to show multiple operations.
Exercise 3:
 Draw the flowchart and write the pseudo-code for the following problem;
 Get three numbers from the user, and calculate and display the product
(multiplication) of numbers.
start

Integer START
a,b,c.mul
DECLARE Integer a, b, c, mul
a, b, c
INPUT a.b.c

mula*b*c SET a*b*c TO mul

DISPLAY mul
mul
STOP
stop
Exercise 4:
 Draw the flowchart and write the pseudo-code for the
following problem;
 Get a number from the user, and calculate and display
the square of that number.

𝑁𝑢𝑚𝑏𝑒𝑟 5 𝑆𝑞𝑢𝑎𝑟𝑒 𝑜𝑓 𝑁𝑢𝑚𝑏𝑒𝑟 5 5∗5 25

𝑁𝑢𝑚𝑏𝑒𝑟 8 𝑆𝑞𝑢𝑎𝑟𝑒 𝑜𝑓 𝑁𝑢𝑚𝑏𝑒𝑟 8 8∗8 64


Exercise 5:
 Draw the flowchart and write the pseudo-code for the
following problem;
 Get two sides of rectangle from the user, and calculate
and display the area.

l𝑒𝑛𝑔ℎ𝑡 4 𝑤𝑖𝑑𝑡ℎ 5 𝐴𝑟𝑒𝑎 𝑙𝑒𝑛𝑔ℎ𝑡 ∗ 𝑤𝑖𝑑𝑡ℎ 4∗5 20


Structure of algorithms
Selection structure
 This structure allows for a choice between two alternatives
of action.

This group This group


of F (False/No) T (True/Yes) of
?
statements (Condition)
statements
will be will be
executed executed
ONLY if the ONLY if the
condition is condition is
FALSE TRUE
Example IF statement
Symbols of Flowchart Words of Pseudocode
IF x is greater than 10 THEN
set 8 to x
x >10
T END IF

F X8

IF x is greater than 10 THEN


set 8 to x
T ELSE
x >10
set 9 to x
END IF
F X8

X9
Exercise 6:
 Draw the flowchart and write the pseudo-code for the following problem;
 Get two numbers from the user and display the greatest number.
start

start START
Integer
x, y,max DECLARE Integer X,Y,max
Integer
x, y
x, y INPUT X,Y

x, y if X IS GREATER THAN Y THEN


set X TO max
x>y
else
F SET Y TO max
x>y
ENDIF
maxx maxy
T DISPLAY max
x y
STOP
max

end
end
Exercise 7:
 Draw the flowchart and write the pseudo-code which will
accept an X value from the user and will show different
outputs according to the entered X value:

X == 0, the output is X is zero

X > 0, the output is X is a positive number

X < 0, the output is X is a negative number


Exercise 8:
 Draw the flowchart and write the pseudo-code which will
ask the user to enter his/her Total Grade from CPE112 and
show corresponding letter grade he/she earned from the
course.

Total Grade Latter Grade


90 – 100 A
77 – 89 B
65 – 76 C
50 – 64 D
00 – 49 F
Structure of algorithms
Loop sequence
 This structure allows for repetation of action. It is used whenever
we want to execute a process more than one time.
 Loops are displayed with the following symbol in flowchart

true
false
Example LOOP sequence
Write the pseudo code and
draw the flowchart of an
algorithm that would count
numbers from 0 to100

START

DECLARE Integer count

set 0 to count

WHILE count is less than or equals to 100


DISPLAY count
SET count+1 to count
END WHILE

STOP
Exercise 9:
 Draw the flowchart and write the pseudo-code which will
display “Hello World” on the screen for 5 times.
START

DECLARE Integer x

set 0 to X

WHILE X is less than 5


DISPLAY “Hello World”
SET X+1 to X
END WHILE

STOP

Count count<5 output

0 0<5 T Hello World


1 1<5 T Hello World
2 2<5 T Hello World
3 3<5 T Hello World
4 4<5 T Hello World
5 5<5 F
Exercise 10:
 Draw the flowchart and write the pseudo-code which will
display the summation of numbers from 1 to 6.

START

DECLARE Integer num,sum


SET 1 to num
SET 0 to sum

WHILE num is less than or equal to 6


SET sum TO sum+num
SET num TO num+1
END WHILE

DISPLAY sum

STOP

num sum num<=6 output

1 0 1<=6 T
1+1=2 0+1=1 2<=6 T
2+1=3 1+2=3 3<=6 T
3+1=4 3+3=6 4<=6 T
4+1=5 6+4=10 5<=6 T
5+1=6 10+5=15 6<=6 T
6+1=7 15+6=21 7<=6 F 21
Exercise 11:
 Draw the flowchart and write the pseudo-code which will
display the summation of numbers from 1 to 1000.

Exercise 12:
 Draw the flowchart and write the pseudo-code which will
display the avarage of numbers from 1 to 1000.

Exercise 13:
 Draw the flowchart and write the pseudo-code which will
display the multiplication of numbers from 15 to 250.
Exercise 14:
 Write the pseudo-code which will get 10
numbers from the user and display their
summation.

START Count Sum count<10 x output

DECLARE Integer count, num, 0 0 0<10 T 5


sum 0+1=1 0+5=5 1<10 T 2
SET 0 to count 1+1=2 5+2=7 2<10 T 10
SET 0 to sum 2+1=3 7+10=17 3<10 T 1
3+1=4 17+1=18 4<10 T 3
WHILE count is less than 10 4+1=5 18+3=21 5<10 T 5
INPUT num 5+1=6 21+5=26 6<10 T 2
SET sum+x TO sum 6+1=7 26+2=28 7<10 T 4
SET count+1 TO count 7+1=8 28+4=32 8<10 T 0
END WHILE 8+1=9 32+0=32 9<10 T 6
9+1=10 32+6=38 10<10 F 38
DISPLAY sum

STOP
Exercise 14:
 Draw the flowchart
which will get 10
numbers from the user
and display their
summation.

 Notice that the & sign


allows to add a
variable to a String
output in FlowChart
Exercise 15:
 Draw the flowchart and write the pseudo-code which will
get 100 numbers from the user and display their
summation.

Exercise 16:
 Draw the flowchart and write the pseudo-code which will
get 1500 numbers from the user and display their average.

Exercise 17:
 Draw the flowchart and write the pseudo-code which will ask
the user to enter a number from keyboard and then display
all the even numbers starting from 2 up to that number.
𝑁𝑢𝑚𝑏𝑒𝑟 6 𝑂𝑢𝑡𝑝𝑢𝑡 2 4 6

𝑁𝑢𝑚𝑏𝑒𝑟 10 𝑂𝑢𝑡𝑝𝑢𝑡 2 4 6 8 10
Exercise 18:
 Draw the flowchart and write the pseudo-code which
display all the integer numbers from 1 to 30 which are
divisible by 3.

𝑖𝑓 𝑛𝑢𝑚𝑏𝑒𝑟 𝑚𝑜𝑑 3 𝑖𝑠 𝑧𝑒𝑟𝑜


𝑛𝑢𝑚𝑏𝑒𝑟 % 3 0 𝑡ℎ𝑎𝑡 𝑚𝑒𝑎𝑛𝑠 𝑡ℎ𝑎𝑡 𝑛𝑢𝑚𝑏𝑒𝑟 𝑖𝑠 𝑑𝑖𝑣𝑖𝑠𝑏𝑙𝑒 𝑏𝑦 3.

𝑂𝑢𝑡𝑝𝑢𝑡 3 6 9 12 15 18 21 24 27 30

Exercise 19:
 Draw the flowchart and write the pseudo-code which will get
2 integer numbers from the user and display all the integer
numbers between them which are divisible by 7.
Revision
In this chapter, we covered:

 Computer algorithms and their structure:


Flowcharts and Pseudo Code
 Three programming constructs: sequence,
control structures and loops
 Variables and identifiers

In the next chapter, we will cover:


An Introduction to C++ Programming

You might also like