Answer:
Following are the code to this question:
#include<iostream>//import header file
using namespace std;
int main()//defining main method
{
int ar[7];//defining 1_D array that stores value
int i,x=0,l1=1,j; //defining integer variable
for(i=0;i<7;i++)//defining for loop for input value from user ends
{
cout<<"Enter a Number: ";//print message
cin>>ar[i];//input value in array
if(l1<=2 && i>0)//using if block that checks the array values
{
x++;//increment the value of x by 1
}
if(l1>2 && i>0)//using if block that checks the array values
{
l1=l1-2;//using l1 variable that decrases the l1 value by 2
}
j=i-x;//using j variable that holds the index of the root of the subtree
if(i>0 && ar[j]>ar[i])// use if block that checks heap condition
{
l1++; //increment the value of l1 variable
}
if(i>0 && ar[j]<ar[i])// using the if block that violate the heap rule
{
cout<<ar[i]<<" "<<"Violate the heap";//print message with value
break;//using break keyword
}
}
return 0;
}
Output:
1)
Enter a Number: -15
Enter a Number: -5
-5 Violate the heap
2)
Enter a Number: 45
Enter a Number: 0
Enter a Number: 55
55 Violate the heap
Explanation:
- In the above-given C++ language code, an array "ar" and other integer variables " i,x,l1, j" is declared, in which "i" variable used in the loop for input values from the user end.
- In this loop two, if block is defined, that checks the array values and in the first, if the block it will increment the value of x, and in the second if the block, it will decrease the l1 value by 2.
- In the next step, j variable is used that is the index of the root of the subtree. In the next step, another if block is used, that checks heap condition, that increment the value of l1 variable. In the, if block it violate the heap rule and print its values.