Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example program
- #include <iostream>
- #include <string>
- using namespace std;
- //declare a function prototype called find_maximum with 1: integer array and 2: int parameters
- int find_maximum(int[], int);
- int main() {
- //declare variables
- int c, array[100], size, location, maximum;
- //print "Input number of elements in array" followed by a line feed
- cout << "Input number of elements in array" << endl;
- //Input variable "size" from input console
- cin >> size;
- cout << "Enter " << size << " integers" << endl;
- //use c as a counter within a loop until size to enter all elements in the array
- for(c=0; c<size; c++){
- cin >> array[c];
- }
- location = find_maximum(array, size);
- //assign maximum to the array location returned by the find_maximum function
- maximum = array[location];
- cout << "Maximum element location = " << (location+1) << " and value = " << maximum << endl;
- return 0;
- }
- int find_maximum(int a[], int n) {
- //declare c, max and index as integer types
- int c, max, index;
- //assign max as the first element in array a
- max = a[0];
- index = 0;
- for (c = 0; c < n; c++) {
- if (a[c] > max) {
- // assign index to c
- index = c;
- // assign index to the cth element in array a
- max = a[c];
- }
- }
- return index;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement