## shamila08

Hello, dear all!
Below is heap algorithm. the problem is the output is not appear. How to fix it?

``````#include <stdio.h>
#include <iostream>
using namespace std;
int *Value;
int N;

{	int i;
for (i = 0; i< N; i++)
{ cout << Value[i] << "";
}

}
void swap(int i,int j)
{
int t;
t=Value[i];
Value[i]=Value[j];
Value[j]=t;
}
void HeapPermute(int N)
{    int i;
for (i=0;i<N;i++) {
HeapPermute(N-1);
if (N%2 == 1)
swap(0,N-1);
else
swap(i,N-1);
}
}
void initiate (int N)
{ int i;
for(i=0; i<N; i++)
{
Value[i]=i+1;
}
}

int main()
{
cout << " Enter the Size of elements ";
cin >> N;
initiate (N);
HeapPermute(N);
}``````

## Ancient Dragon 5,243

between lines 45 and 46 add a line: `cin.get();` to make the program stop before exiting.

delete line 1 -- stdio.h -- its not needed in your program.

## shamila08

still doesn't work.
the output as follows:
Enter the Size of element 4
Press any key to continue . . ..

## Salem 5,138

> int *Value;
Where does this point?

You need to allocate some space in your initialise function.

## shamila08

i declare it as follows:

``````#include <iostream>
using namespace std;
int *Value;
int N;``````

## Salem 5,138

And you initialise it NOWHERE!

Back to you.

## shamila08

you mean i have to write it as follows:

``````#include <iostream>
using namespace std;
int N;

void read (int *Value ,int N)
{	int i;
for (i = 0; i< N; i++)
{ cout << Value[i] << "";
}

}
void swap(int *Value,int i,int j)
{
int t;
t= Value[i];
Value[i]=Value[j];
Value[j]=t;
}
void HeapPermute(int *Value,int N)
{    int i;
for (i=0;i<N;i++) {
HeapPermute(Value,N-1);
if (N%2 == 1)
swap(Value,0,N-1);
else
swap(Value,i,N-1);
}
}
void initiate (int *Value,int N)
{ int i;
for(i=0; i<N; i++)
{
Value[i]=i+1;
}
}

void main(int*Value)
{
cout << " Enter the Size of element ";
cin >> N;
initiate (Value, N);
HeapPermute(Value, N);
cin.get();
}``````

but still doesn't work

## stilllearning 148

Ok, first of all ... you CANNOT define your main function this way. Read this.

Its either

``````int main(){

return 0;
}``````

or

``````int main(int argc, char* argv[]){

return 0;
}``````

You need to allocate space to your int array Value and then pass it in. How else is it going to store those values you are assigning it ?

Do something like this :

``````int* Value;

Value = new int[N]; /* here N is the total number of elements in your array */``````

Do NOT forget to clean up the allocated memory by deleting it before you exit your program.

## shamila08

You mean like this:

``````int main()
{
cout << " Enter the Size of element ";
cin >> N;
if (N >0 && N<=100){
int *Value = new int [N];
initiate (Value, N);
HeapPermute(Value, N);
delete [] Value;
}
}``````

## stilllearning 148

Yeah, that should work. You probably also want to throw some kind of an error if N is out of bounds. And don't forget the "return 0" at the end of main().