Can’ t print position of element in an array after heapify-up C++

Can someone help me with the code in c++ below?

#include <iostream>
#include <fstream>
using namespace std;


int PARENT(int i)
{
   return (i/2);
}

int Heapify_up(int arra[], int i)
{
   int j,k;
   if (i>1){
     j = PARENT(i);
     if (arra[i]<arra[j]){
        k=arra[i];
        arra[i]=arra[j];
        arra[j]=k;
        Heapify_up(arra, j);
     }
   }
 return j;
}


int main()
{

 int array3[15];
 int i,p,array_length;

 ifstream inputFile1("Heapfile.txt");

 if (inputFile1.good()){

    int current_number = 0;
    i=1;
    while (inputFile1>> current_number)
        array3[i++] = current_number;

    inputFile1.close();
}
array_length = i;

cout<<"Please, enter an integer: ";
cin>>p;
array3[array_length+1]=p;
int pos=Heapify_up(array3, array_length+1);

 for (i=1; i<15; i++){
    cout<<array3[i]<<"  ";
 }
 cout<<"The position is "<<pos;
}

Let me explain you that have an array in a txt file. After i insert a random integer and with the heapify-up algorith I'm sorting this random number to the array. I want to print the new sorted array(I' have done that) and the new position of the random element that i have entered. Any idea?

thanks in advance! P.S. I am new here and i find it somehow difficult to post my code correctly... still learning! XD



Read more here: https://stackoverflow.com/questions/64896590/can-t-print-position-of-element-in-an-array-after-heapify-up-c

Content Attribution

This content was originally published by CaptainBo at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: