1 / 50

EE 31331 PROGRAMMING METHODOLOGY AND SOFTWARE ENGINEERING

EE 31331 PROGRAMMING METHODOLOGY AND SOFTWARE ENGINEERING. Array. Array. Array and pointers are intertwined int t [100]; t is defined as the address of the zeroth element. t is equivalent to &t[0]. regardless of what datatype declaration of t is, t is a pointer to its element &t[0].

elkan
Download Presentation

EE 31331 PROGRAMMING METHODOLOGY AND SOFTWARE ENGINEERING

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EE 31331PROGRAMMING METHODOLOGY AND SOFTWARE ENGINEERING

  2. Array

  3. Array Array and pointers are intertwined int t [100]; t is defined as the address of the zeroth element. t is equivalent to &t[0]. regardless of what datatype declaration of t is, t is a pointer to its element &t[0]. t +1 points to &t[1]….t+i points to &t[i] in other words, t + i is the address of t[i] *(t+i) is equivalent to t[i]

  4. /* Compute average and print interesting counts. Uses: * table_fill - reads in table entries (same table_fill as before). * table_avg_cnts - compute average statistics. */ #include <stdio.h> #define MAXVALS 100 int main() { int table_fill(int a[], int max); void table_avg_cnts(int a[], int n, double avg); double table_average(int a[], int n); int t[MAXVALS]; int n = table_fill(t, MAXVALS); double avg = table_average(t, n); printf("There are %i values.\n", n); printf("The average is %g.\n", avg); table_avg_cnts(t, n, avg); return 0; }

  5. int table_fill(int a[], int max) {int count = 0; for (; count < max; count++) if (scanf("%i", &a[count]) != 1) break; /* kick out on error */ return count; } void table_avg_cnts(int a[], int n, double avg) { int i; /* index */ int above = 0; /* above average */ int below = 0; /* below average */ for (i = 0; i < n; i++) if (a[i] > avg) above++; else if (a[i] < avg) below++; printf("There are %i values above average.\n", above); printf("There are %i values below average.\n", below); }

  6. /*Compute average of an array, array subscripting version.*/ double table_average(int a[], int n) { double sum = 0.0; /* running total */ int i; /* count of items */ for (i = 0; i < n; i++) sum += a[i]; return (n != 0) ? sum / n : 0.0; } /* Compute average of an array, pointer version. */ double table_average(int a[], int n) { double sum = 0.0; /* running total */ int i; /* count of items */ int *ptr; /* traversing pointer */ ptr = a; for (i = 0; i < n; i++) { sum += *ptr; ptr++; } return (n != 0) ? sum / n : 0.0; }

  7. Pointer Arithmetic • the only legal; arithmetic operators on pointers are adding and subtracting an integer, or subtracting one pointer from another • p is &t[0], q is &t[3], then q-p is 3 • how to find t[n/2] • suppose, • minptr points to the array’s first element • maxptr points to the array’s last element • *((minptr+maxptr)/2) is illegal as adding to pointer is not allowed. • how about *(minptr + (maxptr-minptr)/2) • as (maxptr-minptr)/2 is an integer

  8. Pointer Comparison • pointers can be compared with the logical operators(==, !=, <. <=, >, >=) • p is &t[j], q is &t[k], if j < k, then p < q • Don’t compare pointers that don’t access the same array

  9. /* Compute average of an array, concise pointer version.*/ double table_average(int a[], int n) { double sum = 0.0; /* running total */ int *ptr; /* traversing pointer */ int *endptr = a + n; /* pointer to just past end */ for (ptr = a; ptr < endptr; ptr++) sum += *ptr; return (n != 0) ? sum / n : 0.0; } int *endptr = a + n; it does not mean *endptr = a + n; but endptr = a + n; int *endptr = a + n; it assign a + n to endptr

  10. *ptr ++ Example while (ptr < endptr) sum += *ptr++; /* it means obtaining the value of the pointer ptr points to and add to sum, then increment the pointer ptr by one. */ it is equivalent to sum += *ptr; ptr++;

  11. /* * Pointer version of input-reversal program. */ #define MAXVALS 100 /* max values in table */ int main() { int table_fill(int a[], int max); void table_print_rev(int a[], int num); int t[MAXVALS]; /* input values*/ int n = table_fill(t, MAXVALS); table_print_rev(t, n); return 0; }

  12. /*Pointer versions of table-handling functions. *table_fill - read values into table. *table_print_rev - print values in reverse order. */ int table_fill(int a[], int max) { int *ptr = a; /* pointer to first element */ int *endptr = ptr + max; /* pointer to just past last element */ for (ptr = a; ptr < endptr; ptr++) if (scanf("%i", ptr) != 1) break; return ptr - a; /* # of values read successfully */ } void table_print_rev(int a[], int num) { int *ptr = a + num; /* pointer to just past last element */ while (ptr-- > a) printf("%i\n", *ptr); }

  13. Array Parameters and Pointers double table_average(int a [ ], int n); is equivalent to double table_average(int *a, int n); /* to compute the average of k elements of t, staring with t[i] */ avg = table_average(&t[i], k); avg = table_average(t + i, k); avg = table_average(t[i], k); /* wrong */

  14. Pointer Conversion double f[MAXVALS], g[MAXVALS]; f = g; intends to copy array g to f; *assignment doesn’t work */ instead use memcpy(f, g, sizeof(g));

  15. DYNAMICALLY MEMORY ALLOCATION

  16. ALLOCATING STORAGE WITH malloc malloc allocates a block of bytes of at least the desired size and returns a pointer, or NULL if a large enough chunk of memory location could not be found. double *tptr /* an array of n double */ .. tptr = malloc(n* sizeof(double)); if (tptr = NULL) { printf(“Couldn’t allocate %I elements\n”, n); return 1; /* out of memory error, quit */ }

  17. FREE STORAGE WITH free To free the memory with free: free(tptr);

  18. ABSTRACT DATA TYPE

  19. ABSTRACT DATA TYPE Definition: An abstract data type (ADT) is a data type (a set of values and a collection of operations on those values) that is accessed only through an interface. We refer to a program that uses an ADT as a client, and a program that specifies the data type as an implementation.

  20. IMPLEMENTATION The representation of the data and the functions that implement the operations are in the implementation, and are completely separated from the client, by the interface. The interface is opaque: the client cannot see the implementation through the interface.

  21. EXAMPLE Point data type interface: The interface defines a data type consisting of the set of values “pair of floating point numbers” and the operation consists of a function that computes the distance between two points. typedef struct { float x; float y;} point; float distance (point a, point b);

  22. EXAMPLE function implementation float distance(struct point a, struct point b) { float dx = a.x - b.x; dy = a.y - b.y; return sqrt(dx*dx + dy*dy); }

  23. PURPOSE • The use of ADT is to make the client program more flexible. Since the data and function implementation are opaque to the clients, any change in the interface would not affect the functionality of the client. • provide flexibility for change and update • various designs for interface • provide independence of source programs

  24. REALIZATION At a higher level, as we have seen, ADT can be defined as an interface in the form of .h files that describe a set of operations on some data structure, with implementations in some independent .c files.

  25. REALIZATION examples: Item.h typedef int Item # define eq(A, B) (A==B) Item.h typedef char* Item; #define eq(A, B) (strcmp(A, B)==0)

  26. LINKED LIST

  27. LINKED LIST Definition: A linked list is a set of items where each item is part of a node that also contains a link to a node. It is a collection of data type ordered in sequence. A general list is of the form, A1, A2 …AN . It is a list of size N. An empty list is a list of size null.

  28. LINKED LIST CHARACTERISTIC • Except an empty list, • Ai+1 follows/succeeds Ai ( i < N) • Ai-1 precedes Ai (i > 1) • A1 is the first node and AN is the last node • The predecessor of A1 and the successor of AN are undefined • The position of node Aiis i

  29. LINKED LIST CHARACTERISTIC • The final node: • It is a null link that points to no node • It refers to a dummy node that contains no item

  30. NODE • The node: • consist of a body • a pointer/link to the next node

  31. BODY Body of a node may consists of one or multiple fields to hold the actual data. It is usually implemented with structure. for example: a student record student number year of admission year status major …….

  32. LINK Link is a pointer points to the next node. Traditionally, it is implemented with pointer that points to the physical address/location of the next node of the link.

  33. EXAMPLE typedef struct Node *PtrToNode; struct Node { int coefficient; int Exponent; PtrToNode Next; };

  34. EXAMPLE

  35. BASIC POINTER OF A LIST Header - a pointer points to the first element/node of a list

  36. LINKED LIST WITH HEADER Header

  37. LINKED LIST WITH HEADER AND TAIL Header Tail Header and Tail are two pointers. Header points to the first node of the list, and tail points to the last node of the list.

  38. BASIC OPERATION ON LINKED LIST Functions defined to operate on a list: 1. Insert - insert a new node into a list 2. Delete - delete a node from a list 3. Length - compute the length of a list 4. Next - return the next node of the current node in a list 5. Search - search if particular element is in the list

  39. INSERTION OF A NODE

  40. INSERTION OF A NODE-CAUTION • When inserting a node to a list, there are several cases needed to be considered: • It has to check the following condition with priority: • an empty list • at the first node • after the last node

  41. INSERTION OF A NODE-CAUTION Header CHECK FOR EMPTY LIST The header pointer should be NULL. If the header NULL then, create a node with CREATE function. list header : = list ; next : = NULL; header

  42. INSERTION OF A NODE-CAUTION Check for insertion at the first node header if insertion_point == first node then new_node_next_pointer = header; header := new_node_pointer ; header

  43. INSERTION OF A NODE-CAUTION Check for insertion as the last node header if insertion point == last node (check next_pt == NULL) then insertion_point_next := new_node_pointer; new_node_next_point := NULL; header

  44. INSERTION OF A NODE AT ANY POINT pre_next_pt := new_node_pt; new_next_pt := insert_pt; *need to find the pre_next_pt based on insert_pt

  45. DELETION OF A NODE

  46. DELETION OF A NODE- CAUTION • When deleting a node to a list, there are several cases needed to be considered: • It has to check the following: • an empty list • the first node • the last node

  47. DELETION OF A NODE- CAUTION Check for empty list. if header == NULL, error for deletion.

  48. DELETION OF A NODE- CAUTION Check for deletion of first node. header header if delete_point = = header then header := delete_point_next; free (delete_point); if header = = Null; then empty_list;

  49. DELETION OF A NODE- CAUTION Check for deletion of last node. header if delete_point ! = header if delete_next_point = = null then pre_next_point := null; free (delete_point);

  50. DELETION OF ANY INTERNAL NODE pre_pt_next := delete_pt_next; free (delete_pt);

More Related