In: Computer Science
Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and then find the index of the largest element in the array.
You are to write two functions, printArray() and getIndexLargest(), which are called from the main function.
printArray() outputs integers to std::cout with a space in between numbers and a newline at the end.
getIndexLargest () returns the index of the largest element in the array. Recall that indexes start at 0. If there are multiple largest elements, return the smallest index. See the main() to see how the two functions are called.
#include <iostream>
#include <string>
using namespace std;
// Implement printArray here
// Implement getElement here
// DO NOT WRITE ANYTHING BELOW THIS LINE
int main() {
int myarray[100];
cout << "Enter number of integers : ";
int n;
cin >> n;
cout << "Enter " << n << " integers"
<< endl;
for (int i = 0; i < n; i++)
cin >> myarray[i];
cout << "Contents of array : ";
printArray(myarray, n);
cout << "Output of getElement: " <<
getElement(myarray, n) << endl;
}
#include <iostream> #include <string> using namespace std; // Implement printArray here void printArray(int arr[], int n) { for (int i = 0; i < n; ++i) { cout << arr[i] << " "; } cout << endl; } // Implement getElement here int getElement(int arr[], int n) { int maxIndex = 0; for (int i = 0; i < n; ++i) { if (arr[i] > arr[maxIndex]) { maxIndex = i; } } return maxIndex; } // DO NOT WRITE ANYTHING BELOW THIS LINE int main() { int myarray[100]; cout << "Enter number of integers : "; int n; cin >> n; cout << "Enter " << n << " integers" << endl; for (int i = 0; i < n; i++) cin >> myarray[i]; cout << "Contents of array : "; printArray(myarray, n); cout << "Output of getElement: " << getElement(myarray, n) << endl; }