## Function Comparation

User projects written in or related to FreeBASIC.
lrcvs
Posts: 576
Joined: Mar 06, 2008 19:27
Location: Spain

### Function Comparation

Code: Select all

`' Program comparison function' Returns   0   if all elements are equal' Returns   1   if any of the items was different' We can compare from position 1 to the end' And / or from an initial position to an intermediate / end in the arraydeclare function comparation  (i, f, a ()) as integerdim i as integer ' position initdim f as integer ' position enddim a () as integerclsx = 100redim a (x)i = 1f = xfor h = 1 to 100a (h) =   3  next hprint comparation  (i, f, a ())a (5) =   8  print comparation  (i, f, a ())sleependfunction comparation  (i, f, a ()) as integerz  =   0  y = a(i)for j = i to ffor k = i to fif a (k) <> y then z  =   1   :exit fornext kif z  =   1   then exit fornext jif z   =   0   thencomparation   =   0   'Trueelsecomparation   =   1   'Falseend ifend function`
fxm
Posts: 9993
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE
Compatible with any FreeBasic dialect:

Code: Select all

`' Program comparison function' Returns   0   if all elements are equal' Returns   1   if any of the items was different' We can compare from position 1 to the end' And / or from an initial position to an intermediate / end in the arraydeclare function comparation (byval i as integer, byval f as integer, a() as integer) as integerdim i as integer ' position initdim f as integer ' position enddim a() as integerclsdim x as integerx = 100redim a(x)i = 1f = xfor h as integer = 1 to 100  a(h) = 3 next ha(5) = 8print comparation(i, f, a ())sleependfunction comparation (byval i as integer, byval f as integer, a () as integer) as integer  comparation = 0 ' True  for j as integer = i to f    for k as integer= i to f      if a(j) <> a(k) then        comparation = 1 ' False        exit for, for      end if    next k  next jend function `
lrcvs
Posts: 576
Joined: Mar 06, 2008 19:27
Location: Spain

### Function comparation more easy

Code: Select all

`'I delete the (for/next j), now is more fast' Program comparison function more easy' Returns   0   if all elements are equal' Returns   1   if any of the items was different' We can compare from position 1 to the end' And / or from an initial position to an intermediate / end in the arraydeclare function comparation  (i, f, a ()) as integerdim i as integer ' position initdim f as integer ' position enddim a () as integerclsx = 100redim a (x)i = 1f = xfor h = 1 to 100a (h) =   3  next hprint comparation  (i, f, a ())a (5) =   8  print comparation  (i, f, a ())sleependfunction comparation  (i, f, a ()) as integerz  =   0  y = a(i)for k = i to fif a (k) <> y then z  =   1   :exit fornext kif z   =   0   thencomparation   =   0   'Trueelsecomparation   =   1   'Falseend ifend function `
fxm
Posts: 9993
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE
I thought that you worked in lang 'qb' (or 'fb')!
(It was why I essentially corrected the wording)

Now, I see that you work in lang 'fblite' (or 'deprecated').
Just a tiny improvement of the function comparation

Code: Select all

`function comparation  (i, f, a ()) as integercomparation   =   0   'Truey = a(i)for k = i + 1 to fif a (k) <> y then comparation   =   1   :exit for   'Falsenext kend function`
Last edited by fxm on May 15, 2011 12:27, edited 1 time in total.
lrcvs
Posts: 576
Joined: Mar 06, 2008 19:27
Location: Spain
Certainly, I work in Qbasic.

( I'm old school! )

Thank you very much for your observation is very interesting.!

Now modified my program.

Greetings
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Code: Select all

`for j = i to ffor k = i to fif a (k) <> y then z  =   1   :exit fornext kif z  =   1   then exit fornext j`

This outer for loop is completely unnecessary, right?
fxm
Posts: 9993
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE
lrcvs wrote:Certainly, I work in Qbasic.

( I'm old school! )

I don't understand because when I compile with #lang 'qb', I obtain a compilation error:
error 55: Type mismatch, at parameter 1 of COMPARATION()
print comparation (i, f, a ())

In fact, the problem that I found is on all the parameters of the function COMPARATION():
In 'qb' lang, these parameters (non explicitly defined) are defined by default as single.
When you call the function, you pass integer parameters!

In order to understand why you don't have error:
- What version of fbc do you use?
- What are the compilation options?
because from my point of view, you compile either in 'fblite' or in 'deprecated' lang.
(in these two langs, non explicitly defined variables are considered by default as integer).
lrcvs
Posts: 576
Joined: Mar 06, 2008 19:27
Location: Spain
FBIde 0.4.6
A IDE open-source by the compilator FreeBasic
(www.freebasic.net)