In: Computer Science
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.
ANSWER:-
int maxint(int *arr,int n)
{
int max=*(arr+0);
int index;
for(int i=0;i<n;i++)
{
if(max<*(arr+i))
{
max=*(arr+i);
index=i;
}
}
return index;
}
// FULL PROGRAM
#include <iostream>
using namespace std;
int maxint(int *arr,int n)
{
int max=*(arr+0);
int index;
for(int i=0;i<n;i++)
{
if(max<*(arr+i))
{
max=*(arr+i);
index=i;
}
}
return index;
}
int main() {
int arr[]={4,2,7,1,2};
int k=maxint(arr,5);
cout<<arr[k]; // OUTPUT 7
return 0;
}
// If any doubt please comment