Поміняти місцями min елемент з передостаннім на с++
Ответы на вопрос
#include <iostream>
using namespace std;
void zamina_array(double *arr, const int n) {
int min = 0, id = 0, temp = 0;
for (int i = 0; i< n; i++) {
cin >> arr[i];
min = arr[0];
}
for (int i = 0; i< n; i++) {
if (arr[i] < min) {
min = arr[i];
id = i;
}
}
temp = arr[id];
arr[id] = arr[n-2];
arr[n-2] = temp;
for (int i = 0; i< n; i++) cout << arr[i] << "\t";
cout << "Min = " << min;
}
int main() {
const int n = 10;
double arr[n];
zamina_array(arr, n);
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr)/sizeof(arr[0]);
// Find the minimum element
int min = arr[0];
for (int i=1; i<n; i++)
if (arr[i] < min)
min = arr[i];
// Find the index of minimum element and last element.
int min_index, last_index;
for (int i=0; i<n; i++) {
if (arr[i] == min) {
min_index = i;
} else if (i == n-1) {
last_index = n-1;
}
}
// Swap the elements using a temporary variable.
int temp = arr[min_index];
arr[min_index] = arr[last_index];
arr[last_index] = temp;
cout << "Array after swapping: " << endl;
for (int i=0; i<n; i++) {
cout << arr[i] << " " ;
} return 0;
}