12 Replies - 1900 Views - Last Post: 14 February 2013 - 05:13 PM Rate Topic: -----

#1 zaiMAx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-February 13

translating Qbasic code to C++

Posted 12 February 2013 - 12:12 AM

hello!
i've got a code from QBasic which i need translating to C++
i've tried a somewhat code by code translation as to what i understand in C++

here is my Qbasic program

sq = 50
t1 = .1 ’
tfac = .95 
tend = .0001 
maxmc = 6
RANDOMIZE TIMER
CLS
DIM x(sq), y(sq)

FOR i = 0 TO sq - 1
x(i) = RND
y(i) = RND
NEXT i

CLS
GOSUB 1000
GOSUB 4000
l2 = lt
DO
 GOSUB 5000
 IF INKEY$ = CHR$(27) THEN END
 CLS
 GOSUB 1000
 PRINT ”"Temperature"”;
 PRINT USING ''’’##.####’’''; t1
 PRINT ''’’Energie’’'';
 PRINT USING ''’’###.#####''’’; l2
 t1 = t1 * tfac
LOOP UNTIL t1 <= tend
END ’

1000 SCREEN 2
 LINE (100, 30)-(420, 160), , B
 FOR l = 0 TO sq - 1
 CIRCLE (260 + 160 * x(l), 95 + 65 * y(l)), 3
 NEXT l
 RETURN

4000 lt = 0
 FOR i = 0 TO sq - 1
 FOR k = 0 TO i - 1
 u = 0
 r2 = (((x(i) - x(k)) ^2 + (y(i) - y(k)) ^2))
 IF r2 < .01 THEN 4020 ELSE GOTO 4010
 4010 IF r2 < .03 THEN 4030 ELSE GOTO 4100
 4020 u = 20
  GOTO 4100
 4030 u = -.001 / r2
  GOTO 4100
 4100 lt = lt + u
  NEXT k

  NEXT i
  RETURN

’'Monte Carlo
5000 FOR ntrial = 1 TO maxmc

 FOR h = 0 TO sq - 1
  a = x(h)
  B = y(h)
 x(h) = 2 * RND - 1
 y(h) = 2 * RND - 1
  GOSUB 4000
 IF lt <= l2 OR RND < EXP(-(lt - l2) / t1) THEN
 l2 = lt
 ELSE x(h) = a
 y(h) = B
 END IF
 NEXT h
 NEXT ntrial
 RETURN




here's my translation in C++ (Dev C++)

#include "QBasic.h"
#include <iostream>
#include <ctime>
using namespace std;

int main() {

int sq = 50 ; 
int t1 = .01 ;
int tfac = .95 ; 
int tend = .0001 ; 
int maxmc = 6 ; 

srand(static_cast<unsigned>(time(0))) ;
clrscr() ;
int x [ sq +1] , y [ sq +1] ;

//Start
for( int i = 0 ;i<=sq;i+=1){ - 1
x [ i ] = rand() ;
y [ i ] = rand() ;
} i

//main program
clrscr() ;
GOSUB 1000 ;
GOSUB 4000 ;
int l2 = lt ;
do {
GOSUB 5000 ;
if( inkey$() == CHR$ ( 27 ) ){ END
clrscr() ;
GOSUB 1000 ;
PRINT "TEMPERATURE" ; ;
PRINT USING " ##.#### " ; t1 ;
PRINT ; //'ENERGY'';
PRINT USING " ###.##### " ; 12 ;
t1 = t1 * tfac ;
} UNTIL t1 < = tend
END ;
//plotting
1000 //SCREEN 2
LINE ( 100 , 30 ) - ( 420 , 160 ) , , int B ;
for( int l = 0 ;l<=sq;l+=1){ - 1
CIRCLE ( 260 + 160 * x [ l ] , 95 + 65 * y [ l ] ) , 3 ;
} l
RETURN ;

//ENERGY
4000 lt = 0 ;
for( i = 0 ;i<=sq;i+=1){ - 1
for( int k = 0 ;k<=i;k+=1){ - 1
int u = 0 ;
int r2 = ( ( ( x [ i ] - x [ k ] ) ^ 2 + ( y [ i ] - y [ k ] ) ^ 2 ) ) ;
if( r2 < . int 01 ){ 4020 ELSE GOTO 4010 ;}
4010 if( r2 < . int 03 ){ 4030 ELSE GOTO 4010 ;}
4020 u = 20 ;
GOTO 4100 ;
4030 u = - . int 001 / r2 ;
GOTO 4100 ;
4100 lt = lt + u ;
} k
} i
RETURN ;

//MONTE CARLO
5000 for( int ntrial = ;ntrial<=maxmc;ntrial+=1){ maxmc

for( int h = 0 ;h<=sq;h+=1){ - 1
int a = x [ h ] ;
B = y [ h ] ;
x [ h ] = 2 * rand() - 1 ;
y [ h ] = 2 * rand() - 1 ;
GOSUB 4000 ;
if( lt < == 12 || rand() < EXP ( - ( lt - 12 ) / t1 ) ){
l2 = lt ;
ELSE x [ h ] = a ;
y [ h ] = B ;
}
} h
} ntrial
RETURN ;

return 0;
}



so someone said i should change CLS to clrscr
debugging the program tells me to
-convert int to double
-undeclared clrscr
-some identifiers
-undeclared GOSUB, inkey$, CHR$, END
should i change PRINT to cout?

Is This A Good Question/Topic? 0
  • +

Replies To: translating Qbasic code to C++

#2 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: translating Qbasic code to C++

Posted 12 February 2013 - 01:00 AM

Hi,


You would need to translate it to Win32 code as it contains

Line and Circle and you cannot emulate that in a console.

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 3848
  • View blog
  • Posts: 11,775
  • Joined: 25-December 09

Re: translating Qbasic code to C++

Posted 12 February 2013 - 07:23 AM

Is there a reason you are trying to convert this program into a C/C++ program? Do you understand the Qbasic version of the program? How familiar are you with C/C++? Without knowing, at least the basics of, both languages it will be hard to translate from one language to another.


Jim
Was This Post Helpful? 0
  • +
  • -

#4 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: translating Qbasic code to C++

Posted 12 February 2013 - 08:48 AM

It's almost always far easier to get the pseudo code to the Qbasic program, and then re-write it, if the Qbasic code is sprinkled with goto's and such.

What does the Qbasic program do?
Was This Post Helpful? 0
  • +
  • -

#5 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: translating Qbasic code to C++

Posted 12 February 2013 - 06:39 PM

@Adak,

Well,

It first sets up 50 Random numbers into the arrays x and y
1.It clears the screen
2.It draws 50 circles at various random positions on screen using x and y coordinates
3.It then does a determinisitic computation on the x and y coords.
4.It then aggregates the results.

It then goes into a loop doing 1 - 4 constantly
It also in this loop Prints the words Temperature and displays a temperature to 4 decimal places and also the words Energy (in French of all things) and displays the energy reading to 5 decimal places. It then updates the temperature according to an equation and it keeps doing this until t1 is less than or equal to tend.

Its called the Monte Carlo Method and was developed by Stanislav Ulam at the Los Alamos Labs in the 1940's (no I wasnt around then) in this implementation (a very badly written implementation) it seems to be simulating Temperature and energy in what I can only determine as either single particles or quantum dots...

Does that answer your question Adak..?

And no I wont be translating it for the OP, (sorry OP its just not interesting enough)

But I would be willing to lend a hand if the OP shows a bit of effort... :) yay !!

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1276
  • View blog
  • Posts: 4,401
  • Joined: 19-February 09

Re: translating Qbasic code to C++

Posted 12 February 2013 - 10:29 PM

QBasic to C++ translator
Was This Post Helpful? 0
  • +
  • -

#7 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: translating Qbasic code to C++

Posted 12 February 2013 - 10:47 PM

Thanks Snoopy. I saw the "Monte Carlo" reference in the code, but just WHAT the tests were designed to find, wasn't clear.

For instance, in my last Monte Carlo program, the user "threw" (virtual) random "darts" at a circle, inside a square. By using the proportion of "hits" inside the circle, and the relative area of the circle and square, you found (ever so slowly), the value of Pi.

And just when did the French become frivolous, anyway? ;)
Was This Post Helpful? 0
  • +
  • -

#8 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: translating Qbasic code to C++

Posted 12 February 2013 - 11:59 PM

View PostAdak, on 13 February 2013 - 05:47 AM, said:

And just when did the French become frivolous, anyway? ;)/>


Ah mon ami, je plaisantais seulement.

Vive La Belle France !!

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#9 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: translating Qbasic code to C++

Posted 13 February 2013 - 12:02 AM

See? That's what we get for supporting de Gaulle's return into France as the big conquering hero -- Bah!

LOL!
Was This Post Helpful? 0
  • +
  • -

#10 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: translating Qbasic code to C++

Posted 13 February 2013 - 12:35 AM

View Post#define, on 13 February 2013 - 05:29 AM, said:



I dont think that will work #define,

as the OP needs to draw 50 circles and a line.

However I suppose it may get the OP some of the way there...

Snoopy.

This post has been edited by snoopy11: 13 February 2013 - 12:35 AM

Was This Post Helpful? 0
  • +
  • -

#11 zaiMAx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-February 13

Re: translating Qbasic code to C++

Posted 14 February 2013 - 04:38 PM

View Postjimblumberg, on 12 February 2013 - 07:23 AM, said:

Is there a reason you are trying to convert this program into a C/C++ program? Do you understand the Qbasic version of the program? How familiar are you with C/C++? Without knowing, at least the basics of, both languages it will be hard to translate from one language to another.


Jim

i have a windows 7 computer but qbasic runs on DOS. i have to translate it to C/C++ to be able to run it on windows. i have a bit of understanding with the algorithm of the codes but not on all the syntax.
Was This Post Helpful? 0
  • +
  • -

#12 zaiMAx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-February 13

Re: translating Qbasic code to C++

Posted 14 February 2013 - 05:08 PM

thanks for your replies sirs. been a great help to me. my group have finally made the program run in visual C#. though i'm still trying to make it run in C/C++.
Was This Post Helpful? 0
  • +
  • -

#13 zaiMAx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-February 13

Re: translating Qbasic code to C++

Posted 14 February 2013 - 05:13 PM

the code is to do a cooling simulation on 50 molecules of van der waals gas. experimenting on varying the time factor for cooling and the number of molecules would determine the critical cooling temperature of the system. that's basically what the code aims for.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1