Loops - all done. Programs due this week. Exam Tuesday, Nov. 24. Sections 5.1 - 5.5 and 5.7 Arrays - Chapter 8 An array is a data structure - a composite of related data items stored under the same name. Why do we need arrays? Used to store a collection of similar items - for example, the hours worked by all employees. without arrays you can store just one at a time or store them in separate variables: double hours_1, hours_2, hours_3, ... With arrays, you can store them all in a single variable. One variable declaration double hours[10]; Allocates a group of 10 type double storage cells. Storage model for double hours[10] hours[0] hours[1] hours[2] hours[3] hours[4] ... hours[9] ? ? ? ? ? ? Each storage location name consists of 2 parts: the variable name (hours) and the subscript (0 through 9) in brackets. hours[0] is the name of the first storage location, not hours[1]. The last storage location is hours[9], not hours[10]. We use the names shown above to reference the individual storage locations. hours[0] = 30; hours[1] = 20; hours[2] = 15.5; hours[3] = 40.0; hours[4] = hours[3] + 10; hours[9] = (hours[0] + hours[3]) / 2; hours[0] hours[1] hours[2] hours[3] hours[4] ... hours[9] 30.0 20.0 15.5 40.0 50.0 35.0 printf("You worked %f hours in week %d\n", hours[0], 1); printf("You worked %f hours in week %d\n", hours[1], 2); You worked 30.00000 hours in week 1 You worked 10.00000 hours in week 2 More flexible way to declare arrays: #define hours_size 10 int main() { double hours[hours_size]; Also creates an array with 10 storage locations for array elements. Can change this to 20 elements easily - just change the constant definition: #define hours_size 20 We use for loops often to process arrays. Enables us to access all array elements in sequence and to process all the elements in a consistent and uniform manner. #define hours_size 10 int main() { double hours[hours_size]; int I; /* should be i but stupid word-processor capitalizes it automatically! */ /* Set all array elements to 0 */ for (I = 0; I < hours_size; I++) hours[I] = 0; /* What does this one do? */ for (I = 0; I < hours_size; I++) hours[I] = 10 * i; /* answer - sets array elements to 0, 10, 20, ... */ /* This one is more complicated - see if you can figure it out. */ hours[0] = 20; for (i = 1; i < hours_size; i++) hours[i] = hours[i-1] + 5; /* answer - sets array elements to 20, 25, 30, ... */ /* print the array subscripts and element values */ printf("%s %s\n", "I", "hours[i]"); for (i = 0; i < hours_size; i++) printf("%d %f\n", i, hours[i]); return 0; } i hours[i] 0 20.000000 1 25.000000 2 30.000000 3 35.000000 4 40.000000 5 45.000000 6 50.000000 7 55.000000 8 60.000000 9 65.000000 Reading data into an array: Read the values one at a time. for (int I = 0; I < hours_size; I++) { printf("Enter value for hours[%d]: ", i); scanf("%lf", &hours[I]); } Enter value for hours[0]: 10 Enter value for hours[1]: 8 Enter value for hours[2]: 16 Enter value for hours[3]: 5 Enter value for hours[4]: 50 Enter value for hours[5]: 50 Enter value for hours[6]: 60 Enter value for hours[7]: 70 Enter value for hours[8]: 80 Enter value for hours[9]: 90 Class exercise: Write a loop that finds the sum of all values in array hours. Also write a statement to find the average of these values. Hint: You want to be able to find hours[0] + hours[1] + ... hours[hours_size - 1] without writing all the array elements as we do above. Class exercise: Write a loop that finds the sum of all values in array hours. Also write a statement to find the average of these values. Hint: You want to be able to find hours[0] + hours[1] + ... hours[hours_size - 1] without writing all the array elements as we do above. double sum, average; sum = 0; for (I = 0; I < hours_size; I++) sum += hours[I]; /* sum = sum + hours[I]; */ average = sum / hours_size;