Вы находитесь на странице: 1из 25

?? ?

Algorithms and
programming techniques

Algorithms and programming techniques

Course name: Algorithms and programming techniques

Course and seminars: Cristian Uscatu


Course:
Monday
15:00 - 16:20
Seminars: Monday
12:00 - 13:20 (1025)

Monday
16:30 - 17:50 (1026)

Tuesday
13:30 - 14:50 (1028)

Friday
12:00 - 13:20 (1027)
Consultations
Room 2301, Monday 13:30-14:50
Appointment by email cristian.uscatu@ie.ase.ro

(use your institutional address, provided by the university)

Algorithms and programming techniques

Prerequisites
Basic computer knowledge and abilities
Programing basics (first semester)
Basic Visual Studio skills

You must understand and know how to work with


C/C++

Write console applications


Arrays
1, 2, 3 etc. dimensions
Pointers
operations, memory (de)allocation
Subprograms build, use, parameters

Visual Studio

Create a simple Console Application (no precompiled headers)


Write C/C++ code
Compile/build the application
Debug the application

Algorithms and programming techniques

You will study


Function libraries
Pointers to functions
Iterativity and recursivity
Divide et Impera method
Search, sort, merge arrays
Data validation
External data structures: files - text, binary (sequential, relative, indexed)
Graphs: representation, traversing, connectivity, paths, trees
Greedy method
Backtracking method

Algorithms and programming techniques

Books, available at library


Manual
C. Uscatu, C. Cocianu, B. Ghilic-Micu, M. Stoica, M. Mircea, Algoritmi i tehnici de
programare, Editura ASE Bucureti 2014, 2015

Solved problems /examples book


C. Uscatu, C. Cocianu, M, Mircea, L. Pocatilu, Algoritmi i tehnici de programare.
Aplicaii, Editura ASE 2015

Other books
I. Gh. Roca, B. Ghilic-Micu, C. Cocianu, M. Stoica, C. Uscatu, M. Mircea,
Programarea calculatoarelor. Algoritmi n programare, Editura ASE Bucureti, 2007
C. Uscatu, M. Popa, L. Pocatilu (Btgan), C. Silvestru, Programarea
calculatoarelor. Aplicaii, Editura ASE Bucureti 2012
Thomas H. Cormen, Charles E. Leiserson, Ronald R. Rivest, Introducere n
algoritmi, Computer Libris Agora, 2000

Algorithms and programming techniques

Evaluation
Seminar:

40%

Mandatory homework (prerequisite for practical test): 10%


Practical test:
30%

Examination: 50%
Ex officio: 10%

Miscellaneous
Attendance, missed classes, individual study, rules, collaboration

online.ase.ro accessible from ASE intranet


Course description (fia disciplinei), presentations, other materials

Libraries

Library: collection of functions

Purpose
Code reuse for multiple applications
Distribution to other users

Types
Source code, binary (object) code
Static, dynamic

Libraries

Work options
Command line
cl.exe
- compiler
lib.exe
- library manger
link.exe - link editor
In IDE (Visual Studio)
Same solution (with multiple projects)
Separate solutions

Static libraries

Function code is inserted into the program

File extension
Windows:
Linux:

.lib
.a

Advantages
Single executable file
Only called functions are extracted from the library and inserted

in the program

Drawbacks
Larger executable file
Each executable includes a separate copy of the same functions

Static libraries

Create static library


Source
code
(.c, .cpp, .h)

Compile

Object code
(.obj)

Compile

Object code
(.obj)

Library
manager

Object code
library
(.lib)

Use the library


Source
code files
(.c, .cpp, .h)

Link editor

Object code
library
(.lib)

Executable
file
(.exe)

Static libraries
#include
#include
#include
//alocare

<stdio.h>
<malloc.h>
"matrice.h"
dinamica matrice

// I - nr. linii, nr. coloane


matrice
patrata cu alocare
////citire
E - adresa
matricei
// I double **aloca_matrice(int m, int n);

Source

files

// E - adresa matrice, dimensiune


double** citire_matrice(int *m)
//dezalocare
matrice dinamica
{ int i,j;
// Idouble**
- adresa
a; matricei, nr. linii
// Eprintf_s("\n\nDimensiune:
- adresa matricei (NULL)
");
double**
dezalocare_matrice(double **a, int m);
scanf_s("%d",m);
a=new double*[*m];
for(i=0;i<*m;i++)
//produs
matrice patrate de dimensiuni egale, prealocate
a[i]=new
//
I
a,
b, ndouble[*m];
#include <stdio.h>
for(i=0;i<*m;i++)
E - c
#include//<conio.h>
for(j=0;j<*m;j++)
produs_mpn(double**
a, double **b, int n, double** c);
#includevoid
"matrice.h"
{ printf_s("a[%d,%d]= ",i,j);
scanf_s("%lf",&a[i][j]);
//copiaza} matrice prealocate
void main()
{ double**
// Ia;
- a, a;
m, n
return
int m,n;
//} E - b

header (antet) matrice.h


implementation matrice.cpp
test

test.cpp

void copiaza(double** a, int m, int n, double** b);

a=citire_matrice(&m);
//afisare matrice patrata
afisare_matrice(a,m);
// I - adresa matrice, dimensiune

//citire matrice patrata cu alocare


////I E-_getch();
a, int m)
//void
E -afisare_matrice(double**
adresa matrice, dimensiune
}
{ int i,j;
double** citire_matrice(int *m);

//cout<<"\nMatricea este: \n";


for(i=0;i<m;i++)
//afisare
matrice patrata
{ for(j=0;j<m;j++)
// I - printf_s("%10.6lf
adresa matrice, dimensiune
",a[i][j]);
// E printf_s("\n");
void} afisare_matrice(double** a, int m);
printf_s("\n");
}

Static libraries

Command line
Create a new folder and save the required files
Run vcvars32.bat, found in Visual Studios folder bin
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin

cl -c matrice.cpp
matrice.obj

lib matrice.obj /out:matrice.lib


matrice.lib

cl test.cpp matrice.lib
test.exe

Static libraries

In IDE create binary library


Create a new project, Win32 Console Application

solution name: BSIDE, project name: matrice


In Application settings dialog choose

Application type: Static library


Additional options: Empty project, no Precompiled headers
Add source files to the project (header and implementation)
Compile solution (Build)
The library is created in folder BSIDE\Debug

Library name: matrice.lib

Static libraries

In IDE use binary library in the same solution


Create a new project Win32 Console Application

project name: Test, add to existing solution BSIDE


In Application settings dialog choose:
Application type: Console Application
Additional options: Empty project, no Precompiled headers
Add source file to the project (test file, with main function)
Project > Properties > Common Properties > Framework and References
> Add New Reference > matrice
Project > Properties > Configuration Properties > C/C++ > General >
Additional Include Directories > path to matrice.h
Project > Set As StartUp Project
Compile (Build)
Launch from IDE or separately
Test.exe is in BSIDE\Debug

Static libraries

In IDE use binary library in the separate solution


Create a new project Win32 Console Application in a new solution

solution name (also for project): TestS


In Application settings choose
Application type: Console Application
Additional options: Empty project, no Precompiled headers
Add source file to the project (test file with main function)
Copy to the project folder the files matrice.h and matrice.lib
Project > Properties > Configuration Properties > Linker > Input >
Additional Dependencies > add matrice.lib
Compile (Build)
Launch

Dynamic libraries

Function code is separated from the main program

File extension
Windows:
Linux:

.dll
.so

Advantages
Smaller executable
The library is shared by several applications

Drawbacks
Several files (main executable + dynamic libraries)
Additional processing time
Library must be on the search path or current folder

Dynamic libraries

Create
dynamic
library
Use library

Launch
application
with
dynamic
library

Source
code
(.c, .cpp, .h)
Source
code
(.c, .cpp, .h)

Compile

Compile

Object file
(.obj)
Import table
(.lib)

Link editor

Object file
(.obj)

Link editor

Dynamic
library
(.dll)

Executable
file
(.exe)

Dynamic
library
(.dll)

Object code
library

Executable
file
(.exe)

Dynamic libraries

Building and using dynamic libraries is similar to static


libraries

Differences
Function prototypes must include (only the header file .h)
__declspec(dllexport)

For each function:

__declspec(dllexport) double** citire_matrice(int *m);

At execution, .dll file must be available (same folder or known search

path)

Dynamic libraries

Command line
Create a new folder for the project, include the 3 files
Run vcvars32.bat, from bin folder of Visual Studio
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin

cl matrice.cpp /LD
matrice.dll, matrice.lib

cl test.cpp matrice.lib
test.exe

For execution you need both:


text.exe and matrice.dll

Dynamic libraries

In IDE create dynamic library


Create new project Win32 Console Application

Solution name: BDIDE, project name: matrice


In Application settings dialog choose

Application type: DLL


Additional options: Empty project
Add source files to the project (header and implementation)
Build solution (Build)
The dynamic library will be in the folder BDIDE\matrice\Debug

files: matrice.dll, matrice.lib

Dynamic libraries

In IDE use the dynamic library in the same solution


Create a new project Win32 Console Application

project name: Test, add to existing solution BDIDE


In Application settings dialog choose
Application type: Console Application
Additional options: Empty project, no Precompiled headers
Add source file to the project (text file with main function)
Project > Properties > Common Properties > Framework and References
> Add New Reference > matrice
Project > Properties > Configuration Properties > C/C++ > General >
Additional Include Directories > path to matrice.h
Project > Set As StartUp Project
Build solution (Build)
Run the application

Dynamic libraries

In IDE use the dynamic library in a separate solution


Create a new project Win32 Console Application

solution name (same for project): TestD


Copy matrice.h and matrice.lib to project folder
In Application settings dialog choose
Application type: Console Application
Additional options: Empty project, no Precompiled headers
Add source file (test file with main function)
Project > Properties > Configuration Properties > Linker > Input >
Additional Dependencies > add matrice.lib
Project > Properties > Configuration Properties > Debugging >
Environment > add path to matrice.dll
Build solution (Build)
Run the application

Libraries

HOMEWORK!
Create and test a library with functions for processing

arrays (1/2 dimensions) must include reading and


displaying the arrays. All arrays will be allocated
dynamically.
Static library
Work in command line
Work in IDE (same solution)

Dynamic library
Work in command line
Work in IDE (separate solution)

Dynamic libraries

HOMEWORK!
Size comparison:
Static (.lib)
matrice.h

Dynamic (.dll)

L.C.

IDE

L.C.

IDE

matrice.lib

matrice.dll

test.exe

When should we use libraries?

When should we choose static / dynamic library?

Spor la nvat!