![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 120
Rep Power: 3
![]() |
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',hhWhen I try to print hh, it gives me 0. Is there another way to check for empty array( of interger) Thank you B. |
|
|
|
|
|
#2 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3
![]() |
What if 0 is a valid data value?
|
|
|
|
|
|
#3 | |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 120
Rep Power: 3
![]() |
Quote:
But suppose that 0 is not an elligible value in the array, why does my approach not work? B. |
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|