1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Mkey [24]
3 years ago
9

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 first item (root,[1]) in the heap three. it does not violate any other item. So, no // 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. The 61 violated the heap.
Exp-2:
Enter a number: 100
Enter a number: 95 1/ 95 < 100, OK
Enter a number: 76 // 76 < 100, OK
Enter a number: 58 // 58 < 95, OK
Enter a number: 66 1/ 66 < 95, OK
Enter a number: 58 // 58 < 76, OK
Enter a number: 66 // 66 < 76, OK
Exp-3:
Enter a number: -15
Enter a number: -5
-5 violated the heap.
Exp-4:
Enter a number: 45
Enter a number: 0
Enter a number: 55
55 violated the heap.
Computers and Technology
1 answer:
mafiozo [28]3 years ago
5 0

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.
You might be interested in
A(n) __________ is a common list operation used in programming. Its purpose is to iterate through a list of items, one item at a
34kurt
Linear search

You implement this algorithm by iterating over each item, and checking if the item matches what you are searching for.

It is linear because it takes a linear amount of time to search for an item.
5 0
2 years ago
Which statement is true regarding Artificial Intelligence (AI)?
Effectus [21]
Data is the fundamental reason AI succeeds or fails.
This statement is true regarding Artificial Intelligence (AI)
6 0
2 years ago
What is the name of html command? ​
Makovka662 [10]

Here are some.

  • <html></html> This is the root element tag. ...
  • <head></head> ...
  • <title></title> ...
  • <body></body> ...
  • <h1></h1> ...
  • <p></p> ...
  • <a></a> ...
  • <img></img>
<h2>hope it helps.</h2><h2>stay safe healthy and happy....</h2>
3 0
2 years ago
The exercise instructions here are LONG -- please read them all carefully. If you see an internal scrollbar to the right of thes
AnnyKZ [126]

Answer:

The following codes are:

int monthOfYear=11;

 // integer type variable is initialized

long companyRevenue=5666777;

 // long type variable is initialized

int firstClassTicketPrice=6000;

 // integer type variable is initialized

long totalPopulation=1222333;  //// long type variable is initialized

Explanation:

Here, we define integer type variable "monthOfYear" to 11.

Then, we define long type variable "companyRevenue" to 5666777.

Then, we define integer type variable "firstClassTicketPrice" to 6000.

Then, we define long type variable "totalPopulation" to 1222333.

8 0
3 years ago
There is a huge demand for cyber security professionals. True or false
STatiana [176]
True.
Thank You!
Please mark Brainliest
5 0
3 years ago
Read 2 more answers
Other questions:
  • Putting the word int into a code will create a string variable?
    12·1 answer
  • With respect to the general classes of computers, a ________ is the most expensive and most powerful kind of computer, which is
    7·1 answer
  • Create an interactive program to use class a LightsOut class to allow a user to play a game. Each step in the game will require
    6·1 answer
  • The four key stages with regards to data visualization workflow. Select one key stage and explain it briefly about that stage.St
    10·1 answer
  • If you’re presenting in a darkened room, what text and background should you use to provide maximum readability?1) Light text on
    5·1 answer
  • A(n) ____ string contacts the data source and establishes a connection with the database using the Data Source Configuration Wiz
    5·1 answer
  • Detecta 1 problema
    5·2 answers
  • What is the main difference between a search engine and a web browser?
    6·2 answers
  • The image shows a sample group contract.
    13·2 answers
  • Identify when programmers use an Else statement.
    15·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!