BASIC to C conversion
8 messages in this thread
Hi All, Ok…anyone wanna take a shot at converting the following BASIC
program routine into a Lattice/SAS C program? Here is the BASIC segment:
10 DIM A(9,3) 20 FOR I = 1 TO 9 30 FOR J = 1 TO 3 40 READ A 50 A(I,J)=A 60
NEXT J,I 70 DATA 2,4,0,1,3,0,2,6,0 80 DATA 1,5,7,4,6,8,3,5,9 90 DATA
4,8,0,5,7,9,6,8,0
Thanks as always. – al saveriano –
PS..I kinda managed to do a version of this with a SINGLE dimension array
using strmid and other goodies. But it wasn't exactly correct and, besides,
this is a MULTI-Dimensioned array.
The easiest way doesn't require much C programming at all:
int a[9][3] = {
{ 2,4,0,1,3,0,2,6,0 },
{ 1,5,7,4,6,8,3,5,9 },
{ 4,8,0,5,7,9,6,8,0 }
};
which just initializes an array with your data, but doesn't do anything
with it, which is what your BASIC program does, too. If you want to read
numbers from a file, or fetch them from the user when you run the program,
that would make a more useful program. But the C code for what you want to
do is almost a direct translation of the BASIC code. Are you trying to
learn C?
Hi, Thxs for the help. Yes…I've played with C for over a year and a half
now. I still have plenty of problems on occasion with 'simple' stuff like
this! 🙂 Appreciate the help. Regards, – al –
PS…I also have trouble with pointers, structures, etc., etc. :))
John,
Shouldn't the array be set up like I have it in my message? I thought
a[9][3] gives 9 arrays of size three, but you have it as three arrays of
size 9. ???
73, Kelly
They're the same thing, right? 🙂
I hope so! I'm an amateur at programming!!
73, Kelly
Al, Try the following:
int a_array[9][3] = {
{2,4,0},
{1,3,0},
{2,6,0},
{1,5,7},
{4,6,8},
{3,5,9},
{4,8,0},
{5,7,9},
{6,8,0}
};
The array will be filled with the proper values at compile time. If you
wanted to fill the array from within the program, then two loops could be
used and data pulled in from a file.
73, Kelly
Kelly, THanks for the help. Much appreciated. Reghards, – al –