Download EEM 480 Algorithms and Complexity

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
EEM 480 Algorithms and Complexity
by
Assist. Prof. Dr. Emin Germen
What is this Course About

Of Course it is about :
Algorithms
Complexity
Any More?
Algorithms + Complexity = Data Structure
Data Structure : Concerns with the representation
and manipulation of data.
All the programs use and manipulate data
IS (Computer Programming == Data Structure)
YES OR NO
What is Data Structure
Designing the program which modifies data
The Key Point
Algorithm
What is this Course About




Learning and designing algorithms to
manipulate the data
Comparing criteria of different algorithms
Understand what a good program is
Learning effective utilizations of computer
sources
Syllabus

Review of C++





Arrays and Linked Lists










Arrays, Matrices, Special matrices, Sparse matrices
Stacks
Queues
Hash Tables
Trees


Linear lists
Formula based representation
Linked representations
Indirect addressing and pointers
Arrays and Matrices


Classes
Encapsulation, Overloading, Inheritance, Overriding
Pointers to objects
Templates
Binary trees
Tree traversals
Priority Queues
Search Trees



Binary search trees
AVL trees
B-trees
Reference and Evaluation
Data Structures, algorithms, and
Applications in C++, Sartaj Sahni


Evaluation:
Mt 1
 Mt 2
 Projects
 Final Exam

10%
20%
30%
40%
Object Oriented Programming





Object
Class
Encapsulation
Inheritence
Polymormhism
Object


An object is a bundle of variables and related
methods.
When an object is mapped into software representation,
it consists of 2 parts:


DATA STRUCTURE
characteristics of data structure are refered to as
ATTRIBUTES
PROCESSES that may correctly change the data structure
processes are refered to as OPERATIONS or METHODS
Class


A class is a blueprint for an object.
Objects with the same data structure (Attributes) and behavior
(Methods or Operations) are grouped together (called a class ).
Encapsulation
Encapsulation is the procedure of covering up
of data and functions into a single unit
Class

Data1
Data2
Procedure1(Data1)
Data2 = Procedure()

Encapsulation hides the implementation away
from the user
Inheritence

As objects do not exist by themselves but are instances
of a CLASS, a class can inherit the features of another
class and add its own modifications. (This could mean
restrictions or additions to its functionality). Inheritance
aids in the reuse of code.
Polymorphism

Polymorphism means the ability to request that
the same Operations be performed by a wide
range of different types of things.
So What???????




What are those things????
How can I use them???????
I have searched OOP in the Internet and find
ABSTRACTION. What does it mean????
I can write good programs using structural
proggramming techniques? Why should I use
OOP?
Intoduction to OOP with C++

Object Oriented Programming

Class
The structure which keeps data and function


Interitance
Inheritance is also called derivation. The new class inherits
the functionality of an existing class. The existing class is
called the base class, and the new class is called the derived
class. A similar inheritance can be derived for animals,
mammals, and dogs.
Polymorphisim Overloading
Ability to have more than one function with the same name that differ
in their parameter lists.
Program 1
#include <stdio.h>
#include <iostream.h>
Header for the basic C++ procedures
void main(void)
{
cout << "This is not my first program";
cout << "\n";
}
Streaming functions
Program 2
// Workspace: Triangle
// Program name: Area.cpp
// The area of a triangle is half its base times height
// Area of triangle = (Base length of triangle * Height of triangle)/2
#include <iostream.h> // Precompiled header
double base,height,area; // Declaring the variables
double Area(double,double); // Function Prototype/declaration
int main()
{
cout << "\nEnter Height of Triangle: "; // Enter a number
cin >> height; // Store the input in variable
cout << "\nEnter Base of Triangle: "; // Enter a number
cin >> base; // Store the input in variable
area = Area(base,height);// Store the result from the Area function
// in the variable area
cout << "The Area of the Triangle is: "<< area << endl ;
return 0;
}
double Area (double base, double height) // Function definition
{
area = (0.5*base*height);
return area;
}
The Output
Program 3 Polymorphism
# include <iostream.h>
if (choice == 2)
{
cout << "Enter radius of the Circle: ";
cin >> radius;
Area_of_circle = Area(radius);
cout << "The area of the Circle is: " <<
Area_of_circle<<endl;
}
if (choice != 1 && choice != 2)
{
cout << "Sorry! You must enter either 1 or 2 \n";
}
double base,height,radius; // Global variables
double Area_of_triangle,Area_of_circle; // Global variables
int choice; // Global variable
double Area (double,double); // Function prototype
double Area (double); // Function prototype
const double pi = 3.14; // Constant variable
void main() // main function
{
cout << "To find the area of a Triangle, input 1 \n";
cout << "To find the area of a Circle, input 2 \n";
cin >> choice;
if (choice == 1)
{
cout << "Enter the base of the triangle: ";
cin >> base;
cout << "Enter the height of the triangle: ";
cin >> height;
Area_of_triangle = Area(base,height);
cout << "The Area of the Triangle is: " <<
Area_of_triangle<<endl;
}
}
double Area (double base, double height)
{
return (0.5*base*height);
}
double Area(double radius)
{
return (pi*radius*radius);
}
The Output
Defining Class in C++



Almost same as defining Struct
Insert Functions into the Struct
Some important functions
Constructor
 Destructor

Object Oriented C++ Class
#include <iostream.h>
class CCircle
{
public:
CCircle(int r);
void SetRadius(int r);
void DisplayArea(void);
~CCircle();
private:
float CalculateArea(void);
int m_radius;
int m_color;
};
CCircle::CCircle(int r)
{
m_radius = r;
}
CONSTRUCTOR
CCircle::~CCircle()
{}
void CCircle::DisplayArea(void)
{
float fArea;
fArea = CalculateArea();
cout << "The Arae of the circle : "
<< fArea << "\n";
}
float CCircle::CalculateArea(void)
{
DESTRUCTOR
float f;
f = (float) (3.14 * m_radius *
m_radius);
return f;
}
Dynamic Memory Allocation
new
int *y //pointer
y = new int; // creates place
*y = 10 // OK
Or
int *y = new int(10)
OOP in C++

Simple example of defining object and class


Overloading


Programming Example
Inheritance


Programming Example
Programming Example
Templates

Programming Example
One Dimensional Array and
Exception Handling
float *x = new float [n]
Or better
float *x
try { x = new float [n];}
catch (xalloc) {
cerr << “Out of memory << endl;
exit(1); }
Delete
delete y; //free the memory allocated by *y
delete [] x // free the one dimensional array