C++ Programming By using Binary heap
Develop a CPP program to test is an array conforms heap ordered binary tree. This program read data from cin (console) and gives an error if the last item entered violates the heap condition. Use will enter at most 7 numbers. Example runs and comments (after // ) are below. Your program does not print any comments. An output similar to Exp-3 and Exp-4 is expected.
Exp-1:
Enter a number: 65 // this is first item (root,[1]) in the heap three. it does not violate any other item. So, no violation hereEnter a number: 55 // this is second [2] number, should be less than or equal to its root ([1]) Enter a number: 56 // this is third [3] number, should be less than or equal to its root ([1]) Enter a number: 45 // this is fourth number, should be less than or equal to its root ([2]) Enter a number: 61 // this is fifth number, should be less than or equal to its root ([2]). It is not, 61 > 55. Then give an error and exit 61 violated the heap. Bye..
Exp-2:
Enter a number: 100 Enter a number: 95 // 95 < 100, OK Enter a number: 76 // 76 < 100, OK Enter a number: 58 // 58 < 95, OK Enter a number: 66 // 66 < 95, OK Enter a number: 58 // 58 < 76, OK Enter a number: 66 // 66 < 76, OK All good. Bye
Exp-3:
Enter a number: -15 Enter a number: -5 -5 violated the heap. Bye..
Exp-4:
Enter a number: 45 Enter a number: 0 Enter a number: 55 55 violated the heap. Bye..
Code:-
#include<iostream>
using namespace std;
int main()
{
int i,arr[7],k=0,l=1,j;
/* here atmost 7 entries are considered as per
question */
int n=7;
/* loop for 7 times of 7 entries */
for(i=0;i<n;i++)
{
/* console input of number
*/
cout<<"Enter a number :
";
/* insert number in array
*/
cin>>arr[i];
if(l<=2 &&
i>0)
{
k++;
}
if(l>2 &&
i>0)
{
l=l-2;
}
/* index of the root of sub
trees' */
j=i-k;
/* if it supports heap
condition
means i th element will be
greater than 2*i+1 and 2*i+2 th element */
if(i>0 &&
arr[j]>arr[i])
{
l++;
}
/*if it violate heap rule
means i th element will be smaller than 2*i+1 or 2*i+2 th element
*/
if(i>0 &&
arr[j]<arr[i])
{
/*
print message and break loop */
cout<<endl<<arr[i]<<" violate the heap.
Bye..";
break;
}
}
/* if i is 7 means full loop run, so it does not
violate heap rule */
if(i==7)
{
cout<<endl<<"All
good. Bye";
}
return 0;
}
Code Screenshot:-
![#include<iostream> using namespace std; int main() int i, arr[7], k=0,1=1,j; /* here atmost 7 entries are considered as per q](http://img.homeworklib.com/questions/e4d6e440-0507-11eb-b6b8-85b790723e72.png?x-oss-process=image/resize,w_560)
Outputs:-




Please UPVOTE thank you...!!!
C++ Programming By using Binary heap Develop a CPP program to test is an array conforms...
Language C++
Instructions Develop a CPP program to test is an array conforms heap ordered binary tree. This program read data from cin (console) and gives an error if the last item entered violates the heap condition. Use will enter at most 7 numbers. Example runs and comments (after //) are below. Your program does not print any comments. An output similar to Exp-3 and Exp-4 is expected. Exp-1: Enter a number: 65 Enter a number: 56 // this is...
Instructions Develop a CPP program to test is an array conforms heap ordered binary tree. This program 7 numbers. Example runs and comments (after // ) are below. Your program does not prin Exp-1: Enter a number: 65 // this is first item (root, [1]) in the heap three. it does not violate a Enter a number: 56 // this is third (3) number, should be less than or equal to its root ([1 Enter a number: 45 // this...
Write a Java program, In this project, you are going to build a max-heap using array representation. In particular, your program should: • Implement two methods of building a max-heap. o Using sequential insertions (its time complexity: ?(?????), by successively applying the regular add method). o Using the optimal method (its time complexity: ?(?), the “smart” way we learned in class). For both methods, your implementations need to keep track of how many swaps (swapping parent and child) are required...
Create a c++ program which: 1. Displays a menu: 1) Decimal to Binary • 2) Binary to Decimal • 3) Decimal to Hex • 4) Hex to Decimal • 9) Exit Program 2. For Menu item #1: Ask the user for a Decimal number. If they type a negative number, go back to step #1 1. Convert the Decimal number to binary and display it. 3. For Menu item #2: Ask the user for a binary number (1's and 0's)....
C++ programming language POINTERS Write a program that dynamically allocates an array, on the Heap, large enough to hold 200 test scores between 55 and 99 -- using a Random Number generator to populate the array. Then do the following: 1) Sort scores in ascending order. 2) List your scores in rows of ten(10) values. 3) Calculate the Mean for the distribution. 4) Calculate the Variance for the distribution. 5) Calculate the Median for the distribution. 6) Calculate the Mode...
The ExceptionLab class provided: – Creates an array of 100
elements and fills it with random numbers from 1 to 100. – It asks
the user for an index value between 0 and 99. – Prints the element
at that position. – If a number > 99 is entered by the user, the
class will abort with an ArrayIndexOutOfBoundsException • Modify
the ExceptionLab: – Add a try-catch clause which intercepts the
ArrayIndexOutOfBounds and prints the message: Index value cannot be...
Is Prime Number In this program, you will be using C++ programming constructs, such as functions. main.cpp Write a program that asks the user to enter a positive integer, and outputs a message indicating whether the integer is a prime number. If the user enters a negative integer, output an error message. isPrime Create a function called isPrime that contains one integer parameter, and returns a boolean result. If the integer input is a prime number, then this function returns...
1) Write Single-line comments for each code in programming C++ 2) Prompt the user to enter a number from 1 to 100, inclusive. If the number is not valid, meaning out of range, then indicate that the number is not valid and that they should try again. This program should test for negative numbers, which are not valid. Prompt the user to indicate that the number must be greater than 0 and that they should try again. If the number...
In this program, you will be using C++ programming constructs, such as overloaded functions. Write a program that requests 3 integers from the user and displays the largest one entered. Your program will then request 3 characters from the user and display the largest character entered. If the user enters a non-alphabetic character, your program will display an error message. You must fill in the body of main() to prompt the user for input, and call the overloaded function showBiggest()...
Write a C program to assign natural numbers 1 to 100 into a one-dimensional integer array. Display all the values in the array on the screen. For each number in the array, determine if the number contains digit 7 or is divisible by 7. Display all those numbers on the screen. Original array: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27...