1: // Filename: ARRPROC.CPP
2: // Purpose: Illustrates array initialization, some additional
3: // array processing, and passing array parameters.
5: #include <iostream>
6: using namespace std;
9: int MaxArrayValue(/* in */ const int a[],
10: /* in */ int first,
11: /* in */ int last);
13: void GetArrayValuesFromUser(/* out */ int a[],
14: /* in */ int howMany);
16: void Swap(/* inout */ int& i1,
17: /* inout */ int& i2);
20: int main()
21: {
22: cout << endl;
23: cout << "\nThis program illustrates some features "
24: << "of arrays, as well as some array "
25: << "\nmanipulation and the use of arrays as "
26: << "function parameters. Study the code "
27: << "\nwhile running the program. ";
28: cout << endl << endl;
30: // Inititalizing a 10-element array with 10 values:
31: int a1[10] = {5, -6, 12, 43, 17, -3, 29, 14, 35, 4};
33: // Initializing a 10-element array with only 2 values,
34: // so the other 8 values are 0 by default.
35: int a2[10] = {-5, -6};
37: // Initializing a 6-element array with 6 values.
38: // Size of array is determined by number of values in braces.
39: int a3[] = {1, 3, 5, 7, 9, 11}; // Values increasing
41: // Initializing a 7-element array with 7 values.
42: // Size of array is determined by number of values in braces.
43: int a4[] = {26, 24, 22, 20, 16, 12, 0}; // Values decreasing
45: // Initializing a 9-element array with 9 values.
46: // Size of array is determined by number of values in braces.
47: int a5[] = {31, 24, -6, 0, -2, 13, 8, 11, 16}; // Values "at random"
49: cout << endl;
51: cout << "The maximum value in the first array is "
52: << MaxArrayValue(a1, 0, 9) << "." << endl << endl;
53: cout << "The maximum value in the second array is "
54: << MaxArrayValue(a2, 0, 9) << "." << endl << endl;
55: cout << "The maximum value in the third array is "
56: << MaxArrayValue(a3, 0, 5) << "." << endl << endl;
57: cout << "The maximum value in the fourth array is "
58: << MaxArrayValue(a4, 0, 6) << "." << endl << endl;
59: cout << "The maximum value in the fifth array is "
60: << MaxArrayValue(a5, 0, 8) << "." << endl << endl;
62: cout << "The amount of storage used by the first array is "
63: << sizeof(a1) << " bytes." << endl;
65: cout << "\nWe now enter some values into an array, "
66: << "then choose two indices and report the "
67: << "\nlargest value between those two indices (inclusive). ";
69: bool finished;
70: char response;
72: int b[15];
74: int numberOfValues;
75: int first, last;
77: do
78: {
79: cout << "\nHow many values would you "
80: << "like to put into your array? ";
81: cin >> numberOfValues; cin.ignore(80, '\n'); cout << endl;
82: GetArrayValuesFromUser(b, numberOfValues);
84: cout << "Now we will find the largest value "
85: << "in a range of your choosing. ";
86: cout << "\nEnter the first and last array positions "
87: << "you wish to examine: ";
88: cin >> first >> last; cin.ignore(80, '\n'); cout << endl;
90: cout << "The maximum value in the array between "
91: << "position " << first << " and position " << last
92: << " is " << MaxArrayValue(b, first-1, last-1) << ".\n";
94: cout << "\nWould you like to process another array? (Y/N) ";
95: cin >> response; cin.ignore(80, '\n'); cout << endl;
97: finished = (response != 'Y' && response != 'y');
99: } while (!finished);
101: cout << endl;
102: cout << "The last set of array values entered was: " << endl;
103: for (int i = 0; i < numberOfValues; i++)
104: cout << b[i] << " ";
105: cout << endl;
107: cout << "Now enter the positions of two values to swap: ";
108: cin >> first >> last; cin.ignore(80, '\n'); cout << endl;
110: // Note the array componets used as actual parameters:
111: Swap(b[first-1], b[last-1]);
113: cout << "The revised array values, with "
114: << "the two values swapped, is: " << endl;
115: for (/* int */ i = 0; i < numberOfValues; i++)
116: cout << b[i] << " ";
117: cout << endl;
119: return 0;
120: }
123: int MaxArrayValue(/* in */ const int a[],
124: /* in */ int first,
125: /* in */ int last)
126: // Pre: The array "a" has been initialized, and
127: // 0 <= first <= last <= number of elements in "a" - 1.
128: // Post: Function value is the maximum value in the array "a"
129: // between the indices "first" and "last", inclusive.
130: {
131: int i, maxVal;
133: maxVal = a[first];
134: for (i = first+1; i <= last; i++)
135: {
136: if (a[i] > maxVal)
137: maxVal = a[i];
138: }
139: return maxVal;
140: }
143: void GetArrayValuesFromUser(/* out */ int a[],
144: /* in */ int howMany)
145: // Pre: howMany contains the number of values to be entered into "a".
146: // Post: "a" contains howMany integer values entered by the user.
147: {
148: cout << "Enter " << howMany << " integer values on the "
149: << "following line and press ENTER: \n";
150: for (int i = 0; i < howMany; i++)
151: cin >> a[i];
152: cin.ignore(80, '\n'); cout << endl;
153: }
156: void Swap(/* inout */ int& i1,
157: /* inout */ int& i2)
158: // Pre: i1 and i2 have been initialized.
159: // Post: The values of i1 and i2 have been swapped.
160: {
161: int temp = i1;
162: i1 = i2;
163: i2 = temp;
164: }