DSA Session1
DSA Session1
A.Gautam
1
6 Apr 2013
• Faculty: Gautam Avasarala
2
Course Outline
• Introduction
• Overview of C
• Top-Down Design with Functions
• Selection Structures
• Loop Statements
• Simple Data Types
• Arrays
• Strings
• Recursion
• Structure and Union Types
• Dynamic Data Structures
• On to C++
3
Part-1
Overview of Computers
• Overview of the development of computers.
• Computer system components
• Computer Networks and Types
• Software categories and languages
• Software development steps
• Process of writing, compiling, and executing
high-level language programs.
4
Computers
• IBM ThinkPad ®
• Sony Clié PDA ®
• IBM NetVista Desktop
5
Computer Components
• Memory:
– main
– secondary
• CPU
• Input devices
• Output devices
6
Architecture of Computer
Control Processing Unit (CPU)
Control Unit
Arithmethic &
Input Memory Output
Logic Unit (ALU)
System Bus
7
Architecture of Computer
• Input unit
– Takes inputs from the external world via a variety of input devices keyboard, mouse,
temperature sensors, odometers, wireless devices etc.
• Output Unit
– Sends information (after retrieving, processing) to output devices –monitors/displays,
projectors, audio devices, switches, relays,Unit
Control gearbox etc.
• Memory
– Place where information is stored.
• System Bus
Used by the other units
– Input Arithmethic
to communicate with each other. &
Memory Output
• ALU – Arithmetic and Logic Unit
Logic Unit (ALU)
– Processes data; add, subtract, decision – after comparing with data, for example
• Control Unit
System
– Controls the interaction among other Bus
units.
– Knows each unit by its name, responds to requests fairly, reacts quickly on certain critical
events. Gives up control periodically in the interest of the system.
– Together with the ALU is called the CPU. 8
Architecture of Computer
• THE CPU Control Unit
– Coordinating all computer operations
– Performing arithmetic and logical& operations in data
Arithmethic
Input Memory Logic Unit (ALU)
Output
– Can fetch an instruction from memory
– Execute the instruction
– Store the result in memory
• A program – a set of instructions
• An instruction has the following structure:
System Bus
– Operation operands, destination
9
Memory
• Memory cells
• Address
• Contents
• Main Memory
– Random access memory
– Read-only access memory
• Secondary Memory
– CD, DVD, etc.
10
Computer Networks
• Local area network (LAN)
11
Computer Networks
• Wide Area Network (WAN)
12
Computer Software
• System Software: Operating System (OS),
Application Software: Programs
• Operating System:
– Command-Line Interface
• UNIX
• MS-DOS
– Graphical User Interface
• Macintosh OS
• Windows
• UNIX + X Window System
13
Computer Languages
• Machine Language
– Collection of binary numbers
• Assembly Language
– Represented by Mnemonic codes
• High-Level Language
– Independent of the CPU (works on any Platform)
14
High-Level Languages
Language Applications Areas Origin of Name
FORTRAN Scientific programming Formula Translation
16
High-Level Languages
17
Flow of Information during Program
Execution
18
Problem Solving
• The student must read the problem statement a
number of times to ensure that he/she understands
what is asked before attempting to solve the problem.
• The following steps need to be followed:
1. read the problem carefully
2. understand what the problem entails
3. and only then, write down the steps to solve the problem.
• An everyday example will be:
– I need to go to school. Write down the steps to take from
waking up in the morning until I am at school.
19
Problem Solving
• 1. Wake up.
• 2. Get out of bed.
• 3. Wash.
• 4. Put on clothes.
• 5. Prepare something to eat.
• 6. Eat.
• 7. Take my bag.
• 8. Go to school.
20
Algorithms
• your steps must always be in a logical order!
• These steps are called an algorithm that can be defined as a
set of sequential instructions to solve a problem.
• The most important aspect of solving a problem by using a
computer is to write an algorithm to solve it.
• An algorithm is a set of steps that must be written in such a
way that is it unambiguous and precise.
• The computer cannot think for itself – you as the
programmer must tell the computer exactly what to do.
• You may never assume that the computer will do something
if you have not explicitly included the specific step.
21
Algorithms
• Think of an algorithm as a recipe.
• We can re-use this recipe to
– take specific ingredients(input)
– cook them together(processing)
– produce some food(output)
22
Algorithms
• An algorithm to solve a computer based problem consists of 3
phases i.e.
1. What you have available for the algorithm to solve the problem
2. How you are going to solve the problem i.e. what steps you are
going to take
3. What is the required result
23
Algorithms
• An algorithm must receive data (input) that must be
processed to render meaningful results (output or
information).
• In other words:
• Input is processed to render meaningful output.
• Data is processed to render meaningful information.
24
Software Development Method
1. Specify the problem requirements
2. Analyze the problem
3. Design the algorithm to solve the problem
4. Implement the algorithm
5. Test and verify the completed program
6. Maintain and update the program
25
Flowcharts
• A flow chart is a graphical or symbolic
representation of a process.
• Each step in the process is represented by a
different symbol and contains a short
description of the process step.
• The flow chart symbols are linked together with
arrows showing the process flow direction.
• This diagrammatic representation can give a
step-by-step solution to a given problem.
26
Flowcharts
Different flow chart symbols have different meanings. The most common flow
chart symbols are:
• Terminator: An oval flow chart shape indicating the start or end of the
process.
• Process: A rectangular flow chart shape indicating a normal process
flow step.
• Decision: A diamond flow chart shape indication a branch in the
process flow.
• Connector: A small, labeled, circular flow chart shape used to indicate
a jump in the process flow. (Shown as the circle with the letter “A”,
below.)
• Data: A parallelogram that indicates data input or output (I/O) for a
process.
• Document: Used to indicate a document or report. 27
Flowcharts
28
Part-2
Overview of Programming
• C Language Introduction
• Variables
• Data Types
• Operators
• Logical Operators
• Increment/Decrement Operators
• Operators
• Expressions
• C Compilation
• Frames
• C / Assembler
29
C – A Programming Language
• Developed at AT&T’s Bell Labs in 1972 – by Dennis Ritchie
• Initially implemented on Unix OS
• During late 70’s C began to replace ALGOL,PL/I,…
• C is Reliable, Simple, Portable, Efficient, Easy to use.
• Many languages emerge and vanished but C survived for more
than 3 decades
• C is a middle level Language
– It combines elements of high-level languages with functionalities of
Assembly Level language
– C allows manipulation of Bits, Bytes and Address also.
• Unlike Sequential Language, C is a Structural Language.
30
C – A Programming Language
C has been superceded by C++, C#, Java,.. But why to Learn C?
– We can not learn C++ directly (learn language elements & implement)
– C++, C#, Java,.. uses OOPS concepts
– Major parts of popular O.S like Windows, Unix, Linux still uses C
– C is exclusively used in Device Driver programming
– Programs not only have to run fast but also have to work in limited
amount of Memory.
– Games in early years are written in C, because a Game has to react fast
to the used input.
– C also supports Graphics
– C is case sensitive.
– C uses English language words as English is popular through out the
world.
31
C – Programming Constructs
Variable:
• The name itself says that it varies
• Variables are named locations in memory which have symbolic
names that hold values
• In C, All Variables should be declared before they can be used.
– Variable declarations include
• A symbolic name
• Type (int, char, double) - data type
• Scope (code region where the variable is defined)
– Variables are stored in memory or in registers.
– The compiler keeps track of
• Where a variable value is currently stored
• When it needs to be moved from memory to a register, or
from a register to memory 32
C – Programming Constructs
Variable:
Variables are declared in two types
– Local (automatic)
– Global (static)
• Local Variables (automatic)
– Declared at the beginning of a block
– Stored in activation record on the stack
– Scope is from point of declaration to the end of the block
– Un-initialized
• Global Variables (static)
– Declared outside of a function
– Stored in Global Data Section of memory
– Scope is entire program
– May be initialized to zero
33
C – Programming Constructs
Datatype:
• The name itself says that the type of data being stored in a
location.
• Four basic data types:
– character, integer, floating-point, double floating-point
(char, int, float, double respectively)
char type holds one character and occupies 1 byte of space
int type holds one integer value and occupies 2 bytes of space
float and double are used for floating point numbers.
float occupies 4 bytes
double occupies 8 bytes
• Modifiers are used to alter the meaning of the base type to fit
various situations precisely
34
C – Programming Constructs
Type Size Representation Minimum Maximum
char, signed char 8 bits ASCII -128 127
unsigned char bool 8 bits ASCII 0 255
short, signed short 16 bits 2s complement -32768 32767
unsigned short 16 bits Binary 0 65535
int, signed int 16 bits 2s complement -32768 32767
unsigned int 16 bits Binary 0 65535
long, signed long 32 bits 2s complement -2,147,483,648 2,147,483,647
unsigned long 32 bits Binary 0 4,294,967,295
enum 16 bits 2s complement -32768 32767
float 32 bits IEEE 32-bit 1.175495e-38 3.4028235e+38
double 32 bits IEEE 32-bit 1.175495e-38 3.4028235e+38
long double 32 bits IEEE 32-bit 1.175495e-38 3.4028235e+38
pointers, references 16 bits Binary 0 0xFFFF
function pointers 16 bits Binary 0 0xFFFF
35
C – Programming Constructs
Declaring a Local and Global Variables by specifying Datatype :
void main(void)
{ local variables
int outLocal;
int inLocalA;
int inLocalB;
outLocal = inGobal+inLocalA+inLocalB;
return;
}
36
C – Programming Constructs
int i,j,k; // declaring more than one variable
int i1, i2, i3, c3po; // numbers OK, except for first letter
int bananas = 10; // using an initializer
int monkey_count = 0; // two ways of doing ...
int monkeyCount = 0; // ... multi-word names
int ab, Ab, aB, AB; // case sensitive names
int _compilerVar; // compiler uses _ as first char
38
C – Programming Constructs
Operators:
Operators can be further classified into 3 categories
Unary – operates on 1 operand.
Binary – operates on 2 operands.
Ternary – operates on 3 operands.
39
C – Programming Constructs
Operators:
Arithmetic Operator – Performs Arithmetic operations like
– Add + x + y
– Subtract –
–
x – y
Multiply *
– Divide / x * y
• Integer; integer division; 5/3 = 1 (truncated to int) x / y
• Floating point : 5.0 / 3.0 = 1.66666666
– Modulus % x % y
• Integer; remainder after integer division; 5 % 3 = 2
40
C – Programming Constructs
Operators:
Bitwise Operator – Perform logical operations across individual bits of a value.
– AND & x : 1 0 1 0 (binary)
– OR | y : 1 1 0 0 (binary)
– XOR ^ x & y : 1 0 0 0 (binary)
– NOT ~ (1’s complement) x | y : 1 1 1 0 (binary)
Bitwise Shifts: x ^ y : 0 1 1 0 (binary)
– SHIFT LEFT <<
~x : 0 1 0 1 (binary)
– SHIFT RIGHT >>
42
C – Programming Constructs
Operators:
Logical Operator
– Operators are Symbols used to combine or negate expressions
containing relational operators f && g
• AND &&
f || g
• OR ||
• NOT ! (2’s complement) !f
– Logical operators take “Boolean” inputs and produce “Boolean” outputs
– Don’t confuse Logical Operators with Bitwise operators
AND - Checks if both conditions are true
OR - Checks if any one of the conditions being tested is true
NOT – Reverses the logical value of the expression.
43
C – Programming Constructs
Operators:
Increment / Decrement Operator
– Increments / Decrements the value of the operand
– Used in two ways : Before (pre) or After(post)
– Mostly used in Loops • x++ post-increment
• ++x pre-increment
• x-- post-decrement
• --x pre-decrement
eg. x y x y
x = 4; x = 4;
y = x++; 5 4 y = x--; 3 4
x = 4; x = 4;
y = ++x; 5 5 y = --x; 3 3
44
C – Programming Constructs
Expressions:
• A syntactical structure that returns or can be changed to a single
value is termed as an Expression
• To solve most programming problems, you will need to write
Expressions.
• To write an expression we use different types of operators.
• Precedence-The order in which operators and expressions are evaluated
• Associativity
– The order in which operators of the same precedence are evaluated
• Left to Right for +, – , *, /, %
• Right to Left for some other operators
• Parentheses
– Override the evaluation rules
45
C – Programming Constructs
Expressions: Operator Precedence / Associativity
OPERATORS ASSOCIATIVITY
( ) [ ] -> . left to right
! ~ ++ -- + - * & (type) sizeof right to left
* / % left to right
+ - left to right
<< >> left to right
< <= > >= left to right
== != left to right
& left to right
^ left to right
| left to right
&& left to right
|| left to right
?: left to right
= += -= *= /= %= &= ^= |= <<= >>= right to left
, left to right
46
C – Programming Constructs
Expressions:
• Precedence (*, /, %) > (+, –)
a - b / c * d + e * f
(( a - (( b / c ) * d )) + ( e * f ))
47
C – Programming Constructs
Expressions:
Conditional Expression:
• C Language offers a shorthand notation, which provides conditional
execution of Statements.
• This Conditional expression has two parts;
– ? and :
• This Conditional Expression uses Ternary Operator.
expression1 ? expression2 : expression3
49