C to PB help.

C to PB help.

Post by Anonymou » Sat, 27 Oct 2007 15:38:38


ello,

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 < @hp.length THEN
h = m + 1
IF h < @hp.length AND @hp.@node[h] > @hp.@node[m] THEN m = h
IF @hp.@node[m] > @hp.@node[i]THEN
hpSwap @hp.node+i,@hp.node+m
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.@node[i] '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; }//-----------------------------------------------------------
 
 
 

C to PB help.

Post by Anonymou » Sat, 27 Oct 2007 15:51:04

he C code looks garbled in my news reader, so I'll post it one more time.
//---------------------------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; }//---------------------------------------------------------------------------"Anonymous" < XXXX@XXXXX.COM > skrev i meldingnews:gYfUi.1519$ XXXX@XXXXX.COM ...> Hello,>> I'm trying to convert some piece of C code to PB, but have some trouble toconvert C memory manipulation code to its equivalent in PB, which results isincorrect 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 < @hp.length THEN> h = m + 1> IF h < @hp.length AND @hp.@node[h] > @hp.@node[m] THEN m = h> IF @hp.@node[m] > @hp.@node[i]THEN> hpSwap @hp.node+i,@hp.node+m> 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.@node[i] 'Output :2,10,12,7,5,8,1,3,5> NEXT 'Should b
 
 
 

C to PB help.

Post by Judson McC » Sat, 27 Oct 2007 19:55:14


The C source code is probably from a ?nix system with only a LF character at the end of the line instead of CRLF. PowerBASIC Console
Compiler installs program CRLF in the Samples folder, and it will fix the file. Or search powerbasic.com for the file LF2CRLF.ZIP
which contains such a program.
--
Judson McClendon XXXX@XXXXX.COM (remove zero)
Sun Valley Systems http://www.yqcomputer.com/
"For God so loved the world that He gave His only begotten Son, that
whoever believes in Him should not perish but have everlasting life."
 
 
 

C to PB help.

Post by Judson McC » Sat, 27 Oct 2007 21:30:06

Judson McClendon" < XXXX@XXXXX.COM > wrote:

Actually, this provoked me into writing a better one. It doesn't simply change
'LF' into CRLF, which will screw up the file if it already has CRLF (e.g. you
run it twice on the same file) but 'corrects' CRLF, even if it already has CRLF
in some or all lines, or has CR instead of LF, which I have seen. You can run
this program repeatedly on any correct ASCII CRLF text file and it won't
mess it up. However, if the file has CRLF reversed as LFCR, which I've also
seen, it will change LFCR to double CRLF. For such (rare) files, modify the
code to first change LFCR to LF, or simply remove all CR's first. I don't
know of a good way to certainly recognize such files at runtime. Even if you
scanned for the first CR and checked for an LF before it, you wouldn't know
for certain that it wasn't a ?nix LF file that happened to have a CR, or a CR
format that happened to have a LF, both of which I have seen (it's fairly
common to see CRLF followed by one or more LF's or CR's when the file
is the redirected output of a program). But the following program will work
for LF-only, CR-only, CRLF or mixed formats, changing them to CRLF. It
uses the overall best algorithm for CRLF correction.

'
' **************************************************
' * *
' * FixCRLF.bas *
' * *
' * Convert files to proper CRLF format *
' * *
' * Wildcards and drive/path may be used *
' * *
' * Compile using PB/CC *
' * *
' * Judson D. McClendon *
' * Sun Valley Systems *
' * 4522 Shadow Ridge Pkwy *
' * Pinson, AL 35126-2192 *
' * 205-680-0460 *
' * *
' **************************************************
'
#COMPILE EXE
#DIM ALL
#INCLUDE "WIN32API.INC"

DECLARE SUB ParseFiles(ParamStr AS STRING, FileTab() AS STRING, FileEnd AS LONG)
DECLARE SUB ExpandWildcard(WildCard AS STRING, FileTab() AS STRING, FileEnd AS LONG)
DECLARE SUB ParseParams(ParamStr AS STRING, ParamTab() AS STRING, ParamEnd AS LONG)

FUNCTION PBMAIN () AS LONG
DIM FileTab(10) AS STRING
DIM FileEnd AS LONG
DIM FilePtr AS LONG
DIM TempParams AS STRING
DIM FileName AS STRING
DIM FileText AS STRING

TempParams = TRIM$(COMMAND$)
IF (TempParams = "/?" OR TempParams = "-?") THEN
STDOUT "FixCRLF: Convert files to proper CRLF format"
STDOUT " Usage: FixCRLF filespec [filespec [filespec ...]]"
STDOUT " Wildcards and drive/path may be used"
EXIT FUNCTION
END IF

ParseFiles(TempParams, FileTab(), FileEnd)

IF (FileEnd = 0) THEN
STDOUT "No files found"
EXIT FUNCTION
END IF

FOR FilePtr = 1 TO FileEnd
FileName = DIR$(FileTab(FilePtr),16)
IF (FileName = "") THEN
STDOUT $DQ & FileTab(FilePtr) & $DQ & " not found"
ITERATE FOR
END IF
IF ((GETATTR(FileTab(FilePtr)) AND 16) <> 0) THEN
STDOUT $DQ & FileTab(FilePtr) & $DQ & " is a folder
 
 
 

C to PB help.

Post by Anonymou » Sat, 27 Oct 2007 21:58:23

Done so!
//---------------------------START OF C
CODE---------------------------------------

#include <string.h>

#include <memory.h>

#pragma hdrstop

//------------------------------------------------------------------------------------

typedef struct {

int len;

int node[];

}heap;
 
 
 

C to PB help.

Post by Anonymou » Sat, 27 Oct 2007 22:11:03

One more time. This time I loaded the file in UltraEdit and saved it in
ANSI/ASCII code DOS format.
//---------------------------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;
}
//---------------------------------------------------------------------------

"Judson McClendon" < XXXX@XXXXX.COM > skrev i melding
 
 
 

C to PB help.

Post by Michael Ma » Sat, 27 Oct 2007 22:33:24


Looks like a quicksort, or at least some kind of sort.

Which begs the question: Wouldn't ARRAY SORT be a hell of a lot easier?

--
Michael C. Mattias
Tal Systems Inc.
Racine WI
XXXX@XXXXX.COM
 
 
 

C to PB help.

Post by Anonymou » Sat, 27 Oct 2007 22:58:19

How can ARRAY SORT sort from 2,10,12,7,5,8,1,3,5 to12,10,8, 7,5,2,1,3,5? As
you can see the table isn't sorted; neither in ascending nor descending
order. At least not the whole part of it.It would be very hard for ARRAY
SORT to find which item in the table should be moved; in accordance with how
a heap data structure should be organized.

Secondly I'm currently studying Algorithms and Datastructures at the moment,
and this code is about the heap structures, as you probably spotted.
PB is just a tool in this study, not the target. I don't think ARRAY SORT
would be just as efficient here.Direct memory manipulation, as in the C
code, would be much more preferable.

"Michael Mattias" < XXXX@XXXXX.COM > skrev i melding
 
 
 

C to PB help.

Post by Michael Ma » Sat, 27 Oct 2007 23:31:46


Well, it looked like some kind of sort. (I am not a C programmer).


You don't think ARRAY SORT would be as efficient as direct memory
manipulation? Um, what do you think the compiler does when your source code
reads "ARRAY SORT ..??" (Hint: the initials are "direct memory
manipulation").

Then again, using ARRAY SORT doesn't really teach you much about structures
and memory management. Of course, that's why people actually purchase
licenses for high-level language products, so they don't have to do all that
dirty work themselves.

MCM
 
 
 

C to PB help.

Post by Judson McC » Sat, 27 Oct 2007 23:43:04

Anonymous" < XXXX@XXXXX.COM > wrote:

My favorite books on algorithms & data structures are "Algorithms" by Robert
Sedgewick and "Algorithms + Data Structures = Programs" by Niklaus Wirth.
But "Practical Algorithms for Programmers" by Binstock & Rex and
"Algorithms in C," also by Sedgewick are also very good.

The following is a sample program that installs with QuickBASIC 4.5, and there
is no copyright notice, so I'm posting it. It demonstrates several sort algorithms,
including Heap Sort, and runs under QBASIC. I'm not sure how good the
implementation of the algorithms is, because I have a Shell Sort algorithm that's
faster than this one. But it should fairly easy to convert the functional stuff to
PowerBASIC. Surely easier than C. :-)
--
Judson McClendon XXXX@XXXXX.COM (remove zero)
Sun Valley Systems http://sunvaley.com
"For God so loved the world that He gave His only begotten Son, that
whoever believes in Him should not perish but have everlasting life."

' SORTDEMO
' This program graphically demonstrates six common sorting algorithms. It
' prints 25 or 43 horizontal bars, all of different lengths and all in random
' order, then sorts the bars from smallest to longest.
'
' The program also uses SOUND statements to generate different pitches,
' depending on the location of the bar being printed. Note that the SOUND
' statements delay the speed of each sorting algorithm so you can follow
' the progress of the sort. Therefore, the times shown are for comparison
' only. They are not an accurate measure of sort speed.
'
' If you use these sorting routines in your own programs, you may notice
' a difference in their relative speeds (for example, the exchange
' sort may be faster than the shell sort) depending on the number of
' elements to be sorted and how "scrambled" they are to begin with.

DEFINT A-Z ' Default type integer.

' Declare FUNCTION and SUB procedures, and the number and type of arguments:
DECLARE FUNCTION RandInt% (lower, Upper)

DECLARE SUB BoxInit ()
DECLARE SUB BubbleSort ()
DECLARE SUB CheckScreen ()
DECLARE SUB DrawFrame (TopSide, BottomSide, LeftSide, RightSide)
DECLARE SUB ElapsedTime (CurrentRow)
DECLARE SUB ExchangeSort ()
DECLARE SUB HeapSort ()
DECLARE SUB Initialize ()
DECLARE SUB InsertionSort ()
DECLARE SUB PercolateDown (MaxLevel)
DECLARE SUB PercolateUp (MaxLevel)
DECLARE SUB PrintOneBar (Row)
DECLARE SUB QuickSort (Low, High)
DECLARE SUB Reinitialize ()
DECLARE SUB ShellSort ()
DECLARE SUB SortMenu ()
DECLARE SUB SwapBars (Row1, Row2)
DECLARE SUB ToggleSound (Row, Column)

' Define the data type used to hold the information for each colored bar:
TYPE SortType
Length AS INTEGER ' Bar length (the element compared
' in the different sorts)
ColorVal AS INTEGER ' Bar color
BarString AS STRING * 43 ' The bar (a string of 43 characters)
END TYPE

' Declare global constants:
CONST FALSE = 0, TRUE = NOT FALSE, LEFTCOLUMN = 49
CONST NUMOPTIONS = 11, NUMSORTS = 6

' Declare global variables, and allocate storage space for them. SortArray
' and SortBackup are both arrays of the data type SortType defined above:
DIM SHARED SortArray(1 TO 43) AS SortType, SortBackup(1 TO 43) AS SortType
DIM SHARED OptionTitle(1 TO NUMOPTIONS) AS STRING * 12
DIM SHARED StartTime AS SINGLE
DIM SHARED Foreground, Background,
 
 
 

C to PB help.

Post by Judson McC » Sun, 28 Oct 2007 05:39:29


He is studying sorting algorithms (algorithms & data structures), so using
array sort wouldn't help with that. :-)
--
Judson McClendon XXXX@XXXXX.COM (remove zero)
Sun Valley Systems http://www.yqcomputer.com/
"For God so loved the world that He gave His only begotten Son, that
whoever believes in Him should not perish but have everlasting life."
 
 
 

C to PB help.

Post by Olav Berge » Wed, 14 Nov 2007 03:57:05

hange the line hpSwap @hp.node+i,@hp.node+m to hpSwap
@hp.node+(i*4),@hp.node+(m*4). It looks like that Powerbasic does not
understand how to do pointer arithmetic in the way C does it.

And change the lines in hpSwap to:
k = @j
@j = @i
@i = k
Olaf B

"Anonymous" < XXXX@XXXXX.COM > skrev i melding
news:gYfUi.1519$ XXXX@XXXXX.COM ...