CSE 142 Computer Programming I Overview Concepts this lecture Data structures Arrays Subscripts indices Arrays 2000 UW CSE O 1 O 2 Chapter 8 Rainfall Data Revisited 8 1 Declaration and Referencing General task Read daily rainfall amounts and print some interesting information about them 8 2 Subscripts Input data Zero or more numbers giving daily rainfall followed by a negative number sentinel 8 3 Loop through arrays 8 4 8 5 Arrays arguments and parameters 8 6 Example O 3 Rainfall Analysis Possible things to report How many days worth of data are there How much rain fell on the day with the most rain On how many days was there no rainfall What was the average rainfall over the period On how many days was the rainfall above average What was the median rainfall Question of the day Can we do all of these while O 5 we read the data Example input data 0 2 0 0 0 0 1 5 0 3 0 0 0 1 1 0 Empty input sequence 1 0 O 4 Rainfall Analysis cont For some tasks median number of days above average we need to have all the data before we can do the analysis Where do we store the data Lots of variables rain1 rain2 rain3 rain4 Awkward Doesn t scale Need something better O 6 O 1 Arrays Data Structures Functions give us a way to organize programs Data structures are needed to organize data especially large amounts of data variable amounts of data sets of data where the individual pieces are related to one another In this course we will structure data using arrays structs O 7 combinations of arrays and structs Definition A named ordered collection of variables of identical type Name the collection rain number the elements 0 to 6 Example rainfall for one week 0 1 0 1 0 2 double 0 0 0 0 rain 7 1 4 0 1 O 8 6 0 0 Array Declaration Syntax Accessing Variables type name size array declaration Rainfall for one week double rain 7 0 1 6 1 0 0 2 0 0 0 0 1 4 0 1 0 0 size must be an int constant Variable access rain 0 is 1 0 double rain 7 rain 6 is 0 0 2 0 rain 4 is 2 8 O 9 Array Terminology O 10 Rainfall Analysis cont double rain 7 rain is of type array of double with size 7 rain 0 rain 1 rain 6 are the elements of the array rain Each is a variable of type double 0 1 6 are the indices of the array Also called subscripts The bounds are the lowest and highest values of the subscripts here 0 and 6 O 11 Strategy for processing data if we need all of it before we can process it Read data and store it in an array Analyze data stored in the array Key detail In addition to the array we need to keep track of how much of the array currently contains valid data O 12 O 2 Keep the valid entries together Keeping Track of Elements In Use rain Since an array has to be declared a fixed size you often declare it bigger than you think you ll really need define MAXRAINDAYS 400 int rain MAXRAINDAYS 0 numRainDays 7 6 7 How do you know which elements in the array actually hold data and which are unused extras MAX RAIN DAYS 1 1 Keep the valid entries together at the front 2 Record number of valid entries in a separate O 13 variable Print Days Above Average Algorithm Read data into an array Compute average rainfall from array Keeping track of total of days Count days above average from array Print result O 15 Read Data Into Array for k 0 k numRainDays k process rain k O 14 Declarations Maximum of days of input data define MAXRAINDAYS 400 int main void rainfall data is stored in rain 0 numRainDays 1 double rain MAXRAINDAYS int numRainDays double rainfall current input value double rainTotal sum of input rainfall values double rainAverage average rainfall days with above average rainfall O 16 int numAbove int k Calculate Average double rain MAXRAINDAYS rainfall data int numRainDays of data values double rainTotal sum of input values double rainAverage average rainfall int k read and store rainfall data printf Please enter rainfall data n numRainDays 0 scanf lf rainfall while rainfall 0 0 rain numRainDays rainfall numRainDays scanf lf rainfall calculate average rainfall rainTotal 0 for k 0 k numRainDays k rainTotal rainTotal rain k rainAverage rainTotal numRainDays O 17 O 18 We should make a test to avoid a divide by zero O 3 Calculate and Print Answer Index Rule double rain MAXRAINDAYS rainfall data int numRainDays of data values double rainAverage average rainfall int numAbove of days above average int k count of days with rainfall above average numAbove 0 for k 0 k numRainDays k if rain k rainAverage numAbove Print the result printf d days above the average of 3f n O 19 numAbove rainAverage Rule An array index must evaluate to an int between 0 and n 1 where n is the number of elements in the array No exceptions C Array Bounds are Not Checked define DAYS IN WEEK 7 double rain DAYS IN WEEK int index index 900 rain index 3 5 Is index out of range You need to be sure that the subscript value is in range Peculiar and unpleasant things can and probably will happen if it isn t O 21 Parallel Arrays A set of arrays may be used in parallel when more than one piece of information must be stored for each item Example we are keeping track of a group of students For each item student we might have several pieces of information such as scores O 23 Example rain i 3 k OK as long as 0 i 3 k 6 The index may be very simple rain 0 or incredibly complex rain int 3 1 fabs sin 2 0 PI sqrt 29 067 O 20 Technicalities An array is a collection of variables Each element can be used wherever a simple variable of that type is allowed Assignment expressions input output An entire array can t be treated as a single variable in C Can t assign or compare arrays using Can t use scanf or printf to read or write an entire array But you can do these things one element at a O 22 time Parallel Arrays Example Suppose we have a midterm grade final exam grade and average score for each student define MT WEIGHT 0 30 define FINAL WEIGHT 0 70 define MAX STUDENTS 200 int num student midterm MAX STUDENTS final MAX STUDENTS double score MAX STUDENTS O 24 O 4 Array Elements as Parameters Parallel Arrays Example Suppose we know the value of num students have read student i s grades for midterm and final and stored them in midterm i and final i Now Store a weighted average of exams in array score Individual array elements can be used as parameters …
View Full Document
Unlocking...