ee 31331 programming methodology and software engineering
DESCRIPTION
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]. - PowerPoint PPT PresentationTRANSCRIPT
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
EE 31331 PROGRAMMING METHODOLOGY AND SOFTWARE ENGINEERING
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
Array
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
ArrayArray and pointers are intertwinedint 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]
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
/* 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 100int 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;}
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
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);}
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
/*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;}
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
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 3how to find t[n/2]suppose, minptr points to the array’s first elementmaxptr 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
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
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
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
/* 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
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
*ptr ++
Examplewhile (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++;
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
/* * 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;}
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
/*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);}
Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
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 */
Copyright©1998 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
Pointer Conversiondouble f[MAXVALS], g[MAXVALS];
f = g;
intends to copy array g to f;
*assignment doesn’t work */
instead use
memcpy(f, g, sizeof(g));
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
DYNAMICALLY MEMORY ALLOCATION
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
ALLOCATING STORAGE WITH malloc
malloc allocates a block of bytes of at least the desiredsize 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 */}
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
FREE STORAGE WITH free
To free the memory with free:
free(tptr);
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
ABSTRACT DATA TYPE
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
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.
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
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.
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
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);
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
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);}
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
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
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
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.
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
REALIZATION
examples:
Item.h typedef int Item# define eq(A, B) (A==B)
Item.htypedef char* Item;#define eq(A, B) (strcmp(A, B)==0)
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
LINKED LIST
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
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.
A 1 A 4A 3A 2
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
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 Ai is i
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
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
A 4
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
NODE
The node: • consist of a body • a pointer/link to the next node
B ody L ink
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
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 recordstudent numberyear of admissionyear statusmajor……. B ody L ink
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
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.
B ody L ink
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
EXAMPLE
typedef struct Node *PtrToNode;struct Node{
int coefficient;int Exponent;PtrToNode Next;
};
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
EXAMPLE
A 1 800 A 4 0A 3 694A 2 712
1000 694712800
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
BASIC POINTER OF A LIST
Header - a pointer points to the first element/node of a list
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
LINKED LIST WITH HEADER
A 1 800 A 4 0A 3 694A 2 712
1000 694712800
Header
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
LINKED LIST WITH HEADER AND TAIL
A 1 800 A 4 0A 3 694A 2 712
1000 694712800
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 thelist.
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
BASIC OPERATION ON LINKED LIST
Functions defined to operate on a list:1. Insert - insert a new node into a list2. Delete - delete a node from a list3. Length - compute the length of a list4. Next - return the next node of the current node in
a list5. Search - search if particular element is in the list
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
INSERTION OF A NODE
A 1 A 6 0A 5A 4A 3A 2
A 1 A 3A 2
A i
A 6 0A 5A 4
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
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
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
INSERTION OF A NODE-CAUTION
CHECK FOR EMPTY LIST
Header
The header pointer should be NULL. If the header NULLthen, create a node with CREATE function.
A 1 0header : = list ;next : = NULL;
list
header
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
INSERTION OF A NODE-CAUTION
Check for insertion at the first node
A 1 A 6 0A 5A 4A 3A 2
headerif insertion_point == first node then new_node_next_pointer = header; header := new_node_pointer ;
A 1 A 6 0A 5A 4A 3A 2
new
header
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
INSERTION OF A NODE-CAUTION
Check for insertion as the last node
A 1 A 6 0A 5A 4A 3A 2header
if insertion point == last node (check next_pt == NULL)then insertion_point_next := new_node_pointer; new_node_next_point := NULL;
header
A1 A6A5A4A3A2
new 0
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
INSERTION OF A NODE AT ANY POINT
A 1 A 6 0A 5A 4A 3A 2
A 1 A 3A 2
A i
A 6 0A 5A 4
pre_next_pt := new_node_pt;new_next_pt := insert_pt;
*need to find the pre_next_pt based on insert_pt
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
DELETION OF A NODE
A 1 A 6 0A 5A 4A 3A 2
A 1 A 3A 2 A 6 0A 5A 4
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
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
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
DELETION OF A NODE- CAUTION
Check for empty list.
if header == NULL, error for deletion.
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
DELETION OF A NODE- CAUTION
Check for deletion of first node.
A1 A6A5A4A3A2
A6A5A4A3A2
header
header
if delete_point = = header then header := delete_point_next;
free (delete_point);if header = = Null; then empty_list;
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
DELETION OF A NODE- CAUTION
Check for deletion of last node.
A1 A6A5A4A3A2
header
if delete_point ! = header if delete_next_point = = null then pre_next_point := null; free (delete_point);
A1 A5A4A3A2
Copyright©1998 Angus WuPROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING
DELETION OF ANY INTERNAL NODE
A 1 A 6 0A 5A 4A 3A 2
A 1 A 3A 2 A 6 0A 5A 4
pre_pt_next := delete_pt_next;free (delete_pt);