lecture25-mar30 - Announcements Lecture 25 Assignment 5...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Announcements Lecture 25 • Assignment 5 – Q&A; extended the due date to Sunday night Heuristic (problem solving approach) for autoguess intelligence - should be based on a learning approach - e.g. don’t make the same mistakes twice; and use the frequency table given. Otherwise it is totally up to you. KISS principle to start. • Topics for today Finish function pointers Advanced declarations (Ch. 18) – Variables – Functions Focus Exercises Chapter: exercise# 10: 2 11: 2 , 3, 6 12: 1, 10 13: 5, 11 , 14, 18 14: 1, 4, 10 15: 2, 4 16: 5, 10 ,15 17: 8 18: 2, 4 , 6, 8, 10 , 12 22: 1, 16 26: 5, 10 The qsort Function • Certain functions in the C standard library require a function pointer as a parameter. • One of the most commonly used is qsort, a general-purpose algorithm capable of sorting any array. • The following declaration for qsort appears in <stdlib.h>: void qsort (void *base, size_t num, size_t size, int (*compar) (const void *p, const void *q)); • base is a pointer to the first element in the array. • num is the number of elements in the array. • size is the size of each array element. compar is a pointer to a function that compares two array elements pointed to by parameters p and q. • By convention, when given two pointers p and q to array elements, compar returns a number that is – Negative if *p is “less than” *q. – Zero if *p is “equal to” *q. – Positive if *p is “greater than” *q. Example How to use qsort to sort the inventory array by part #: First, write a function that compares 2 given elements in the array int cmp_parts (const void *p, const void *q) { if (((struct part *)p)->part_no < ((struct part *)q)->part_no) {return -1;} else if (((struct part *)p)->part_no == ((struct part *)q)->part_no) {return 0;} else {return +1;} } Pass the comparison function name to qsort when called qsort (inventory, num_parts, sizeof(struct part), cmp_parts ); /* Inventory example */ #define NAME_LEN 25 #define MAX_PARTS 1000 struct part { int part_no; char part_name[NAME_LEN+1]; int on_hand; } inventory[MAX_PARTS]; int num_parts; /* assume it is filled with values*/ Terminology Reminder Definition - each variable (and function) has only one definition which causes its memory space to be allocated, and its value to be initialized Declaration - informs the compiler that we need access to a named variable (or function), but doesn’t cause it to allocate memory space • A given statement can be both Declaration Syntax •A declaration tells the C compiler about the properties of variables and/or functions. • Declarations have the form declaration-specifiers declarators ; •A declaration-specifier is one of the following: • Storage class specifier (auto, static, extern, or register) • Type specifier (e.g. long, short, unsigned, int, or float) • Type qualifier (const or volatile) • At most one storage class is allowed; if present, it comes first • There may be several type specifiers and/or qualifiers;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

lecture25-mar30 - Announcements Lecture 25 Assignment 5...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online