Problem with my code. Expected output is index however output is zero. int min_i
ID: 3532628 • Letter: P
Question
Problem with my code. Expected output is index however output is zero.
int min_index(int [], int);
int main()
{
int num;
int min;
cout << "How many elements? ";
cin >> num;
int *arr = new int[num];
cout << "Enter " << num << " elements: ";
for (int i=0; i<num; i++)
{
cin >> arr[i];
}
min = min_index(arr, num);
int x = std::distance(arr, std::find(arr, arr + num, min));
cout<< "Index of min element: "<< x << endl;
delete[] arr;
return 0;
}
// return index of the minimal array element
int min_index(int arr[], int size)
{
if(size == 1)
return arr[0];
size--;
if(arr[0] > arr[size])
arr[0] = arr[size];
return min_index(arr, size);
}
Explanation / Answer
Try this once This program returns the index of the array slot * whose value is minimal in the array. * * * */ #include using namespace std; int min_index(int [], int); int main() { int num; cout > num; int *arr = new int[num]; coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.