+ 1

How to code this in C++?

Let arr be an array of 12 integers. Write a program that first fills the array with up to 20 input values and then finds and displays both the subscript of the largest item in arr and the value of the largest number.

4th Dec 2017, 5:23 AM
Khalil Jeland Bacon
Khalil Jeland Bacon - avatar
2 Answers
+ 1
What do you mean by subscript? Is it the last digit? Or is it the index? And you need to fill an array of size 12 with 20 values? So are pointers allowed?
4th Dec 2017, 5:30 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
Here a simple way to find the maximum element's value and position: #include<iostream> #include<algorithm> using namespace std; int main() { int arr[20]; int c = 1; cout<<"Enter the array - \n"; for(int& i : arr) { cout<<"Enter Element "<<c<<" - "; cin>>i; c++; } // Range Based loop to read // Till the size of the array. int* max = max_element(arr,arr+20); // std::max_element() - // Returns an iterator to the max element. int index = distance(arr,max); // std::distance() - // Finds how many elements are // b/w the array's start and max. cout<<"The greatest element of the array is "<<*max<<" and it was found at the "<<index<<"th position (Element Number "<<index+1<<")"<<endl; // We need to dereference the pointer // to the max element to get its value. }
4th Dec 2017, 5:44 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar