LA is an array with 15 elements .write a c++ program that rearrange the elements in LA in ascending order and delete the largest element in the array .
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void print_array(int arr[], int n) {
for (int i=0; i<n; ++i) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int LA[15];
// Init array with random numbers
srand(time(nullptr));
for (int i=0; i<15; ++i) {
LA[i] = rand() % 90 + 10;
}
cout << "Unsorted array:" << endl;
print_array(LA, 15);
// Insertion sort
for (int i=0; i<14; ++i) {
int i_min = i;
for (int j=i+1; j<15; ++j) {
if (LA[j] < LA[i_min]) {
i_min = j;
}
}
int tmp = LA[i];
LA[i] = LA[i_min];
LA[i_min] = tmp;
}
// Delete the largest element (set it equals zero)
LA[14] = 0;
cout << "Sorted array:" << endl;
print_array(LA, 14);
}
Comments
Leave a comment