EE31331 PROGRAMMING METHODOLOGY AND SOFTWARE ENGINEERING PROGRAMMINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu EE 31331 PROGRAMMING METHODOLOGY AND SOFTWARE ENGINEERING
Array PROGRAMMINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu 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[o] regardless of what datatype declaration of t is t is a pointer to its element &t[o] t+1 points to &t[1] .. tti points to &tO in other words, t+iis the address of to (tt) is equⅳ alent to t[] PROGRAM MINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu 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]
/ 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 i define MAXVALS 100 int main o t int table fill(int a[] int max)i void table avg cnts(int a[l, int n, double avg) double table average(int a[l, int n)i Ln in t七 t [MAXVALS]; table fill(t, MAXVALS) double avg table average(t, n)i printf("There are %i values. \n", n) printf("The average is g.\n", avg)i table avg cnts(t, n, avg)i return 0 PROGRAM MINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu ol Hone Kone
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu /* 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 #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; }
int table fill(int all int max) fint 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 bel。w=0; / below average * for(立=0;主 avg) above++ else if (all]< avg) below++ printf(" There are %i values above average. \n", above) printf("There are i values below average. n below)i PROGRAM MINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu ol Hone Kone
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu int table_fill(int a[], int max) {int count = 0; for (; count 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); }
/*Compute average of an array, array subscripting version. * double table average (int a[], int n double sum =0.0;/* running total in七 / count of items * for(主=0;i<n;i++) sum + a[ili 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 in七 1; / count of items * in七 ptri /* traversing pointer * ptr for(主=0;i<n;i++) I sum + *ptri ptr++i return (n ! 0)? sum /n: 0.0; PROGRAM MINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu ol Hone Kone
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu /*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; }
Pointer Arithmetic the only legal; arithmetic operators on pointers are adding and subtracting an integer, or subtracting one pointer from another p is &t[o], q is &t[3], then g-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 PROGRAM MINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu 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
Pointer Comparison pointers can be compared with the logical operators p is &tU], q is &t[k], if j<k, then p<q Don't compare pointers that don't access the same array PROGRAMMINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu 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
/ Compute average of an array concise pointer version. * double table average(int a[], int n) double sum =0.0; /* running total * int ptri /* traversing pointer * int *endptr a n; / pointer to just past end * for (ptr ai ptr endptri ptr++) sum + *ptri return (n 0)? sum/n: 0.0 int *endptr atn it does not mean endptr +n; but endptr a+n 七 endptr a+ n; it assign a n to endptr PROGRAM MINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu ol Hone Kone
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu /* 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
ptr +t 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 equivalentto sum+=ptr ptr++, PROGRAMMINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu *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++;