Re: Basic array question, need help

From: Anthony Borla (ajborla_at_bigpond.com)
Date: 12/10/03


Date: Wed, 10 Dec 2003 08:46:33 GMT


"KellyH" <Kelly@whatever.com> wrote in message
news:UqoBb.484596$Tr4.1330037@attbi_s03...
>
> Hi, I hope someone can point me in the right direction.
> I'll get it out of the way: Yes, I am a college student.
> No, I am not looking for anyone to do my homework,
> just looking for help. I have been reading this ng all
> semester, and found it very helpful. I just have a
> stupid problem going on right now.
>
> Here's the project:
> We have to make a little frame with a text field, where
> the user inputs a number, and a text area where a message
> is displayed. There are several buttons on the frame:
> Enter, Average, Show Ascending, Show Descending,
> Median, and High Number. We are to use an array to store
> up to 50 integers the user enters.
>
> Here's my problem:
> My problem is that I can't figure out how to load the array.
> If I do it in the Enter button event, then a new array is created
> each time an integer is entered. I tested this by adding the
> current int to the int in the previous cell. Oh, I am also
> storing the logical end of the array in the 0 cell. If I try to
> load the array in the main, I get an error: Non-static method
> load_aNum cannot be referenced from a static context.
> We had a project previously where I used two arrays
> (pre-filled, not using input numbers), and I loaded it in the
> main, and it worked fine. Don't know why this won't work.
>
> What I have so far (I've just been concentrating on getting
> the user numbers to enter into the array. I'll worry about the
> rest of it later):
>
<SNIP CODE>
>

I didn't look at your code, but will give you some general pointers:

* Since the array size has a known, maximum size [50 ?]
   create it at program startup / initialisation time e.g

       // Note: This is an example - your code may be
       // different
       int array = new array[50];

  You will note that such an array is automatically zero-filled; you
   can use this value as an 'empty element' marker. If you need
   to use zero as a valid value, then fill the array with an arbitrary
   value [say -1 ?] to act in this role

* Each time the input routine successfully completes add the value
   to the array by incrementing an index e.g.

       ...
       if (nextElement < array.length)
       {
           array[nextElement] = inputValue;
           ++nextElement;
       }
       ...

   'nextElement' will always point to the next enpty array
   element.

* If not all elements have been filled you can still traverse
   the array [and know when to stop] by checking the
   current element value for -1.

   This allows you to display, or otherwise manipulate
   array contents even if it is not full

* If you need to have some sort of 'reset' operation,
   ensure all it does is refill the array with -1 values. You
   don't have to recreate the array again

I hope this helps.

Anthony Borla