Page 1 of 1

1 Replies - 112 Views - Last Post: 30 January 2018 - 04:45 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=409009&amp;s=afcf06dd3e23015ed048d20a11054790&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 crazyamerican

Reputation: 0
• Posts: 18
• Joined: 26-January 17

Posted 30 January 2018 - 04:16 PM

It is supposed to pass 4 tests; random floats, big numbers, small numbers, and random ints. It passes on the small test but none of the others. i do not know whats wrong with the code. Any help would be great.

```public static float heapAdd(float[] a) {
if (a.length == 0){
return 0;
}

float min = 0;
int min2 = 0;
float sum = 0;
int end = a.length;

if (a.length == 0) {
return 0;
}

for (int i = 0; i < a.length; i++) {
pullUp(i, a);
}

for (int j = 0; j < a.length; j++) {
if (end != 1) {
min = a[0];

if (end != 2) {
if (a[1] > a[2]) {
min2 = 2;
} else {
min2 = 1;
}
} else {
min2 = 1;
}

if (min2 > end - 1) {
sum = min + a[1];
} else {
sum = min + a[min2];
}

a[0] = sum;

if (a[min2] == a[1]) {
a[1] = a[end - 1];
} else {
a[2] = a[end - 1];
}

end--;

min = a[0];

pushDown(min2, a, end);
}
}

return a[0];// return the error-minimized sum of floats
}

private static float[] pushDown(int i, float[] a, int end) {

int Lchild = (2 * i) + 1;
int Rchild = (2 * i) + 2;

float tmp;
int min = 0;

if (Lchild > end - 1) {
return a;
} else if (Rchild > end - 1) {
min = Lchild;
}

if (min != Lchild) {
if (a[Lchild] < a[Rchild]) {
min = Lchild;
} else if (a[Rchild] < a[Lchild]) {
min = Rchild;
}
}

if (min != i) {
tmp = a[i];
a[i] = a[min];
a[min] = tmp;
return (pushDown(min, a, end));
}
return a;
}

private static float[] pullUp(int i, float[] a) {

int parent = (i - 1) / 2;

if (parent >= 0) {

float tmp;

if (a[i] < a[parent]) {
tmp = a[i];
a[i] = a[parent];
a[parent] = tmp;
return pullUp(parent, a);
}
}
return a;
}
```

Is This A Good Question/Topic? 0

Replies To: need help with heapAdd(float[] a)

#2 NormR

• D.I.C Lover

Reputation: 660
• Posts: 4,980
• Joined: 25-December 13

Re: need help with heapAdd(float[] a)

Posted 30 January 2018 - 04:45 PM

Quote

It passes on the small test but none of the others.

Please post something like program output or error messages that shows what you are talking about.