KEMBAR78
Data structure and Algorithms (C++).pptx
TABLE OF CONTENT
 Elementary data representation
 Primitive & non-primitive data types
 Different data structures
 Arrays
 Algorithm
 Pointers
 This poniter
 Dynamic Memory Allocation
REFERENCE BOOKS
 Data Structures and Algorithm Analysis by Mark Allen Weiss
 Object Oriented Programming in C++ 4th Edition by Robert Lafore.
 C++ Programming 5th
Edition by D.S.Malik
Tools and Technology used
• Visual Studio/ Visual Studio Code
• Must be installed on your systems
• Download Link:
• https://code.visualstudio.com/download
• https://visualstudio.microsoft.com/vs/
#include<iostream>
using namespace std;
int main()
{
int arr[10];
for(int i=0;i<10;i++)
{
cout<<"enter a number:";
cin>>arr[i];
}
cout<<"data in array: n";
for(int i=0;i<10;i++)
{
cout<<arr[i]<<endl;
}
}
WHAT IS AN ALGORITHM
An algorithm is a clearly specified set of simple instructions to
be followed to solve a problem.
WHAT IS AN OPTIMIZED
ALGORITHM
 Once an algorithm is given for a problem and decided (somehow) to be
correct, an important step is to determine how much in the way of resources,
such as time or space, the algorithm will require. An algorithm that solves a
problem but requires a year is hardly of any use. Likewise, an algorithm that
requires thousands of gigabytes of main memory is not (currently) useful on
most machines.
POINTERS
 Pointer fun video: https://youtu.be/i49_SNt4yfk
 Normal variables contain a specific value (direct reference)
int count = 7;
 Pointer variables contain memory addresses as their values
int * countPtr;
countPtr = & count;
15
count
7
count
7
countPtr
POINTER VARIABLE DECLARATIONS AND
INITIALIZATION
 A pointer declaration takes the following form:
type *identifier;
e.g.
int *myPtr;
 Declares a pointer to an int (pointer of type int *)
 We can declare pointers to any data type.
e.g. float *fptr; char *cptr;
 We usually initialize pointers to nullptr
 nullptr – points to nothing
e.g.
myPtr = nullptr;
16
POINTER OPERATORS
 & (address operator) - Returns the address of operand
int y = 5;
int *yPtr;
yPtr = &y; // yPtr gets address of y
 yPtr “points to” y
17
yPtr
y
5
yptr
500000 600000
y
600000 5
Address of y
is value of
yptr
POINTER OPERATORS
 * (indirection/dereferencing operator)
 Returns an alias of what its operand points to
 *yptr returns y (because yptr points to y)
 * can be used for assignment
*yptr = 7; // changes y to 7
 * and & are inverses
 They cancel each other out
18
19
int rate;
int *p_rate;
rate = 500;
p_rate = &rate;
1000 1004 1008 1012
500
1008
rate
p_rate
Memory
/* Print the values */
cout <<"rate = "<< rate << endl; /* direct access */
cout <<"rate = "<< *p_rate << endl; /* indirect access */
20
EXERCISE 3
int a, b, *p;
a = b = 7;
p = &a;
// 1st print statement
cout << "*p = " << *p << endl;
*p = 3;
// 2nd print statement
cout << "a = " << a << endl;
p = &b;
*p = 2 * (*p – a);
// 3rd print statement
cout << "b = " << b << endl;
THIS POINTER
 Each class object has a pointer which automatically points to
itself.The pointer is identified by the keyword this.
 Another way to think of this is that each member function (but
not friends) has an implicit first parameter; that parameter is
this, the pointer to the object calling that function.
21
DYNAMIC MEMORY ALLOCATION
 new and delete
– new - automatically creates object of proper size, calls constructor, returns
pointer of the correct type
– delete - destroys object and frees space
 You can use them in a similar way to malloc and free in C.
 Syntax:
– TypeName *typeNamePtr;
– typeNamePtr = new TypeName;
– new creates TypeName object, returns pointer (which typeNamePtr is set equal
to)
– delete typeNamePtr;
– Calls destructor for TypeName object and frees memory
22
EXAMPLES
// declare a ptr to int data type
int *ptr1;
int *ptr2;
// dynamically allocate space for an ptr1;
// initialize values; return pointer and assign
// to ptr1
ptr1 = new int;
// similar for int:
ptr2 = new int(2);
// free up the memory that ptr1 points to
delete ptr1;
23
24
// dynamically allocate array of 23
// Int slots
// each will be initialized to 0
ptr1 = new int[23];
// similar for ptr2
ptr2 = new int[12];
// free up the dynamically allocated array
delete [] ptr1;

Data structure and Algorithms (C++).pptx

  • 2.
    TABLE OF CONTENT Elementary data representation  Primitive & non-primitive data types  Different data structures  Arrays  Algorithm  Pointers  This poniter  Dynamic Memory Allocation
  • 3.
    REFERENCE BOOKS  DataStructures and Algorithm Analysis by Mark Allen Weiss  Object Oriented Programming in C++ 4th Edition by Robert Lafore.  C++ Programming 5th Edition by D.S.Malik
  • 4.
    Tools and Technologyused • Visual Studio/ Visual Studio Code • Must be installed on your systems • Download Link: • https://code.visualstudio.com/download • https://visualstudio.microsoft.com/vs/
  • 10.
    #include<iostream> using namespace std; intmain() { int arr[10]; for(int i=0;i<10;i++) { cout<<"enter a number:"; cin>>arr[i]; } cout<<"data in array: n"; for(int i=0;i<10;i++) { cout<<arr[i]<<endl; } }
  • 13.
    WHAT IS ANALGORITHM An algorithm is a clearly specified set of simple instructions to be followed to solve a problem.
  • 14.
    WHAT IS ANOPTIMIZED ALGORITHM  Once an algorithm is given for a problem and decided (somehow) to be correct, an important step is to determine how much in the way of resources, such as time or space, the algorithm will require. An algorithm that solves a problem but requires a year is hardly of any use. Likewise, an algorithm that requires thousands of gigabytes of main memory is not (currently) useful on most machines.
  • 15.
    POINTERS  Pointer funvideo: https://youtu.be/i49_SNt4yfk  Normal variables contain a specific value (direct reference) int count = 7;  Pointer variables contain memory addresses as their values int * countPtr; countPtr = & count; 15 count 7 count 7 countPtr
  • 16.
    POINTER VARIABLE DECLARATIONSAND INITIALIZATION  A pointer declaration takes the following form: type *identifier; e.g. int *myPtr;  Declares a pointer to an int (pointer of type int *)  We can declare pointers to any data type. e.g. float *fptr; char *cptr;  We usually initialize pointers to nullptr  nullptr – points to nothing e.g. myPtr = nullptr; 16
  • 17.
    POINTER OPERATORS  &(address operator) - Returns the address of operand int y = 5; int *yPtr; yPtr = &y; // yPtr gets address of y  yPtr “points to” y 17 yPtr y 5 yptr 500000 600000 y 600000 5 Address of y is value of yptr
  • 18.
    POINTER OPERATORS  *(indirection/dereferencing operator)  Returns an alias of what its operand points to  *yptr returns y (because yptr points to y)  * can be used for assignment *yptr = 7; // changes y to 7  * and & are inverses  They cancel each other out 18
  • 19.
    19 int rate; int *p_rate; rate= 500; p_rate = &rate; 1000 1004 1008 1012 500 1008 rate p_rate Memory /* Print the values */ cout <<"rate = "<< rate << endl; /* direct access */ cout <<"rate = "<< *p_rate << endl; /* indirect access */
  • 20.
    20 EXERCISE 3 int a,b, *p; a = b = 7; p = &a; // 1st print statement cout << "*p = " << *p << endl; *p = 3; // 2nd print statement cout << "a = " << a << endl; p = &b; *p = 2 * (*p – a); // 3rd print statement cout << "b = " << b << endl;
  • 21.
    THIS POINTER  Eachclass object has a pointer which automatically points to itself.The pointer is identified by the keyword this.  Another way to think of this is that each member function (but not friends) has an implicit first parameter; that parameter is this, the pointer to the object calling that function. 21
  • 22.
    DYNAMIC MEMORY ALLOCATION new and delete – new - automatically creates object of proper size, calls constructor, returns pointer of the correct type – delete - destroys object and frees space  You can use them in a similar way to malloc and free in C.  Syntax: – TypeName *typeNamePtr; – typeNamePtr = new TypeName; – new creates TypeName object, returns pointer (which typeNamePtr is set equal to) – delete typeNamePtr; – Calls destructor for TypeName object and frees memory 22
  • 23.
    EXAMPLES // declare aptr to int data type int *ptr1; int *ptr2; // dynamically allocate space for an ptr1; // initialize values; return pointer and assign // to ptr1 ptr1 = new int; // similar for int: ptr2 = new int(2); // free up the memory that ptr1 points to delete ptr1; 23
  • 24.
    24 // dynamically allocatearray of 23 // Int slots // each will be initialized to 0 ptr1 = new int[23]; // similar for ptr2 ptr2 = new int[12]; // free up the dynamically allocated array delete [] ptr1;