How to swap the maximum and last element of an array?
I want to write the program which requests natural number NN (no more than 100), further sequence from NN of various numbers, changes places of the last element and the maximum, leaving other elements without change, and deduces the received array.
Sample Input 1:
8
3 6 12 1 7 19 25 4
Sample Output 1:
3 6 12 1 7 19 4 25
This is what I tried:
#include <iostream>
using namespace std;
int main()
{
int i,arr[100],n;
cin>>n;
int imin=0;
for (int i=0; i<n; i++){cin>>arr[i];}
int max=0;
for (i=1;i<n;i++)
{
if (arr[i]>arr[max])
max=i;
int tmp=arr[max];
arr[max]=arr[n-1];
arr[n-1]=tmp;
for (i=0;i<n;i++)
cout <<arr[i]<< " ";
cout << "\n";
}
return 0;
}
It does not change places the maximum and the last. How can I do this?