You are on page 1of 58

Introduction

CSE-800: Fundamentals of CSE


Email: fawad@rcms.nust.edu.pk
1
Recommended Books
Contents
• What is a computer & computer program
• Design Recipe
• Software Categories
• Computer Languages
• Typical C++ Environment
• A Simple Program
• Variables/Memory Concepts
• Adding two numbers using program
3
What is a Computer?
• What are they good at?
• What type of problems are not suitable for
computers?
• Can computers think?
Parts of the Computer System
• Computer systems have four parts
– Hardware
– Software
– Data
– User
Parts of the Computer System
• Hardware
– Devices in the computer
– Anything that can be touched
• Software
– Tell the computer what to do
– Also called a program
– Thousands of programs exist
Parts of the Computer System
• Data
– Computer organize and present data
• Users
– People operating the computer
– Most important part
– Tell the computer what to do
Program
“A precise sequence
of steps to
solve a particular
problem”

8
All programs consists of:
1. Sequence of instructions
2. Conditionals
3. Loops

These may contain:


– Data
– Input/output (print, etc.)
– Operations (add, divide, etc.)

9
Critical Skills
– Analysis
– Critical Thinking
– Attention to Detail

10
Logical Error
“Mr. ABC sleeps thirty hours every day"

11
Design Recipe
To design a program properly, we must:
– Analyze a problem statement, typically
expressed as a word problem
– Express its essence, abstractly and with
examples
– Formulate statements and comments in a
precise language
– Evaluate and revise the activities in light of
checks and tests
12
• Think Reuse
• Think User Interface
• Comments liberally

13
Think Reuse

14
Area of the Ring
Inner Circle

Outer Circle

Area of Outer Circle ____ Area of Inner Circle = Area of the Ring

15
Software Categories

There are two main categories of software

• System software

• Application Software

16
Hardware
Device Driver
Operating System

Language Scientific Business Productivity Entertainment


Utility
Translator Apps. Apps. Apps. Apps.

System software

Application software
System Software
• Control the overall operation of computer
• Interact directly with hardware
• Perform system management and maintenance
• Used to develop or maintain other programs

18
Application Software
• Interact directly with the user
• Perform a certain type of task
• Interact with hardware through system software

19
20
Machine Languages, Assembly Languages,
and High-level Languages
• Three types of computer languages
1. Machine language
• Only language computer directly understands
• “Natural language” of computer
• Defined by hardware design
– Machine-dependent
• Generally consist of strings of numbers
– Ultimately 0s and 1s
• Instruct computers to perform elementary operations
– One at a time
• Cumbersome for humans
• Example:
+1300042774
+1400593419
+1200274027

 2003 Prentice Hall, Inc. All rights reserved.


21
Machine Languages, Assembly Languages,
and High-level Languages
• Three types of computer languages
2. Assembly language
• English-like abbreviations representing elementary computer
operations
• Clearer to humans
• Incomprehensible to computers
– Translator programs (assemblers)
• Convert to machine language
• Example:
LOAD BASEPAY
ADD OVERPAY
STORE GROSSPAY

 2003 Prentice Hall, Inc. All rights reserved.


22
Machine Languages, Assembly Languages,
and High-level Languages
• Three types of computer languages
3. High-level languages
• Similar to everyday English, use common mathematical
notations
• Single statements accomplish substantial tasks
– Assembly language requires many instructions to
accomplish simple tasks
• Translator programs (compilers)
– Convert to machine language
• Interpreter programs
– Directly execute high-level language programs
• Example:
grossPay = basePay + overTimePay

 2003 Prentice Hall, Inc. All rights reserved.


Machine Language
Assembly Language (1956-63)
LISP (1956) Ada(1983)
Fortran (1957) C++ (1983-85)
COBOL (1959) Perl (1987)
BASIC (1964) VisualBasic (1991)
Pascal (1970) PowerBuilder
C (1972) Java (1995)
JavaScript
C# (2001) 23
Is HTML a
programming
language?
24
Can a single
language have all
the good bits of
other languages?
25
Is there a
perfect
language?
26
Which programming
language should you
learn?

27
30

Tools of the trade

• Editor
• Interpreter and Compilers
• Debuggers

 2003 Prentice Hall, Inc. All rights reserved.


31

Integrated Development Environment


(IDE)

It contains
• Editor
• Compilers
• Debugger
• Linkers
• Loaders

 2003 Prentice Hall, Inc. All rights reserved.


32

Basics of a Typical C++ Environment


Program is created in
Editor Disk
Phases of C++ Programs: the editor and stored
on disk.

Preprocessor Disk Preprocessor program


1. Edit processes the code.
Compiler creates
Compiler Disk object code and stores
2. Preprocess it on disk.
Linker links the object
Linker Disk code with the libraries,
3. Compile Primary
creates a.out and
stores it on disk
Memory
Loader
4. Link
Loader puts program
in memory.
5. Load Disk ..
..
..

6. Execute CPU
Primary
Memory

CPU takes each


instruction and
executes it, possibly
storing new data
..
.. values as the program
..
executes.
 2003 Prentice Hall, Inc. All rights reserved.
33

Basics of a Typical C++ Environment

• Input/output
– cin
• Standard input stream
• Normally keyboard
– cout
• Standard output stream
• Normally computer screen

 2003 Prentice Hall, Inc. All rights reserved.


34

#include <iostream>
using namespace std;
int main ( )
{
cout << “ Welcome” ;
return 0;
}

 2003 Prentice Hall, Inc. All rights reserved.


35
A Simple Program:
Printing a Line of Text
• Comments
– Document programs
– Improve program readability
– Ignored by compiler
– Single-line comment
• Begin with //
• Preprocessor directives
– Processed by preprocessor before compiling
– Begin with #

 2003 Prentice Hall, Inc. All rights reserved.


36
1 // Fig. 1.2: fig01_02.cpp
2 // A first program in C++. Single-line comments. Outline
3 Function main
#include <iostream> returns an
4 integer { begins Preprocessor
value.
Left brace function directive to
include input/output Statements
stream fig01_02.cpp
5 // function main begins
body. program main appears
execution
Function end with a(1 of 1)
6 int main() header
exactly once file <iostream>.
in every C++ semicolon ;.
7 {
program.. fig01_02.cpp
8 std::cout << "Welcome to C++!\n";
9 Corresponding right brace } output (1 of 1)
10 return 0; // indicate
ends thatbody.
function program ended successfully
11 Name coutStream insertion
belongs to operator.
12 } // end function main namespace std.

Keyword return is one of


Welcome to C++!
several means to exit
function; value 0 indicates
program terminated
successfully.

 2003 Prentice Hall, Inc.


All rights reserved.
37
A Simple Program:
Printing a Line of Text
• Standard output stream object
– std::cout
– “Connected” to screen
– <<
• Stream insertion operator
• Value to right (right operand) inserted into output stream
• Namespace
– std:: specifies using name that belongs to “namespace”
std
– std:: removed through use of using statements
• Escape characters
– \
– Indicates “special” character output

 2003 Prentice Hall, Inc. All rights reserved.


38
A Simple Program:
Printing a Line of Text

Escape Sequence Description

\n Newline. Position the screen cursor to the


beginning of the next line.
\t Horizontal tab. Move the screen cursor to the next
tab stop.
\r Carriage return. Position the screen cursor to the
beginning of the current line; do not advance to the
next line.
\a Alert. Sound the system bell.
\\ Backslash. Used to print a backslash character.
\" Double quote. Used to print a double quote
character.

 2003 Prentice Hall, Inc. All rights reserved.


39
1 // Fig. 1.4: fig01_04.cpp
2 // Printing a line with multiple statements.
Outline
3 #include <iostream>
4 using namespace std;
fig01_04.cpp
5 // function main begins program execution Multiple stream insertion
6 int main()
(1 of 1)
statements produce one line
7 {
of output. fig01_04.cpp
8 cout << "Welcome ";
9 cout << "to C++!\n"; output (1 of 1)
10
11 return 0; // indicate that program ended successfully
12
13 } // end function main

Welcome to C++!

 2003 Prentice Hall, Inc.


All rights reserved.
40
1 // Fig. 1.5: fig01_05.cpp
2 // Printing multiple lines with a single statement
Outline
3 #include <iostream>
4 using namespace std;
fig01_05.cpp
5 // function main begins program execution Using newline characters to
6 int main() print on multiple lines. (1 of 1)
7 {
8 cout << "Welcome\nto\n\nC++!\n"; fig01_05.cpp
9 output (1 of 1)
10 return 0; // indicate that program ended successfully
11
12 } // end function main

Welcome
to

C++!

 2003 Prentice Hall, Inc.


All rights reserved.
41

Memory Concepts

• Variable names
– Correspond to actual locations in computer's memory
– Every variable has name, type, size and value
– When new value placed into variable, overwrites previous
value

 2003 Prentice Hall, Inc. All rights reserved.


42

Variable

Variable X

 2003 Prentice Hall, Inc. All rights reserved.


Variable
• Pic of the memory

• 25

name
• 10323 of the
variable

43
 2003 Prentice Hall, Inc. All rights reserved.
45

Variable is the name of a location in


Variable
the memory

e.g. x= 2;

 2003 Prentice Hall, Inc. All rights reserved.


46

Variable
In a program a variable has:
1. Name
Valid identifier
– Series of characters (letters, digits, underscores)
– Cannot begin with digit
– Case sensitive
2. Type
– Common data types (int, char, double)
– Can declare several variables of same type in one declaration
– Comma-separated list
int integer1, integer2, sum;
3. Size
4. Value
 2003 Prentice Hall, Inc. All rights reserved.
47

Variable
Variable starts with
1. Character
2. Underscore _ (Not Recommended)

 2003 Prentice Hall, Inc. All rights reserved.


48

Assignment Operator
=
x=2

X
2
 2003 Prentice Hall, Inc. All rights reserved.
49

Assignment Operator
L.H.S = R.H.S.

X+ 3 = y + 4 Wrong
Z = x +4
x +4 = Z Wrong

 2003 Prentice Hall, Inc. All rights reserved.


50

X = 10 ; X 10
X = 30 ;
30

 2003 Prentice Hall, Inc. All rights reserved.


51

X = X + 1;

10 + 1
= 11
X
 2003 Prentice Hall, Inc. All rights reserved.
Data type
• int i ; ->
Declaration line

i
52
 2003 Prentice Hall, Inc. All rights reserved.
53

#include <iostream>
main ( )
{
int x ;
int y ;
int z ;
x = 10 ;
y = 20 ;
z=x+y;

cout << " x = " ;


cout << x ;

cout << " y = " ;


cout << y ;

cout << " z =x + y = " ;


cout << z ;
}

 2003 Prentice Hall, Inc. All rights reserved.


54

int x, y, z ;
int x; int y; int z ;

 2003 Prentice Hall, Inc. All rights reserved.


55

Memory Concepts

std::cin >> integer1; integer1 45


– Assume user entered 45

std::cin >> integer2; integer1 45


– Assume user entered 72 integer2 72

sum = integer1 + integer2; integer1 45


integer2 72
sum 117

 2003 Prentice Hall, Inc. All rights reserved.


56

Data Types
1. int
2. short
3. long
4. float
5. double
6. char
 2003 Prentice Hall, Inc. All rights reserved.
57

Arithmetic operators
Plus +

Minus -

Multiply *

Divide /

Modulus %
 2003 Prentice Hall, Inc. All rights reserved.
58

Arithmetic operators

i+j
x*y
a/b
a%b
 2003 Prentice Hall, Inc. All rights reserved.
59

% = Remainder

5%2=1
2%2=0

 2003 Prentice Hall, Inc. All rights reserved.


60

4/2=2
5/2=?

 2003 Prentice Hall, Inc. All rights reserved.


61

Precedence
• Highest: ( )
• Next: *,/,%
• Lowest: +,-

 2003 Prentice Hall, Inc. All rights reserved.

You might also like