The following code is supposed to find the difference between adjacent elements of an array. Why does it fail to work?
1 /************************************************
2 * diff elements -- Print the differences *
3 * between adjacent elements of any array. *
4 ************************************************/
5 #include <iostream>
6
7 // Any array containing pairs of values.
8 // Ends with the sentinel -1
9 static int array[12] =
10 {
11 44, 8,
12 50, 33,
13 50, 32,
14 75, 39,
15 83, 33,
16 -1, -1
17 };
18
19 // Array to hold the differences
20 static int diff[6];
21
22 int main()
23 {
24 int i; // Index into the array
25
26 // Index into the diff results
27 int diff_index;
28
29 i = 0;
30 diff_index = 0;
31 // Difference adjacent elements of an array
32 while (array[i] != 0)
33 {
34 diff[diff_index++] =
35 array[i++] - array[i++];
36 }
37
38 // Print the results
39 for (i = 0; i < 6; ++i)
40 {
41 std::cout << "diff[" << i << "]= " <<
42 diff[i] << std::endl;
43 }
44 return (0);
45 }