Week04--newLecture1

1fn s2 s0 s1 doubley1 y3 3 15 array

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ned we cannot make any assump4ons about their value. A new array is not guaranteed to be ﬁlled with all 0. 13 Referencing an Array • An array is used like any other variable as long as we use the index correctly. • For example, if we wanted to set the values in the array y: #define ARRAY_LEN 8 … int i = 0; … for (i = 0; i < ARRAY_LEN; i++){ y[i] = i * i; } 14 Referencing an Array (cont’d) • Array y would now contain the values: y[0] y y[1] y[2] y[3] y[4] y[5] y[6] y[7] 0 1 4 9 16 25 36 49 • Examples of the array referencing: printf(“%d\n”, y[4]); printf(“%.1f\n”, s[2]); s[0] = s[1] + (double)y[1]; y[3] += 3; 15 Array Advantages One of the advantages of the use of an array is the ease of compu4ng the sum or product of the contents of the array. Summa4on MAX  ­ 1 ∑ xi can be wrigen using an array in a loop: i = 0 sum = 0; for( i=0; i < MAX; i++ ) { sum += x[i]; } 16 Array Advantages (cont’d) Products MAX ­1 ∏ xi can be wrigen using an array in a loop: i = 0 product = 1; for( i=0; i < MAX; i++ ) { product = product*x[i]; } 17 /* Program to Print a Table of Differences * Computes the mean and standard deviation of an arra...
View Full Document

This document was uploaded on 03/10/2014 for the course ENGINEER 2sh4 at McMaster University.

Ask a homework question - tutors are online