Answer to Question #227046 in C++ for Ulando

Question #227046

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 .



1
Expert's answer
2021-08-17T12:52:08-0400
#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);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS