Function Comparation

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
lrcvs
Posts: 578
Joined: Mar 06, 2008 19:27
Location: Spain

Function Comparation

Post by lrcvs »

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 array




declare function comparation  (i, f, a ()) as integer
dim i as integer ' position init
dim f as integer ' position end
dim a () as integer

cls
x = 100
redim a (x)
i = 1
f = x

for h = 1 to 100
a (h) =   3  
next h
print comparation  (i, f, a ())
a (5) =   8  
print comparation  (i, f, a ())

sleep
end


function comparation  (i, f, a ()) as integer
z  =   0  
y = a(i)
for j = i to f
for k = i to f
if a (k) <> y then z  =   1   :exit for
next k
if z  =   1   then exit for
next j
if z   =   0   then
comparation   =   0   'True
else
comparation   =   1   'False
end if
end function
fxm
Moderator
Posts: 12108
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Post by fxm »

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 array



declare function comparation (byval i as integer, byval f as integer, a() as integer) as integer
dim i as integer ' position init
dim f as integer ' position end
dim a() as integer

cls
dim x as integer
x = 100
redim a(x)
i = 1
f = x

for h as integer = 1 to 100
  a(h) = 3 
next h
a(5) = 8
print comparation(i, f, a ())
sleep
end


function 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 j
end function
 
lrcvs
Posts: 578
Joined: Mar 06, 2008 19:27
Location: Spain

Function comparation more easy

Post by lrcvs »

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 array




declare function comparation  (i, f, a ()) as integer
dim i as integer ' position init
dim f as integer ' position end
dim a () as integer

cls
x = 100
redim a (x)
i = 1
f = x

for h = 1 to 100
a (h) =   3  
next h
print comparation  (i, f, a ())
a (5) =   8  
print comparation  (i, f, a ())

sleep
end


function comparation  (i, f, a ()) as integer
z  =   0  
y = a(i)

for k = i to f
if a (k) <> y then z  =   1   :exit for
next k

if z   =   0   then
comparation   =   0   'True
else
comparation   =   1   'False
end if

end function
 
fxm
Moderator
Posts: 12108
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Post by fxm »

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 integer
comparation   =   0   'True
y = a(i)

for k = i + 1 to f
if a (k) <> y then comparation   =   1   :exit for   'False
next k

end function
Last edited by fxm on May 15, 2011 12:27, edited 1 time in total.
lrcvs
Posts: 578
Joined: Mar 06, 2008 19:27
Location: Spain

Post by lrcvs »

Certainly, I work in Qbasic.

( I'm old school! )

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

Now modified my program.

Thanks again for your observation!

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

Post by KristopherWindsor »

Code: Select all

for j = i to f
for k = i to f
if a (k) <> y then z  =   1   :exit for
next k
if z  =   1   then exit for
next j
This outer for loop is completely unnecessary, right?
fxm
Moderator
Posts: 12108
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Post by fxm »

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: 578
Joined: Mar 06, 2008 19:27
Location: Spain

Post by lrcvs »

FBIde 0.4.6
A IDE open-source by the compilator FreeBasic
(www.freebasic.net)
Post Reply