tags:

views:

140

answers:

4

Using this code, the following execution yields strange results:

C 100 R W

The text file's first line defines the number of elements to read from it, and it contains a few values under 15, but every time I run this, the first value in my array is always printed out as 87 (the ASCII value for 'W'). If I change the 'W' functionality to 'X', then the first result in the array is 88.

#include <iostream>
#include <fstream>

using namespace std; 

int arrayLength;

class ELEMENT
{
 public:
  int key;
};

class HEAP
{
 public:
  int capacity;
  int size;
  ELEMENT H [];
};

HEAP initialize(int n)
{
 HEAP h;
 h.capacity = n;
 h.size = 0;
 return h;
}

void buildHeap(HEAP &h, ELEMENT *a)
{
 h.size = arrayLength;
 for (int i = 1; i <= arrayLength; i++)
 {
  h.H[i] = a[i];
 }

 for (int i = h.size/2; i >= 1; i--)
 {
  // HEAPIFY HERE
 }
}

void printHeap(HEAP &h)
{
 cout << "Capacity:\t" << h.capacity << endl;
 cout << "Size:\t\t" << h.size << endl;
 cout << "|";
 for (int i = 1; i <= h.size; i++)
 {
  cout << " ";
  cout << h.H[i].key << " |";
 }
 cout << endl;
}

int main()
{
 char c;
 int val;
 HEAP h;

 while (c != 'S')
 {
  cin >> c;
  switch (c)
  {
   case 'S':
    break;
   case 'C':
    cin >> val;
    h = initialize(val);
    break;
   case 'W':
    printHeap(h);
    break;
   case 'R':
    {
     ifstream infile;
     infile.open("HEAPinput.txt");
     infile >> arrayLength;
     ELEMENT* a = new ELEMENT[arrayLength];
     for (int i = 1; i <= arrayLength; i++)
      infile >> a[i].key;
     infile.close();
     buildHeap(h, a);
    }
    break;
  }
 }

 return 0;
}

It is being compiled using g++ on a Unix server.

EDIT: To clarify: With a text file with the following contents (space = new line): 12 9 10 11 12 8 7 6 5 4 3 2 1

The output is:

Capacity: 100

Size: 12

| 87 | 10 | 11 | 12 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |

So it's working except for that first element.

+4  A: 

Whatever you think

 ELEMENT H [];

is doing, it probably isn't. C++ does not support dynamic arrays - you need to use the std::vector class.

And BTW, C++ by convention uses UPPERCASE to name pre-processor macros and constants. You should use mixed case to name your classes.

anon
+1  A: 

It might be because when you say

cout << h.H[i].key <<

H[] is an array of ELEMENTs and the key is an int. If key was a char or cast to char in the cout statement, you'll see the char representation of the int.

Colin Desmond
+1  A: 

What Neil said. Also, arrays in C++ are zero-based. So for example your loop in main():

for (int i = 1; i <= arrayLength; i++)

Should probably be:

for (int i = 0; i < arrayLength; i++)

It could be that the algorithm for binary heap construction just happens to be simpler to implement if you use one-based arrays -- in that case, you'll need to allocate enough space:

ELEMENT* a = new ELEMENT[arrayLength + 1];    // Note the "+ 1"

Currently, the last loop iteration is writing past the end of the array.

j_random_hacker
+2  A: 

In addition to the wrong use of arrays: it would not be a bad idea to make initialize(), buildHeap(), and printHeap() member functions of heap.

Emile Vrijdags