Qbasic, Quickbasic/Program

Advertisement


Question
Yo what's up.

I have a program to do. We get 2 numbers to input. Lets say the numbers are 4 and 5. The product of these 2 numbers is 20.
We need to make a spiral thing or whatever that goes from 1 to 20. It should look like this.

1  2  3  4  5
14 15 16 17 6
13 20 19 18 7
12 11 10 9  8

I hope you got me. Have fun...

Answer
Hi, Rijad,

To be honest, I've never done a program to do this, but I'll see what I can do to help you out.

First, you'll need (at least) 6 variables:

DIM first AS INTEGER
DIM second AS INTEGER
REDIM matrix(0, 0) AS INTEGER
DIM size AS INTEGER
DIM x AS INTEGER
DIM y AS INTEGER

Now, to get the input,

INPUT "First number  : ", first
INPUT "Second number : ", second

' Resize the matrix
size = first * second
IF size MOD 2 <> 0 THEN
 x = FLOOR( size / 2 )
 y = size - x
ELSE
 x = size / 2
 y = x
END IF
REDIM matrix( 1 TO x, 1 TO y ) AS INTEGER

' Create a spiral with the numbers

FOR y = 1 TO UBOUND( matrix, 2 )
 FOR x = 1 TO UBOUND( matrix, 1 )
   ' The comma at the end keeps it on the same line
   PRINT matrix( x, y ),
 NEXT x
 ' This print statement moves the cursor to the beginning of the next line
 PRINT
NEXT y


Now, to do a spiral, I think I have a way of doing it, but I will let you try before I give you an answer for that.  One thing to remember is that in a spiral, when you hit a spot that already has a number, or it's the edge of the array, there is only one constant direction it can turn (for instance, in an entire spiral, there could only be a left hand turn [counter-clockwise]).

I hope that helps, and take care,
-Alex

PS: If you have trouble making the spiral, just ask a followup question about it, and I'll give you my algorithm.

Qbasic, Quickbasic

All Answers


Answers by Expert:


Ask Experts

Volunteer


Alex Barry

Expertise

I have been a qbasic programmer since 2000, creating games, minor libraries and various small programs. I have experience using interrupts, graphics, file input/output, the mouse cursor, and using libraries. I have also learned FreeBASIC, QB64, c/c++, python, lua, php and html.
I do not claim to be an absolute authority in any language, but I don't mind looking things up and learning with you.

Experience

I have been programming in *Basic dialects since 2000, as mentioned in my expertise. After a year of QBasic, I learned C and C++, and dabbled a little in ASM (I don't program in ASM - I literally just played around to see how things work). When QB64 and FreeBASIC were released, I played with those languages. At the time, FreeBASIC offered more functionality and I sided with that language for a while. During that time, while I was learning new languages, that I would see what scripting languages are available, where I took up python and lua. I started to notice a staleness to QB64's development (which I kept tabs on from time to time), and am now trying to be active in it's community and maybe in it's development in the future. Currently, I am only active on the QB64.net forums, but I appear on occasion on FreeBASIC.net's forums as well.

Education/Credentials
Highschool - 2007

©2012 About.com, a part of The New York Times Company. All rights reserved.