Write even and odd array numbers to file

New to FreeBASIC? Post your questions here.
Post Reply
ganache
Posts: 47
Joined: Aug 04, 2016 9:25

Write even and odd array numbers to file

Post by ganache »

Code: Select all

  'Program uses a random array and stores even and odd numbers in different files. (FreeBASIC)
  Dim As Integer n, i, evennum, oddnum
  input "Enter the no. of values in array=";n
  Dim As Integer arr(n)
  Randomize Timer
  for i=1 to n
  arr(i)=INT(RND(1) * 20) + 1  ' limits the numbers to 1 to 20
  next i
  print
  for i=1 to n
  print arr(i);" ";
  next i

  Open "even.txt" For Output As #1
  for i=1 to n
  if (arr(i) mod 2)=0 then
  evennum=arr(i)
  print #1, evennum;" ";
  end if
  next i
  close #1

  Open "odd.txt" For Output As #2
  for i=1 to n 
  if (arr(i) mod 2) <> 0 then
  oddnum=arr(i)
  print #2, oddnum;" ";
  end if
  next i
  close #2
  print

  print "Contents of file"; " even.txt"
  Open "even.txt" For Input As #3
  Dim As String s
  Do until (EOF(3))
  Line Input #3, s
  print s
  Loop
  close #3
  print

  print "Contents of file"; " odd.txt"
  Open "odd.txt" For Input As #4
  Dim As String c
  Do until (EOF(4))
  Line Input #4, c
  print c
  Loop
  close #4
  Sleep

fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Write even and odd array numbers to file

Post by fxm »

You can simultaneously write to the two open files at the same time:

Code: Select all

'Program uses a random array and stores even and odd numbers in different files. (FreeBASIC)

Dim As Integer n
input "Enter the no. of values in array=";n

Dim As Integer arr(n)
Randomize Timer
for i as integer=1 to n
  arr(i)=INT(RND(1) * 20) + 1  ' limits the numbers to 1 to 20
  print arr(i);" ";
next i
print

Open "even.txt" For Output As #1
Open "odd.txt" For Output As #2
for i as integer=1 to n
  if (arr(i) mod 2)=0 then
    print #1, arr(i);" ";
  else
    print #2, arr(i);" ";
  end if
next i
close #2
close #1

print
print "Contents of file"; " even.txt"
Open "even.txt" For Input As #3
Dim As String s
Do until (EOF(3))
  Line Input #3, s
  print s
Loop
close #3
print

print "Contents of file"; " odd.txt"
Open "odd.txt" For Input As #4
Dim As String c
Do until (EOF(4))
  Line Input #4, c
  print c
Loop
close #4

Sleep
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Write even and odd array numbers to file

Post by jj2007 »

I wonder why this doesn't work:

Code: Select all

Open "even.txt" For Output As #1
Open "odd.txt" For Output As #0
for i as integer=1 to n
    print #(arr(i) mod 2), arr(i);" ";
next i
close #0
close #1
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Write even and odd array numbers to file

Post by fxm »

The file numbers must have valid values 1 to 255 (so 0 is forbidden).
(see FREEFILE)

Code: Select all

Open "even.txt" For Output As #1
Open "odd.txt" For Output As #2
for i as integer=1 to n
  print #(arr(i) mod 2 + 1), arr(i);" ";
next i
close #2
close #1
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Write even and odd array numbers to file

Post by jj2007 »

fxm wrote:The file numbers must have valid values 1 to 255 (so 0 is forbidden).
Thanks. MasmBasic allows #0, so I was confused. The Wiki is well done btw.
Post Reply