Answer to Question #198821 in C++ for Usman Ali

Question #198821

Implement a Heap ADT using vector.

In the main function, declare an array of N (10 < N < 30) elements and fill it with random numbers between 1 and 100. Then fill the heap with these number and then remove all items by displaying them on-screen (heap sort demonstration)


1
Expert's answer
2021-05-28T03:35:28-0400
#include <iostream>
#include <vector>
#include <algorithm>
#include <iostream>
#include <random>
#include <ctime>
 
using namespace std;
 
int main()
{
	srand(time(0));
	const int size = 30;
	int A[size];
	cout << "Initial array: ";
	for (int i = 0; i < size; i++)
	{
		A[i] = rand() % 100 + 1;
		cout << A[i] << " ";
	}
	cout << endl;
	vector<int> V;
	for (int i = 0; i < size; i++)
	{
		V.push_back(A[i]);
	}
	sort(V.begin(), V.end());
	cout << "******Remove elements******" << endl;
	while(!V.empty())
	{
		cout << "Remove element: " << V[V.size() - 1] << endl;
		V.pop_back();
	}
	system("pause");
	return 0;
}

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
New on Blog