Question

C++ Programming By using Binary heap Develop a CPP program to test is an array conforms...

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..
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Outputs:-

C:\Users\Siva Kumar\Desktop\1.exe Enter a number : 65 Enter a number : 56 Enter a number : 45 Enter a number : 61 61 violateC:\Users\Siva Kumar\Desktop\1.exe Enter a number : 100 Enter a number : 95 Enter a number : 76 Enter a number : 58 Enter a nuC:\Users\Siva Kumar\Desktop\1.exe Enter a number : -15 Enter a number : -5 -5 violate the heap. Bye.. Process exited after 13C:\Users\Siva Kumar\Desktop\1.exe Enter a number : 45 Enter a number : 0 Enter a number : 55 55 violate the heap. Bye.. Proce

Please UPVOTE thank you...!!!

Add a comment
Know the answer?
Add Answer to:
C++ Programming By using Binary heap Develop a CPP program to test is an array conforms...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Language C++ Instructions Develop a CPP program to test is an array conforms heap ordered binary...

    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...

    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...

    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...

  • The ExceptionLab class provided: – Creates an array of 100 elements and fills it with random...

    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...

  • PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores...

    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....

  • Write a program that first gets a list of integers from the input and adds them...

    Write a program that first gets a list of integers from the input and adds them to an array. The input begins with an integer indicating the number of integers that follow (Your program should work for any size of the array). Then, get the last value from the input, and output all integers less than or equal to that value by iterating through the array. Ex: If the input is: Enter the number of integers: 5 Enter integer 1:...

  • Write a multithreaded C program that outputs prime numbers. This program should work as follows: the...

    Write a multithreaded C program that outputs prime numbers. This program should work as follows: the user will run the program and will enter a number on the command line. The program will then create a separate thread that outputs all the prime numbers less than or equal to the number entered by the user. ADD  "comments" to the source code please and a screenshot of the output with steps on how to compile

  • Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D...

    Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D array of integers which is 10x10. 2. Prints out the contents of the 2D array after assigning the data to make sure correct data was assigned. 3. Figures out and prints out the square root of the sum of ALL the elements in the 2D array. 4. Figures out and prints out the average of ALL THE ELEMENTS in the 2D array. 5. Figures...

  • Write a C++ program using "for" loop to allow user to guess a certain number say...

    Write a C++ program using "for" loop to allow user to guess a certain number say 55. The loop will end after 5 guesses or if he guesses the right number, which ever comes first. If user enters a number greater than 55, your program should print "You entered a bigger number". If user enters a number smaller than 55, your program should print "You entered a smaller number". If user enters 55 in less than 5 attempt, your program...

  • Language = c++ Write a program to find the number of comparisons using the binary search...

    Language = c++ Write a program to find the number of comparisons using the binary search and sequential search algorithms as follows: o Suppose list is an array of 1000 elements. o Use a random number generator to fill the list. o Use the function insertOrd to initially insert all the elements in the list. o You may use the following function to fill the list: void fill(orderedArrayListType& list) {       int seed = 47; int multiplier = 2743;                                ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT