In: Computer Science
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array.
Please complete this in C++, using pass by reference
#include <iostream>
using namespace std;
void findindex(int ptr_array[],int size,int var1,int var2)
{
int Max,Min;
Min = Max =ptr_array[0];
int max_index_val = 0,min_index_val=0;
//Finding The Minimum and Maximum index value in Array
for(int i=0;i<size;i++)
{
if(ptr_array[i]>Max)
max_index_val = i;
else if(ptr_array[i]<Min)
min_index_val = i;
}
//Inserting the val1 and val2 in desired index location
ptr_array[max_index_val] = var2;
ptr_array[min_index_val] = var1;
}
int main()
{
int array[] = {1,2,3,4,5,6,7,8,9};
int size = sizeof(array)/sizeof(array[0]); //Calculating the Size
of array
int var1,var2;
cout<<"Enter the var1 value to Insert at lowest value
index:";
cin>>var1;
cout<<"Enter the var2 value to Insert at lowest value
index:";
cin>>var2;
cout<<"======Before Function call elements in
array======\n";
for(int i =0;i<size;i++)
cout<<array[i]<<" ";
findindex(array,size,var1,var2);
cout<<"\n======After Function call elements in
array======\n";
for(int i =0;i<size;i++)
cout<<array[i]<<" ";
return 0;
}