Answer to Question #249257 in C++ for Ali

Question #249257
Start with a program that allows the user to input a number of integers, and then stores
them in an int array. Write a function called maxint() that goes through the array,
element by element, looking for the largest one. The function should take as arguments
the address of the array and the number of elements in it, and return the index number of
the largest element. The program should call this function and then display the largest
element and its index number.
1
Expert's answer
2021-10-10T01:50:39-0400
#include <iostream>

using namespace std;

int maxint(int* arr, int length) {
    int mx = arr[0], ind = 0;
    for (int i = 0; i < length; i++) {
        if (mx<arr[i]) {
            ind = i;
            mx = arr[i];
        }
    }
    return ind;
}


int main()
{
    int n;
    cout << "Number of elements in array: ";
    cin >> n;
    int foo[n];
    cout << "Input the elements of the array:\n";
    for (int i = 0; i < n; i++) {
        cin >> foo[i];
    }
    int ans = maxint(foo, n);
    cout << "The maximum element of array: " << foo[ans] << '\n';
    cout << "The index of the maximum element: " << ans << '\n';


    return 0;
}
Example:

Number of elements in array: 4
Input the elements of the array:
1
2
3
4
The maximum element of array: 4
The index of the maximum element: 3

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS