Answer to Question #248609 in C++ for Ali khan

Question #248609
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:48:49-0400
#include <iostream>
using namespace std;
int maxint(int *array, int size){
    int index, temp_max = INT_MIN;
    for(int i = 0; i < size; i++){
        if(array[i] > temp_max){
            temp_max = array[i];
            index = i;
        }
    }
    return index;
}
int main()
{
    int *array, size = 0;


    cout<<"Enter the size of the array: "; cin>>size;
    cout<<"Input the elements\n";
    array = new int[size];
    for(int i = 0; i < size; i++){
        cin>>array[i];
    }
    int index = maxint(array, size);
    cout<<"Largest element: "<<array[index]<<" Index: "<<index<<endl;
    return 0;
}

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