Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 22nd, 2006, 6:03 PM   #1
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 120
Rep Power: 3 brad sue is on a distinguished road
empty array

Hi I would like to check if an array of interger is empty( fortran 77).
my strategy is to set all the elements equal to zero. then to check if the array is empty I set a counter that count the number of zeros. if this number is equal with tha length of the array then the array is empty.
do 11 k=1,10
	listonline(k)=0
11	continue

else if(option.EQ.'PL') then
      
          do 81 p=1,j-1,1
              if(listonline(p).EQ.0) then
                  hh=hh+1 
           end if	
81        continue
       write(6,*)'number of zeros',hh

When I try to print hh, it gives me 0.

Is there another way to check for empty array( of interger)

Thank you
B.
brad sue is offline   Reply With Quote
Old Apr 22nd, 2006, 6:13 PM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
What if 0 is a valid data value?
Jimbo is offline   Reply With Quote
Old Apr 22nd, 2006, 6:32 PM   #3
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 120
Rep Power: 3 brad sue is on a distinguished road
Quote:
Originally Posted by Jimbo
What if 0 is a valid data value?
Yes this is what I though but I dont see something else to do!

But suppose that 0 is not an elligible value in the array, why does my approach not work?
B.
brad sue is offline   Reply With Quote
Old Apr 22nd, 2006, 9:48 PM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
You basically need to define some condition that means the array is empty.

One way is to pick a value that says "this cell is empty" and check how many elements in the array have that value. Your approach of using zero as an invalid value is a particular case of this. Naturally, with this approach, you need to initialise the array before trying to use it, and I'll bet that's causing your code "not to work" --- as with many programming languges, an array in Fortran is not initialised to zero except in special cases, and you're probably assuming the array starts life with zero values.

Another way is to separately keep track of the number of non-empty elements, and shuffle elements in the array whenever you add or remove an element to it. That way you know that the elements 1 through to n (where, if n is positive, it is the number of valid elements) are not empty.

Neither approach is perfect. For example, in a scenario where you wish to add a new valid cell, the first method is probably less efficient than the first as it requires searching the array to find a slot, which is not necessarily efficient, while the method just adds a value to the end and increments a count. However, in another scenario where you wish to remove a cell, the first scenario is more efficient as you simply set the value in a cell to be your invalid value compared with shuffling elements around.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:07 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC