Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Basic Powerbasic > C to PB help.
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 12 Topic 257 of 266
Post > Topic >>

C to PB help.

by "Anonymous" <r1a@[EMAIL PROTECTED] > Oct 26, 2007 at 08:38 AM

Hello,

I'm trying to convert some piece of C code to PB, but have some trouble to

convert C memory manipulation code to its equivalent in PB, which results
is 
incorrect output; from the PB code.See below.

Hopefully someone could have some advice to share.
Thanks in advance!
--

#COMPILER PBCC 4.04
#COMPILE EXE
#DIM ALL
'------------------------------------------------------------------------------------------
TYPE HeapType
  length AS LONG
  node AS DWORD PTR
END TYPE
'------------------------------------------------------------------------------------------
FUNCTION fLeft(BYVAL i AS LONG)AS LONG
  SHIFT LEFT i,1
  i = i + 1
  fLeft = i
END FUNCTION
'------------------------------------------------------------------------------------------
SUB hpSwap(BYVAL i AS DWORD PTR,BYVAL j AS DWORD PTR)
  LOCAL k AS DWORD
  k = j
  k= j
  j = i
  i = k
END SUB
'------------------------------------------------------------------------------------------
SUB Fix_Heap(BYVAL hp AS HeapType PTR,BYVAL i AS LONG)
  REGISTER m AS LONG,h AS LONG
  m = fLeft(i)
  IF m < @[EMAIL PROTECTED]
 THEN
    h = m + 1
    IF h < @[EMAIL PROTECTED]
 AND @[EMAIL PROTECTED]
 > @[EMAIL PROTECTED]
 THEN m = h
    IF @[EMAIL PROTECTED]
 > @[EMAIL PROTECTED]
      hpSwap @[EMAIL PROTECTED]
      Fix_Heap hp,m
    END IF
   END IF
END SUB
'--------------------------------------------------------------------------------------------
SUB Fix_Heap_Test()
  DIM hp AS LOCAL HeapType
  LOCAL i AS LONG
  DIM ary(9) AS LOCAL LONG
  ARRAY ASSIGN ary() = 9,2,10,12,7,5,8,1,3,5
  hp.node = VARPTR(ary(1))
  hp.length = UBOUND(ary)-1
  Fix_heap VARPTR(hp),0
  FOR i = 0 TO hp.length
    ? hp.@[EMAIL PROTECTED]
 'Output    :2,10,12,7,5,8,1,3,5
  NEXT            'Should be:12,10,8, 7,5,2,1,3,5 as in the C code.
END SUB
'------------------------------------------------------------------------------------------
FUNCTION PBMAIN () AS LONG

  Fix_Heap_Test()
  WAITKEY$

END FUNCTION
'---------------------------END OF PB 
CODE---------------------------------------

//---------------------------START OF C 
CODE---------------------------------------
#include <string.h>
#include <memory.h>
#pragma hdrstop
//------------------------------------------------------------------------------------
typedef struct {
 int len;
 int node[];
}heap;

//----------------------------------------------------------------------------------------int
fLeft(int i){return (i << 1) + 1;}
//----------------------------------------------------------------------------------------
void Swap(int *i, int *j) {  int k = *j;  *j = *i;  *i =
k;}//----------------------------------------------------------------------------------------
 void Fix_heap(heap *hp, int i) {    int m = fLeft(i);    if (m < hp->len){
     int h = m + 1;      if (h < hp->len && hp->node[h] > hp->node[m]) m =
h;      if (hp->node[m] > hp->node[i]) {        Swap(hp->node+i,
hp->node+m);        Fix_heap(hp, m);      }    }  } 
//---------------------------------------------------------------------------------------
 int main(int argc, char *argv[]) {    int i;    int ary[]={9, 2, 10, 12,
7, 5, 8, 1, 3, 5};    heap *hp = (heap *) malloc(sizeof(int)*10); //9
elem. + len    memcpy(hp, ary, sizeof(ary));    Fix_heap(hp, 0);    for
(i=0; i<hp->len; i++) printf(" %i", hp->node[i]);    getch();    return 0;
 }//---------------------------------------------------------------------------




 12 Posts in Topic:
C to PB help.
"Anonymous" <  2007-10-26 08:38:38 
Re: C to PB help.
"Anonymous" <  2007-10-26 08:51:04 
Re: C to PB help.
"Judson McClendon&qu  2007-10-26 05:55:14 
Re: C to PB help.
"Judson McClendon&qu  2007-10-26 07:30:06 
Re: C to PB help.
"Anonymous" <  2007-10-26 14:58:23 
Re: C to PB help.
"Anonymous" <  2007-10-26 15:11:03 
Re: C to PB help.
"Michael Mattias&quo  2007-10-26 13:33:24 
Re: C to PB help.
"Anonymous" <  2007-10-26 15:58:19 
Re: C to PB help.
"Michael Mattias&quo  2007-10-26 14:31:46 
Re: C to PB help.
"Judson McClendon&qu  2007-10-26 15:39:29 
Re: C to PB help.
"Judson McClendon&qu  2007-10-26 09:43:04 
Re: C to PB help.
"Olav Bergesen"  2007-11-12 19:57:05 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon May 12 7:13:54 CDT 2008.