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 <stdio. h> 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 <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; }
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;主<n;i++) if (a[i]> 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 < 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); }