Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in C++ that asks the user to enter an array of size N and find i

ID: 3692114 • Letter: W

Question

Write a program in C++ that asks the user to enter an array of size N and find index of the largest value in that array using a function named largest_location that takes an array of integer values and an integer that tells how many integer values are in the array as arguments. The function should return as its value the index of the cell containing the largest of the values in the array. Do not forget to write the main function as well. Your main function should get the array from the user and pass it to the function largest_location. Print the index of the largest location in the main function.

Explanation / Answer

#include <iostream>
using namespace std;

int largest_location(int n[],int size)
{
    int max=-1,maxindex=-1;
    for(int i=0;i<size;i++)
    {
        if(n[i]>max)
        {
            max=n[i];
            maxindex=i;
        }
    }
    return maxindex;
}
int main()
{
    int size,n[size];
   cout << "Enter size of array: ";
   cin>>size;
   for(int i=0;i<size;i++)
   {
       cout<<"Enter element "<<i <<": ";
       cin>>n[i];
   }
   cout<<"Largest elemnet at "<<largest_location(n,size)<<endl;
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote