Title: Introduction to algorithms and data structures in C
1Introduction to algorithms and data structures in
C
2Introduction
3Introduction
- Philosophy learn to program by programming
- Bibliography
The C Programming Language, Second Edition by
Brian W. Kernighan and Dennis M. Ritchie.
Prentice Hall. http//cm.bell-labs.com/cm/cs/cbo
ok/index.html
Programació I web page by Toni Navarrete
http//www.tecn.upf.es/tnavarrete/programacio1/
4von Neumann architecture
CPU
Input
Output
Memory
Instructions Data
5Program design
Problem
Program
- Program Design Process has 2 phases
- Problem Solving Phase
- Creates an algorithm that solves the problem
- Implementation (Coding) Phase
- Translates the algorithm into a programming
language
Algorithm
6Levels
CAB
High-Level Languages
LOAD A ADD B STORE C
Assembly Languages
1300042774 1400593419 1200274027
Machine Languages
7Compiler vs interpreter
- Compiler
- Interpreter
- Hybrid solution virtual machine
8Programming paradigms
- Main paradigms
- Imperative. It uses a programming process that
follows a sequence of commands, that when
executed manipulate data to produce a solution. - Examples Machine languages, Basic, C, Pascal,
Cobol, Fortran, Algol - Declarative. Based on logical deduction, and
require the programmer to develop an accurate and
precise statement of the problem rather than
discovering an algorithm for solving it. - Examples PROLOG, GPSS.
9Programming paradigms
- Main paradigms
- Functional. We express computation as the
evaluation of mathematical functions. These are
nested inside the language like black boxes,
which accept input and produce output - Examples LISP, MIL, Scheme, C
- Object Oriented. Units of data are viewed as
active objects, instead of being merely a passive
collection of data controlled by a program. - Examples C, JAVA, SIMULA, SMALLTALK,ada95,
Visual Basic - C is imperative and functional
10C language
- History
- 1972, Dennis Ritchie, Bell Laboratories at ATT,
created to develop UNIX - 1983, ANSI C
- First program
- include ltstdio.hgt
-
- main()
-
- printf(Hello world!")
-
11Compilation
C library
hello.o
compile
a.out a.exe
hello.c
Link
Edit
Source File
Object File
Executable
gcc hello.c
12Summary
- A program is like a recipe. It contains a list of
ingredients (called variables) and a list of
directions (called statements) that tell the
computer what to do with the variables. The
variables can represent numeric data, text, or
graphical images.
13Summary
- Variables and constants
- Basic data types
- Numbers
- Characters
- Booleans
- Expressions
- Statements
- Assignments
- Input and output
- Control of flow
- Sequential
- Conditional
- Iterative
14Variables
- A variable is a named memory location
- The contents of a variable can change
- All variables in C must be declared before use.
- data_type var1, var2,
- Where data_type is one of the four basic types
(integer, character, float, or double) and varx
are identifiers.
15Variables
- include ltstdio.hgt
-
- main()
-
- int x
- x3
- printf(The value of the variable x is d", x)
-
16Constants
- include ltstdio.hgt
-
- main()
-
- const float pi3.1416
- printf(The value of the constant pi is f",
pi) -
-
-
17Constants
- include ltstdio.hgt
- define pi 3.1416
-
- main()
-
- printf(The value of the constant pi is f",
pi) -
18Identifiers
- Identifiers is C must begin with a character or
underscore, and may be followed by any
combination of characters, underscores, or the
digits 0-9 - summary exit_flag i12 _id
- You should ensure that you use meaningful (but
short) names for your identifiers - distance speed time
- Do not use reserved identifiers
- if, else, while,
19Basic data types
- Data types
- Numbers
- Integers (int)
- Fractional (float, double)
- Characters (char)
- Boolean
- Modifiers short, long, unsigned
20Numbers
- include ltstdio.hgt
-
- main()
-
- const float pi3.1416
- float radi
- float area
-
- printf(Enter the radius ")
- scanf("f",radi)
-
- area pi radi radi
-
- printf(The surface is f",area)
21Characters
- include ltstdio.hgt
-
- main()
-
- char c,d
-
- c 'A'
- d c
-
- a ! A
- C treats character as integers. The ASCII code is
used to associate each character with an integer.
22Characters
- a ! A
- C treats character as integers. The ASCII code is
used to associate each character with an integer. - A \101 \x41
- Special characters
- \b backspace
- \n newline
- \t tab
- \
- \
- \\ \
23Characters
- Special characters
- \b backspace
- \n newline
- \t tab
- \
- \
- \\ \
- printf(Hello \bworld)
- printf(Hello\bworld)
- printf(Hello \nworld)
- printf(Hello \tworld)
24Boolean
- 0 gt FALSE
- Other integer value gt TRUE
25Expressions
- An expression in C is some combination of
constants, variables, operators and function
calls. - a (b c)
- Operators
- Arithmetic operators
- Logic operators
- Relational operators
26Arithmetic operators
27Logic operators
28Relational operators
29Boolean logic
30Statements
- A statement in C is an expression terminated with
a semicolon. - Assignment operator
- a 5 3
- b a
- Increment/Decrement operators
- i is equivalent to i i 1
- i is equivalent to i i 1
- --i is equivalent to i i 1
- i-- is equivalent to i i 1
31Decision making statements
32Decision making statements (if)
if(expression) statement
- include ltstdio.hgt
- main()
-
- int number
- printf( Enter an integer ")
- scanf("d",number)
- if (number lt0)
-
- number number (-1)
-
- printf(Its absolute value is d", number)
-
33Decision making statements (if-else)
if(expression) statement1 else statement2
- if the expression is TRUE, statement1 is
executed statement2 is skipped - if the expression is FALSE, statement2 is
executed, statement1 is skipped
34Decision making statements (if-else)
- include ltstdio.hgt
- main()
-
- float nota1,nota2,nota3,notafinal
- printf("Introdueix la primera nota ")
- scanf("f",nota1)
- printf("Introdueix la segona nota ")
- scanf("f",nota2)
- printf("Introdueix la tercera nota ")
- scanf("f",nota3)
- notafinal (nota1nota2nota3)/3
- if (notafinalgt5)
- printf("La nota final es APROVAT")
- else
- printf("La nota final es SUSPENS")
-
35Decision making statements (if-else)
- main()
-
- int n1,n2
-
- printf("Enter the first integer ")
- scanf("d",n1)
- printf("Enter the second integer ")
- scanf("d",n2)
-
- if (n1gtn2)
- printf(The greater is d",n1)
- else
- printf(The greater is d",n2)
-
36Decision making statements (if-else)
- include ltstdio.hgt
-
- main()
-
- int lloc
- printf("Introdueix el lloc on ha acabat
l'esportista ") - scanf("d",lloc)
- if (lloc1)
-
- printf("Medalla d'or")
-
- else
-
- if (lloc2)
-
- printf("Medalla d'argent")
-
- else
-
37Decision making statements (if-else)
- if (lloc3)
-
- printf("Medalla de bronze")
-
- else
-
- if ((llocgt3) (lloclt9))
-
- printf("Diploma olimpic")
-
- else
-
- printf("Cap premi")
-
-
-
-
-
38Decision making statements (switch)
switch (expression) case constant1 statem
ent1 break case constant2 statement2
break ... default statement
39Decision making statements (switch)
- include ltstdio.hgt
- main()
-
- int lloc
- printf("Introdueix el lloc on ha acabat
l'esportista ") - scanf("d",lloc)
- switch (lloc)
-
- case 1
- printf("Medalla d'or")
- break
- case 2
- printf("Medalla d'argent")
- break
- case 3
- printf("Medalla de bronze")
- break
40Decision making statements (switch)
- case 4
- case 5
- case 6
- case 7
- case 8
- printf("Diploma olimpic")
- break
- default
- printf("Cap premi")
- break
-
-