Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored.

Slides:



Advertisements
Similar presentations
UNIT 9: Pointers Data Variable and Pointer Variable Pass by Reference
Advertisements

Programming and Data Structure
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Algorithm Analysis.
1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Fundamentals of Python: From First Programs Through Data Structures
11.2 Complexity Analysis. Complexity Analysis As true computer scientists, we need a way to compare the efficiency of algorithms. Should we just use a.
Complexity Analysis (Part I)
Cmpt-225 Algorithm Efficiency.
The Efficiency of Algorithms
C++ for Engineers and Scientists Third Edition
Pointers Applications
Basic Concepts Chapter 1 Objectives
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure.
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Equation --- An equation is a mathematical statement that asserts the equality of twomathematicalstatement expressions. An equation involves an unknown,
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Program Performance & Asymptotic Notations CSE, POSTECH.
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
Week 2 CS 361: Advanced Data Structures and Algorithms
For Wednesday Read Weiss chapter 3, sections 1-5. This should be largely review. If you’re struggling with the C++ aspects, you may refer to Savitch, chapter.
Introduction to complexity. 2 Analysis of Algorithms Why do we need to analyze algorithms? –To measure the performance –Comparison of different algorithms.
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms.
Analysis of Algorithms
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Pointer. What is pointer ? A Pointer is nothing but a variable that contains an address which is a location of another variable in memory. If one variable.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Principles of programming languages 5: An operational semantics of a small subset of C Department of Information Science and Engineering Isao Sasano.
Grading Exams 60 % Lab 20 % Participation 5% Quizes 15%
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Pointer Arithmetic CSE 2541 Rong Shi. Pointer definition A variable whose value refers directly to (or "points to") another value stored elsewhere in.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Computer Science and Software Engineering University of Wisconsin - Platteville 8. Comparison of Algorithms Yan Shi CS/SE 2630 Lecture Notes Part of this.
M180: Data Structures & Algorithms in Java Algorithm Analysis Arab Open University 1.
COSC 1P03 Data Structures and Abstraction 2.1 Analysis of Algorithms Only Adam had no mother-in-law. That's how we know he lived in paradise.
Arrays as pointers and other stuff COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
Searching Topics Sequential Search Binary Search.
CISC220 Spring 2010 James Atlas Lecture 07: Big O Notation.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the concept and use of pointers ❏ To be able to declare, define,
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
CSC 212 – Data Structures Lecture 15: Big-Oh Notation.
Chapter 2 Copyright © 2015, 2011, 2007 Pearson Education, Inc. Chapter 2-1 Solving Linear Equations and Inequalities.
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
Algorithm Analysis 1.
Data Structures 1.
Complexity Analysis (Part I)
EGR 2261 Unit 11 Pointers and Dynamic Variables
Introduction to Analysis of Algorithms
Computer Skills2 for Scientific Colleges
Analysis of Algorithms
Introduction to complexity
Algorithm Efficiency Algorithm efficiency
Building Java Programs
Chapter 12: Analysis of Algorithms
Lecture 18 Arrays and Pointer Arithmetic
Computer Skills2 for Scientific Colleges
CSE 373: Data Structures & Algorithms
8. Comparison of Algorithms
Analysis of Algorithms
Pointers and pointer applications
Complexity Analysis (Part I)
Programming fundamentals 2 Chapter 3:Pointer
Complexity Analysis (Part I)
Presentation transcript:

Pointers (Continuation) 1

Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored elsewhere in the computer memory using its address. Obtaining the value that a pointer refers to is called dereferencing the pointer. 2

Pointer to void Major programming languages are strongly typed. This means that operations such as assign and compare must use compatible types or be cast to compatible types. The only exception is the pointer to void, which can be assigned without a cast. This means that a pointer to void is a generic pointer that can be used to represent any data type. 3

Pointer to void 4

5

Important remark: a pointer to void cannot be dereferenced unless it is cast. In other words, we cannot use *p without casting (without connection of the pointer with particular data type). 6

Function malloc This function in C (a similar function is presented in all modern programming languages) returns a pointer to void. This function is used to dynamically allocate any type of data. This is a generic function that returns a pointer to void (void*). It can be used for returning a pointer to any data type. For example, a pointer to an integer can be created using intPtr = (int*)malloc (sizeof (int)) 7

Pointer to Node 8

9

(Continued) 10

11

12

13

14

(Continued) 15

Pointer to Function Functions in our program occupy memory. The name of the function is a pointer constant to its first byte of memory. To declare a pointer to function, we code it as if it was a prototype definition, with the function pointer in parentheses. 16

17

Example: function larger This generic function will return a larger value of two values to be compared To use a larger function as a generic one, we will need to write a compare function for each particular data type. A compare function will return either a positive or negative flag value depending on which value in a compared pair is larger: the first one or the second one. 18

19

20

21

22

23

24

25

Algorithm Efficiency Big-O Notation 26

What is the algorithm’s efficiency The algorithm’s efficiency is a function of the number of elements to be processed. The general format is where n is the number of elements to be processed. 27

The basic concept When comparing two different algorithms that solve the same problem, we often find that one algorithm is an order of magnitude more efficient than the other. A typical example is a famous Fast Fourier Transform algorithm. It requires NxlogN multiplications and additions, while a direct Fourier Transform algorithm requires N 2 multiplications and additions. 28

The basic concept If the efficiency function is linear then this means that the algorithm is linear and it contains no loops or recursions. In this case, the algorithm’s efficiency depends only on the speed of the computer. If the algorithm contains loops or recursions (any recursion may always be converted to a loop), it is called nonlinear. In this case the efficiency function strongly and informally depends on the number of elements to be processed. 29

Linear Loops The efficiency depends on how many times the body of the loop is repeated. In a linear loop, the loop update (the controlling variable) either adds or subtracts. For example: for (i = 0; i < 1000; i++) the loop body  Here the loop body is repeated 1000 times.  For the linear loop the efficiency is directly proportional to the number of iterations, it is: 30

Logarithmic Loops In a logarithmic loop, the controlling variable is multiplied or divided in each iteration For example: Multiply loop Divide loop for (i=1; i<=1000; i*=2) for (i=1000; i<=1; i /=2) the loop body the loop body  For the logarithmic loop the efficiency is determined by the following formula: 31

32

Linear Logarithmic Nested Loop The outer loop in this example adds, while the inner loop multiplies A total number of iterations in the linear logarithmic nested loop is equal to the product of the numbers of iterations for the external and inner loops, respectively (10log10 in our example). for (i=1; i<=10; i++) for (j=1; j<=10; j *=2) the loop body  For the linear logarithmic nested loop the efficiency is determined by the following formula: 33

Quadratic Nested Loop Booth loops in this example add A total number of iterations in the quadratic nested loop is equal to the product of the numbers of iterations for the external and inner loops, respectively (10x10=100 in our example). for (i=1; i<10; i++) for (j=1; j<10; j ++) the loop body  For the quadratic nested loop the efficiency is determined by the following formula: 34

Dependent Quadratic Nested Loop The number of iterations of the inner loop depends on the outer loop. It is equal to the sum of the first n members of an arithmetic progression: (n+1)/2 A total number of iterations in the quadratic nested loop is equal to the product of the numbers of iterations for the external and inner loops, respectively (10x5=50 in our example). for (i=1; i<10; i++) for (j=1; j<i; j ++) the loop body  For the dependent quadratic nested loop the efficiency is determined by the following formula: 35

Big-O notation In general, the number of statements executed in the function for n elements of data is a function of the number of elements expressed as f(n). Although the equation derived for a function may be complex, a dominant factor in the equation usually determines the order of magnitude of the result. This factor is a big-O, as in “on the order of”. It is expressed as O(n). 36

Big-O notation The big-O notation can be derived from f(n) using the following steps (pp ): 1.In each term set the coefficient of the term to 1. 2.Keep the largest term in the function and discard the others. Terms are ranked from lowest to highest: log n, n, n log n, n 2, n 3,…, n k,…2 n,…, n! For example, 37

38

39

40

41

Add two matrices In this algorithm, we see that for each element in a raw, we add all the elements in a column. This means that we have a quadratic loop here and the efficiency of the algorithm is O(n 2 ) 42

43

44

45

Multiply two matrices In this algorithm, we see three nested loops. Because each loop starts at the first element, we have a cubic loop and the efficiency of the algorithm is O(n 3 ) 46

Homework Sections Exercises (Section 1.9): 2, 6, 7, 8, 20, 21 47