Complete code of soution in c++ Programming language:-
#include<bits/stdc++.h>
using namespace std;
// Heapify function to reserve the properties of a heap after
adding a new element.
void heapify(int *heap, int n, int i)
{
int l = 2*i + 1;
int h = 2*i + 2;
int j = i;
if(l < n && heap[i] < heap[l])
{
i = l;
}
if(h < n && heap[i] < heap[h])
{
i = h;
}
if(i != j)
{
int temp = heap[i];
heap[i] = heap[j];
heap[j] = temp;
heapify(heap, n, i);
}
}
// Building max Heap using maxHeap function.
void maxHeap(int *heap, int n)
{
for(int i = (n-1)/2; i >= 0; i--)
{
heapify(heap, n, i);
}
}
// This function will generate new 100 elements and add them to
the heap.
// Whenever needed, it doubles the heap size.
int *grow(int *heap, int *n, int *el)
{
// Condition checks if heap can add 100 new elements
with current size, if not than double the current heap size.
if(*el + 100 > *n)
{
printf("\nCurretn size of heap:
%d\n", *n);
int x = (*n);
*n = (2*x);
int *arr = new int[2*x];
for(int i = 0; i < x; i++)
{
arr[i] =
heap[i];
}
heap = arr;
printf("Size of heap after
doubling: %d\n\n", *n);
}
// Generating 100 new elements and addign them into
heap.
int x = *el;
for(int i = x; i < (x+100); i++)
{
int y = rand();
heap[i] = (y%2000 + 1);
cout << heap[i] << "
";
if((i+1)%20 == 0)
{
cout <<
'\n';
}
}
*el += 100;
maxHeap(heap, *el);
return heap;
}
// printing the heap elements int increasing order.
void print(int *heap, int n)
{
cout << "\n Heap Elements :\n";
int h = n-1;
maxHeap(heap, n);
while(h >= 0)
{
int temp = heap[0];
heap[0] = heap[h];
heap[h] = temp;
heapify(heap, h, 0);
h--;
}
for(int i = 0; i < n; i++)
{
cout << heap[i] << "
";
}
cout << '\n';
}
// Main function.
int main(void)
{
int *heap = new int[100];
int n = 100;
int elements = 0;
bool cond = false;
char ch, x;
cout << "Want to generate 100 Elements (Y/N) :
\n";
cin >> ch;
if(ch == 'Y' || ch == 'y')
{
cond = true;
}
while(cond)
{
heap = grow(heap, &n,
&elements);
cout << "Want to generate
next 100 elements (Y/N) : " << '\n';
cin >> ch;
if(ch == 'N' || ch == 'n')
{
cond =
false;
}
}
print(heap, elements);
return 0;
}
Screenshot of output:-
Write a program in either C++ or Java meeting these requirements Description: In this program, we...
PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores integers and and implements a minimum priority queue. (Your program can be "hard coded" for integers - it does not need to use templates, generics, or polymorphism.) Your data structure must always store its internal data as a heap. Your toString function should return a string with the heap values as a comma separated list, also including the size of the heap as well....
Can anyone help me with my C hw? Exercise 3 You will write a new program that combines dynamically allocating an array and saving that array to a file. These are the tasks your program must perform Open an output file named "data.txt" and prepare it for writing in text mode o If the file handle is NULL, quit the program o By default, it is created and stored in the same directory as your source code file Prompt the...
please explain. Thanks in advance Dynamic Arrays Revisited (40 points) A dynamically re-sizing array is an array that is initially allocated at size 1. and every time you need to insert past the end of the array you double its size and copy elements from the old array to the new array. In class, we showed that n inserts into a dynamic array can be accomplished in O(n) using an accounting argument where we pre-paid $3 for each insert, thus...
Please write a C# program For this part of the lab, you'll be writing methods to work with 1D arrays. We will expect you to know how to create an array, and store and retrieve information from them. Continue working in the same project and “Program.cs” file. You will be generating random numbers, but the class Random is part of the standard library (don’t need any additional using statements). You should consider developing the methods for this project incrementally. In...
C++ Program Int Main First Please Write one program that does the following: 1. 1. Ask the user for ten (10) grades, and store the data in an array. Compute the average of all the grades. Print the original ten grades and the average. a. Declare an integer array with the name of “grades” of size 10 in the main function. b. Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array. i. The function will receive...
In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...
We use bluej for our JAVA program. If you can help id greatly appreciate it. Create a new Java program called Flip. Write code that creates and populates an array of size 25 with random numbers between 1-50. Print the array. Print array in reverse.
Write a complete Java program in a file caled Module1Progrom.java that reads all the tokens from a file named dota.txt that is to be found in the same directory as the running program. The program should ignore al tokens that cannot be read as an integer and read only the ones that can. After reading all of the integers, the program should print all the integers back to the screen, one per line, from the smalest to the largest. For...
Need helps on C++ attach. Thanks Pointers with classes a) A user-defined class named Timer has a constructor that takes two integer parameters to initialize hour and minute data members. Write a single C++ statement to create an object of the Timer class using dynamic memory allocation and assign it to a pointer variable named timePt r. It should call the constructor with two parameters. Use values of 10 and 20 for hour and minute respectively. b) Write the definition...
IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...