... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

C’ POINTERS Basic&Examples. Q:what’s the output? int array[] = { 45, 67, 89 }; int *array_ptr = array; printf(" first element: %i\n", *(array_ptr++));
SEE C GO Provisional Title. Syntax Types int, float, double, char, void Identifiers foo Operators + - * / ^ Delimiters ; {} () “” ‘’ Keywords return,
Part 1 Landscape Hannu Laine. Pointer parameters //prototype of swap void swap(int *a, int *b); //application void main(void) { in number1 = 1, number2.
Functions Prototypes, parameter passing, return values, activation frams.
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Structures Spring 2013Programming and Data Structure1.
Data Types C built-in data types –char, int, float, double, int*, etc. User-defined data types: the programmer can define his/her own data types which.
 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Pointer Arithmetic Dale Roberts, Lecturer Computer.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Chapter 8, Sorting. Sorting, Ordering, or Sequencing “Since only two of our tape drives were in working order, I was ordered to order more tape units.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
1 Pointers & functions Pointers allow us to simulate pass by reference. void swap(int &a, int &b) { int temp = a; a = b; b = temp; } int main () { int.
1 Homework / Exam HW7 due class 25 Exam 3 - class 26 –Open Book, Open Notes –Covers up through end of K&R 7 –and Appendix B Standard Library –Plus UNIX.
Topic 2 Pointers CSE1303 Part A, Summer Semester,2002 Data Structures and Algorithms.
Pointers Chapters 6+9 in ABC. abp 12 int a = 1, b = 2, *p; & - reference operator (address) * - dereference operator (value) p = &a; // *p is now 1 abp.
1 Motivation Dynamically allocated storage and pointers are an essential programming tools –Object oriented –Modularity –Data structure But –Error prone.
1 More on pointers. Example 1 – pass by value/reference int main(){ int p, q, r; p = 5; q = 6; r = Sum_1(p, q + 1); return 0; } void Sum_1(int a, int.
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
Understanding BubbleSort CS-502 (EMC) Fall Understanding BubbleSort CS-502, Operating Systems Fall 2009 (EMC) (Slides include materials from Modern.
Array Must declare a variable to reference the array double [] mylist; // cannot double list[20]; Or double mylist[]; The declaration doesn’t allocate.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 9: Pass-by-Value.
C language issues CSC 172 SPRING 2002 EXTRA LECTURE.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
Value Iteration 0: step 0. Insertion Sort Array index67 Iteration i. Repeatedly swap element i with.
1 MT258 Computer Programming and Problem Solving Unit 9.
Functions, Pointers, Structures Keerthi Nelaturu.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Crypto Project Sample Encrypted Data: –Turing: CS Public\CryptoProjectData Crypto_Short_Keys_P_U.out Crypto_Long_Keys_O_R.out Bonus +10 points on.
Generic Functions1 Generic Functions: A generic function is one that can work on any underlying C data type. Generic functions allow us to reuse programs.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Copyright © – Curt Hill Pointers A Light Introduction.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
Advanced Pointer Topics. Pointers to Pointers u A pointer variable is a variable that takes some memory address as its value. Therefore, you can have.
Pointers & References. Pointers Pointer arithmetic Pointers and arrays Pointer-related typedef’s Pointers and const References.
CSCI 62 Data Structures Dr. Joshua Stough December 2, 2008.
Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC Spring 2007, Section 001.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Structs in C Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens struct Properties The C struct mechanism is vaguely similar.
Array Sort. Sort Pass 1 Sort Pass 2 Sort Pass 3.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Partitioning in Quicksort n How do we partition the array efficiently? – choose partition element to be rightmost element – scan from right for smaller.
1 Memory, Arrays & Pointers. Memory 2 int main() { char c; int i,j; double x; cijx.
Generic Programming in C
How to be generic Lecture 10
Pointer Basics Psst… over there.
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Return by Reference CSCE 121 J. Michael Moore.
Shaker.
Qsort.
Simulating Reference Parameters in C
Programming Language C Language.
CS150 Introduction to Computer Science 1
Type compatibility and pointer operation
Pointer Basics Psst… over there.
Presentation transcript:

Built into qsort is a function that can swap two given array elements.

elem_1. elem_2 Built into qsort is a function that can swap two given array elements.

elem_1. elem_2 Built into qsort is a function that can swap two given array elements.

Built into qsort is a function that can swap two given array elements. Because qsort is general (can operate on any array), there needs to be some function that takes references to any pair of array elements as input and determines which is smaller.

Built into qsort is a function that can swap two given array elements. Because qsort is general (can operate on any array), there needs to be some function that takes references to any pair of array elements as input and determines which is smaller. What general mechanism does c++ provide for passing objects to functions?

Built into qsort is a function that can swap two given array elements. Because qsort is general (can operate on any array), there needs to be some function that takes references to any pair of array elements as input and determines which is smaller. What general mechanism does c++ provide for passing objects to functions? Answer: void *

Suppose array elements are of type int. How do you find the value of the elements?

elem_1. elem_2 Suppose array elements are of type int. How do you find the value of the elements? Answer: int cmp(void *elem_1, void *elem_2) {

elem_1. elem_2 Suppose array elements are of type int. How do you find the value of the elements? Answer: int cmp(void *elem_1, void *elem_2) { int val_1 = *elem_1; int val_2 = *elem_2;

elem_1. elem_2 Suppose array elements are of type int. How do you find the value of the elements? Answer: int cmp(void *elem_1, void *elem_2) { int val_1 = *elem_1; int val_2 = *elem_2; No Good! Cannot find value of unknown element type

elem_1. elem_2 Suppose array elements are of type int. How do you find the value of the elements? Answer: int cmp(void *elem_1, void *elem_2) { int val_1 = *(int *)elem_1; int val_2 = *(int *)elem_2; That's it!! Cast the pointer to be a pointer to type int

elem_1. elem_2 Suppose array elements are of type int. How do you find the value of the elements? Answer: int cmp(void *elem_1, void *elem_2) { int val_1 = *(int *)elem_1; int val_2 = *(int *)elem_2; if (val_1 < val_2) return -1; if (val_1 > val_2) return 1; return 0; }

elem_1. elem_2 typedef struct { int city_1, city_1, cost } Cable; int cmp(void *elem_1, void *elem_2) {

elem_1. elem_2 typedef struct { int city_1, city_1, cost } Cable; int cmp(void *elem_1, void *elem_2) { int val_1 = (*(Cable *)elem_1).cost; int val_2 = (*(Cable *)elem_2).cost;

elem_1. elem_2 typedef struct { int city_1, city_1, cost } Cable; int cmp(void *elem_1, void *elem_2) { int val_1 = (*(Cable *)elem_1).cost; int val_2 = (*(Cable *)elem_2).cost; if (val_1 < val_2) return -1; if (val_1 > val_2) return 1; return 0; }

elem_1. elem_2 typedef struct { int city_1, city_1, cost } Cable; int cmp(void *elem_1, void *elem_2) { int val_1 = ((Cable *)elem_1)->cost; int val_2 = ((Cable *)elem_2)->cost;

elem_1. elem_2 typedef struct { int city_1, city_1, cost } Cable; int cmp(void *elem_1, void *elem_2) { int val_1 = ((Cable *)elem_1)->cost; int val_2 = ((Cable *)elem_2)->cost; if (val_1 < val_2) return -1; if (val_1 > val_2) return 1; return 0; }

int *A = new int[10]; for (int i=0 ; i < 10 ; i++) A[i] = rand() % 100; qsort(A, 10, sizeof(int), cmp);... int cmp (const void *a, const void *b) { if (*(int *)a < *(int *)b) return -1; if (*(int *)a > *(int *)b) return 1; return 0; }

Consider an array of pointers to int

Consider an array of pointers to int elem_1. elem_ int cmp(void *elem_1, void *elem_2) {

Consider an array of pointers to int elem_1. elem_ int cmp(void *elem_1, void *elem_2) { int val_1 = *(int *)elem_1; int val_2 = *(int *)elem_2; No Good! elem_1 is a pointer to a pointer!!

Consider an array of pointers to int elem_1. elem_ int cmp(void *elem_1, void *elem_2) { int val_1 = **(int **)elem_1; int val_2 = **(int **)elem_2; That's it! elem_1 needs to be dereferenced twice!!

Consider an array of pointers to int elem_1. elem_ int cmp(void *elem_1, void *elem_2) { int val_1 = **(int **)elem_1; int val_2 = **(int **)elem_2; if (val_1 < val_2) return -1; if (val_1 > val_2) return 1; return 0; }

typedef struct { int city_1,city_2,cost; } Cable; elem_1. elem_ int cmp(void *elem_1, void *elem_2) {

typedef struct { int city_1,city_2,cost; } Cable; elem_1. elem_ int cmp(void *elem_1, void *elem_2) { int val_1 = (**(Cable **)elem_1).cost; int val_2 = (**(Cable **)elem_2).cost;

typedef struct { int city_1,city_2,cost; } Cable; elem_1. elem_ int cmp(void *elem_1, void *elem_2) { int val_1 = (**(Cable **)elem_1).cost; int val_2 = (**(Cable **)elem_2).cost; if (val_1 < val_2) return -1; if (val_1 > val_2) return 1; return 0; }