return a value from an array element

Finding the last element with a value in an array

Page 1 of 1

5 Replies - 2236 Views - Last Post: 25 February 2010 - 07:42 AM Rate Topic: -----

#1 Hawkeye28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 12-November 09

return a value from an array element

Posted 24 February 2010 - 06:35 PM

I am having trouble with a function in which I am trying to return the last value in an array. My problem is the array does not have a value in very element(this because you have to give the array a size when creating it and I cant get it to work with an arraylist)
so here is the code. I need to find the last value put into the array to do some calculations. If anyone has any ideas to use an arraylist and .Add that would be great. Thanks!

public static void GetCalc(int n1, int n2)
        {
            int i, n = 1, m = n2;

            int[,] numArray = new int[15, 2];
            
            for (i = 0; n <= n1; i++) 
            {
                if (n1 > 0)
                {
                    n = n * 2;
                    m = m * 2;
                }
                    if (n <= n1)
                    {
                        numArray[i, 0] = n;
                        numArray[i, 1] = m;

                        Console.WriteLine(numArray[i, 0]);
                        //Console.WriteLine(numArray[i, 0] + " " + numArray[i, 1]);
                    }
                else if (n1 <= 1) 
                {
                    numArray[i, 0] = n1;
                    numArray[i, 1] = n2;
                    //Console.WriteLine("Case" + (x + 1) + " : " + n1 + " x " + n2 + " = " + (n1 * n2));
                }
            }
        }



Is This A Good Question/Topic? 0
  • +

Replies To: return a value from an array element

#2 Smurphy   User is offline

  • D.I.C Regular
  • member icon

Reputation: 35
  • View blog
  • Posts: 367
  • Joined: 07-July 08

Re: return a value from an array element

Posted 24 February 2010 - 06:53 PM

Arrays are indexed from zero. So if you have an array of size 6 there are only elements from 0-5. Try taking your for loop and making it,
//No equal to.
for (i = 0; n < n1; i++)


Was This Post Helpful? 0
  • +
  • -

#3 Hawkeye28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 12-November 09

Re: return a value from an array element

Posted 24 February 2010 - 07:42 PM

View PostSmurphy, on 24 February 2010 - 05:53 PM, said:

Arrays are indexed from zero. So if you have an array of size 6 there are only elements from 0-5. Try taking your for loop and making it,
//No equal to.
for (i = 0; n < n1; i++)


I should have clarified a bit. I am reading in from a file. The first line is
13 238
I want to bring that in and them multiply each number by 2 up to but not beyond the first number.
2 238
4 476
6 952
8 1904

then I want to pull the 8 from the array. I know indexes begin at 0, my problem is I had to set the array size before I read the numbers in. Each loop that reads a line in will have a different number of items in the array. So if I set it to "[25,2]" and the array only stores, in the above example 3 items, I need the value of the third item. If the next line stores 9 items, I need the value of the 9th item.
Was This Post Helpful? 0
  • +
  • -

#4 DuoShin85   User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 135
  • Joined: 18-December 09

Re: return a value from an array element

Posted 24 February 2010 - 09:56 PM

You can probably get away with either a List(T) or a Dictionary(TKey, TValue) object rather than an Array, because it seems you want to add to your container dynamically. Arrays hinder you in that regard.
Was This Post Helpful? 0
  • +
  • -

#5 Hawkeye28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 12-November 09

Re: return a value from an array element

Posted 24 February 2010 - 10:13 PM

Yes, I do want to add to it dynamically. I am completely stuck right now though and have no idea how to use either one of those....
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: return a value from an array element

Posted 25 February 2010 - 07:42 AM

The solution to this

Quote

I am completely stuck right now though and have no idea how to use either one of those

is to go find out HOW to use them, right? Be inquisitive!

Look, right here is a great tutorial, written by SixOfEleven, that will teach you how!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1