Introduction to algorithms and data structures in C - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

Introduction to algorithms and data structures in C

Description:

Introduction to algorithms and data structures in C lectures 1 and 2. MSc Bioinformatics ... Philosophy: learn to program by programming. Bibliography ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 41
Provided by: sga63
Category:

less

Transcript and Presenter's Notes

Title: Introduction to algorithms and data structures in C


1
Introduction to algorithms and data structures in
C
  • jesus.ibanez_at_upf.edu

2
Introduction
  • Jesús Ibáñez Martínez

3
Introduction
  • 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/
4
von Neumann architecture
CPU
Input
Output
Memory
Instructions Data
5
Program 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
6
Levels
CAB
High-Level Languages
LOAD A ADD B STORE C
Assembly Languages
1300042774 1400593419 1200274027
Machine Languages
7
Compiler vs interpreter
  • Compiler
  • Interpreter
  • Hybrid solution virtual machine

8
Programming 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.

9
Programming 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

10
C 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!")

11
Compilation
C library
hello.o
compile
a.out a.exe
hello.c
Link
Edit
Source File
Object File
Executable
gcc hello.c
12
Summary
  • 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.

13
Summary
  • Variables and constants
  • Basic data types
  • Numbers
  • Characters
  • Booleans
  • Expressions
  • Statements
  • Assignments
  • Input and output
  • Control of flow
  • Sequential
  • Conditional
  • Iterative

14
Variables
  • 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.

15
Variables
  • include ltstdio.hgt
  •  
  • main()
  • int x
  • x3
  • printf(The value of the variable x is d", x)

16
Constants
  • include ltstdio.hgt
  •  
  • main()
  • const float pi3.1416
  • printf(The value of the constant pi is f",
    pi)
  •  
  •  

17
Constants
  • include ltstdio.hgt
  • define pi 3.1416
  •  
  • main()
  • printf(The value of the constant pi is f",
    pi)

18
Identifiers
  • 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,

19
Basic data types
  • Data types
  • Numbers
  • Integers (int)
  • Fractional (float, double)
  • Characters (char)
  • Boolean
  • Modifiers short, long, unsigned

20
Numbers
  • 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)

21
Characters
  • 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.

22
Characters
  • 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
  • \
  • \
  • \\ \

23
Characters
  • Special characters
  • \b backspace
  • \n newline
  • \t tab
  • \
  • \
  • \\ \
  • printf(Hello \bworld)
  • printf(Hello\bworld)
  • printf(Hello \nworld)
  • printf(Hello \tworld)

24
Boolean
  • 0 gt FALSE
  • Other integer value gt TRUE

25
Expressions
  • An expression in C is some combination of
    constants, variables, operators and function
    calls.
  • a (b c)
  • Operators
  • Arithmetic operators
  • Logic operators
  • Relational operators

26
Arithmetic operators
27
Logic operators
28
Relational operators
29
Boolean logic
30
Statements
  • 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

31
Decision making statements
32
Decision 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)

33
Decision 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

34
Decision 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")

35
Decision 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)

36
Decision 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

37
Decision making statements (if-else)
  • if (lloc3)
  • printf("Medalla de bronze")
  • else
  • if ((llocgt3) (lloclt9))
  • printf("Diploma olimpic")
  • else
  • printf("Cap premi")

38
Decision making statements (switch)

switch (expression) case constant1 statem
ent1 break case constant2 statement2
break ... default statement
39
Decision 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

40
Decision making statements (switch)
  • case 4
  • case 5
  • case 6
  • case 7
  • case 8
  • printf("Diploma olimpic")
  • break
  • default
  • printf("Cap premi")
  • break
Write a Comment
User Comments (0)
About PowerShow.com