Chapter 7

Unit 7: ARRAYS The variables used so far have all had a common characteristic: Each variable could only be used to store a value at a time. ... In this chapter we will study how one-dimensional arrays are declared, initialized, stored inside a computer, and used. 7. ... h> int main( ) { int grade[5], total = 0; for (int i = 0; i <= 4; ++i) // Enter five grades { cout << "Enter a grade: "; cin >> grade[i]; } cout << " The total of the grades "; for (i = 0; i <= 4; i++) // Print five grades { cout << grade[i] << " "; total = total + grade[i]; } cout << "is " << total; return 0; } 7. ... h> int main( ) { int nums[5] = {2, 18, 1, 27, 16}; int find_max(int [], int); // function prototype cout << " The maximum value is " << find_max(nums,5); return 0; } int find_max(int vals[], int num_els) { int i, max = vals[0]; for (i = 1; i < num_els; i++) if (max < vals[i]) max = vals[i]; return max; } 7. ... For example: array declaration: int test [7] [9]; char code [26] [10]; and the following function calls: find_max(test); obtain (code); on the receiving side: int find_max (int nums[7] [9]) char obtain (char key[26] [10]) NOTE that if the array is a global one, there is no need to pass the array because the function could reference the array by its global name.

Essay Information


Words: 2204
Pages: 8.8
Rating: None

All Papers Are For Research And Reference Purposes Only. You must cite our web site as your source.