Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. // Example program
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. //declare a function prototype called find_maximum with 1: integer array and 2: int parameters
  7. int find_maximum(int[], int);
  8.  
  9. int main() {
  10.   //declare variables
  11.   int c, array[100], size, location, maximum;
  12.  
  13.   //print "Input number of elements in array" followed by a line feed
  14.   cout << "Input number of elements in array" << endl;
  15.  
  16.   //Input variable "size" from input console
  17.   cin >> size;
  18.  
  19.   cout << "Enter " << size << " integers" << endl;
  20.  
  21.   //use c as a counter within a loop until size to enter all elements in the array
  22.   for(c=0; c<size; c++){
  23.       cin >> array[c];
  24.  
  25.   }
  26.   location = find_maximum(array, size);
  27.   //assign maximum to the array location returned by the find_maximum function
  28.   maximum = array[location];
  29.  
  30.  
  31.   cout << "Maximum element location = " << (location+1) << " and value = " << maximum << endl;
  32.   return 0;
  33. }
  34.  
  35. int find_maximum(int a[], int n) {
  36.   //declare c, max and index as integer types
  37.   int c, max, index;
  38.  
  39.   //assign max as the first element in array a
  40.   max = a[0];
  41.  
  42.   index = 0;
  43.  
  44.   for (c = 0; c < n; c++) {
  45.     if (a[c] > max) {
  46.        // assign index to c
  47.        index = c;
  48.        // assign index to the cth element in array a
  49.        max = a[c];
  50.     }
  51.   }
  52.  
  53.   return index;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement