Question

C++, I am not able to get this program to function correctly, ie not launching at...

C++, I am not able to get this program to function correctly, ie not launching at all

#include <iostream>
#include <stdio.h>

int insert(int num, int location)
{
int parentnode;
while (location > 0)
{
parentnode =(location - 1)/2;
if (num <= array[parentnode])
{
array[location] = num;
return;
}
array[location] = array[parentnode];
location = parentnode;
}
array[0] = num;
}

int array[100], n;
using namespace std;
main()
{
int choice, num;
n = 0;
while(1)
{
printf("1.Insert the element \n");
printf("2.Delete the element \n");
printf("3.Display all elements \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the element to be inserted to the list : ");
scanf("%d", &num);
insert(num, n);
n = n + 1;
break;
case 2:
printf("Enter the elements to be deleted from the list: ");
scanf("%d", &num);
delete(num);
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice \n");
}
}
}

display()
{
int i;
if (n == 0)
{
printf("Heap is empty \n");
return;
}
for (i = 0; i < n; i++)
printf("%d ", array[i]);
printf("\n");
}


delete(int num)
{
int left, right, i, temp, parentnode;

for (i = 0; i < num; i++) {
if (num == array[i])
break;
}
if (num != array[i])
{
printf("%d not found in heap list\n", num);
return;
}
array[i] = array[n - 1];
n = n - 1;
parentnode =(i - 1) / 2;
if (array[i] > array[parentnode])
{
insert(array[i], i);
return;
}
left = 2 * i + 1;
right = 2 * i + 2;
while (right < n)
{
if (array[i] >= array[left] && array[i] >= array[right])
return;
if (array[right] <= array[left])
{
temp = array[i];
array[i] = array[left];
array[left] = temp;
i = left;
}
else
{
temp = array[i];
array[i] = array[right];
array[right] = temp;
i = right;
}
left = 2 * i + 1;
right = 2 * i + 2;
}
if (left == n - 1 && array[i])
{
temp = array[i];
array[i] = array[left];
array[left] = temp;
}
}

Homework Answers

Answer #1
#include <iostream>
#include <stdio.h>

int array[100], n;
using namespace std;
void display();
void delete_elem(int num);


void heapify(int index,int n){
    if(index >= n)return;
    

    int left = 2*index;
    int right = 2*index + 1;
    int mx_ind = index;
    if(left < n and array[left] > array[mx_ind]){
        mx_ind = left;
    }
    if(right < n and array[right] > array[mx_ind]){
        mx_ind = right;
    }
    if(mx_ind != index){
        swap(array[mx_ind],array[index]);
        heapify(mx_ind,n);
    }

}
int insert(int num, int location)
{
    int parentnode;
    while (location > 0)
    {
        parentnode =(location - 1)/2;
        if (num <= array[parentnode])
        {
            array[location] = num;
            return location;
        }
        array[location] = array[parentnode];
        location = parentnode;
    }
    array[0] = num;
    return 0;
}

int main()
{
    int choice, num;
    n = 0;
    while(1)
    {
        printf("1.Insert the element \n");
        printf("2.Delete the element \n");
        printf("3.Display all elements \n");
        printf("4.Quit \n");
        printf("Enter your choice : ");
        scanf("%d", &choice);
        switch(choice)
        {
            case 1:
                printf("Enter the element to be inserted to the list : ");
                scanf("%d", &num);
                insert(num, n);
                n = n + 1;
                break;
            case 2:
                printf("Enter the elements to be deleted from the list: ");
                scanf("%d", &num);
                delete_elem(num);
                break;
            case 3:
                display();
                break;
            case 4:
                exit(0);
            default:
            printf("Invalid choice \n");
        }
    }
}

void display()
{
    int i;
    if (n == 0)
    {
        printf("Heap is empty \n");
        return;
    }
    for (i = 0; i < n; i++)
    printf("%d ", array[i]);
    printf("\n");
}


void delete_elem(int num)
{
    int left, right, i, temp, parentnode;

    for (i = 0; i < num; i++) {
        if (num == array[i])
        break;
    }
    if (num != array[i])
    {
        printf("%d not found in heap list\n", num);
        return;
    }
    array[i] = array[n - 1];
    n = n - 1;
    parentnode =(i - 1) / 2;
    if (array[i] > array[parentnode])
    {
        insert(array[i], i);
        return;
    }
    left = 2 * i + 1;
    right = 2 * i + 2;
    while (right < n)
    {
            if (array[i] >= array[left] && array[i] >= array[right])
                return;
            if (array[right] <= array[left])
            {
                temp = array[i];
                array[i] = array[left];
                array[left] = temp;
                i = left;
            }
            else
            {
                temp = array[i];
                array[i] = array[right];
                array[right] = temp;
                i = right;
            }
            left = 2 * i + 1;
            right = 2 * i + 2;
    }
    if (left == n - 1 && array[i])
    {
        temp = array[i];
        array[i] = array[left];
        array[left] = temp;
    }
}

Know the answer?
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
Similar Questions
create case 4 #include <stdio.h> int main(void) { int counter; int choice; FILE *fp; char item[100];...
create case 4 #include <stdio.h> int main(void) { int counter; int choice; FILE *fp; char item[100]; while(1) { printf("Welcome to my shopping list\n\n"); printf("Main Menu:\n"); printf("1. Add to list\n"); printf("2. Print List\n"); printf("3. Delete List\n"); printf("4. Remove an item from the List\n"); printf("5. Exit\n\n"); scanf("%i", &choice); switch(choice) { case 1: //add to list //get the input from the user printf("Enter item: "); scanf("%s", item); //open the file fp = fopen("list.txt","a"); //write to the file fprintf(fp, "\n%s", item); //close the file...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
I am having problem for the splitStrange function, and here is the feedback I get from...
I am having problem for the splitStrange function, and here is the feedback I get from the professor, but I am not sure how to do it.... Inside the loop, if the odd element is the first in the list(list_head) then you just call remove(); But if it's just another element in the list you need to delete it differently Like with using temp and pointers and delete //Strange.h template <typename ElementType> bool strange(const ElementType &element) { return true; }...
I am trying to write a program in C language but keep running into errors. Any...
I am trying to write a program in C language but keep running into errors. Any help would be awesome. here is my code I have so far. #include <stdio.h> #include <conio.h> #include <string.h> int main(){    int lenght, i = 0, state = 0;    char line[100];    printf("enter the string for checking of comment line: \n");    gets(line);    while(i < strline(line)){        switch(state){            case 0: if (line[i] == '/'){               ...
Run the below code and shift binary left and right and understand the output. #include <stdio.h>...
Run the below code and shift binary left and right and understand the output. #include <stdio.h> int main() {     int num=212, i;     for (i=0; i<=2; i++)         printf("Right shift by %d: %d\n", i, num>>i);      printf("\n");      for (i=0; i<=2; i++)         printf("Left shift by %d: %d\n", i, num<<i);             return 0; } Output:
i want to complete this code to insert a new node in the middle of list...
i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code #include <iostream> #include <stdlib.h> using namespace std ; struct Node{                int data;                Node *link ;}; struct Node *head=NULL, *tail=NULL; /* pointers to Node*/ void InsertFront(); void InsertRear(); void DeleteFront(); void DeleteRear(); int main(){                int choice;                do{                               cout << "1:...
Explain this code function by function in detail int menu() {   int choice;   do {     system("cls");...
Explain this code function by function in detail int menu() {   int choice;   do {     system("cls");     printf("1-Insurence Plan Subscription\n");     printf("2-Claim Processing\n");     printf("3-Accounts Information\n");     printf("4-Searching Functionalities\n");     printf("5-Exit\n");     scanf("%d", &choice);   } while (choice > 5 || choice < 1);   return choice; void subscribe() system("cls");   victims[userCount].id = userCount + 1;   printf("Enter age: ");   scanf("%d", &victims[userCount].age);   printf("\n\n%-25sHealth Insurence Plan\n\n", " ");   printf("-----------------------------------------------------------------------------------------------\n");   printf("|%-30s|%-20s|%-20s|%-20s|\n", " ", "Plan 120(RM)", "Plan 150(RM)", "Plan 200(RM)");   printf("-----------------------------------------------------------------------------------------------\n");   printf("|%-30s|%-20d|%-20d|%-20d|\n", "Monthly Premium", 120, 150, 200);   printf("|%-30s|%-20d|%-20d|%-20d|\n", "Annual Claim Limit", 120000,150000,200000);   printf("|%-30s|%-20d|%-20d|%-20d|\n",...
What is wrong with my recursive Binary Search code? For some reason my 'middle' value is...
What is wrong with my recursive Binary Search code? For some reason my 'middle' value is always zero? Please fix and explain! #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int BinarySearch(const int A[],const int L,const int R,const int key) {              if (R >= 1){           int middle = L + (R-1)/2;                              if (A[middle] == key)        return A[middle];               if (A[middle] > key){...
#include<stdio.h> int main() {       /* For binary search, the array should be arranged in ascending...
#include<stdio.h> int main() {       /* For binary search, the array should be arranged in ascending or descending order */       int data[] = {1,2,3,4,5,6,7,8,9,10}; ****You might change the numbers for your program also       /* 'min' use for starting location of the array, 'max' use for end location of the array and 'mid' use for middle location of the array */       int min = 0, max = 10, search, mid = (max + min) / 2;       printf("Enter...
I need to add two trivial functions to the following code. I am having an issue...
I need to add two trivial functions to the following code. I am having an issue with what i may need to change in the current code to meet the requirements. I am already displaying the bank record but now using a function Here are some examples of what i can add to the code 1. Obtain opening (current) balance. 2. Obtain number the number of deposits. 3. Obtain number of withdrawals. 4. Obtain deposit amounts. 5. Obtain withdrawal amounts....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT