0

I try to fill the an array with the result which get from below function.

The function is:

   private int Sum(int i)
   {
        for (int j = 1; j <= i + 1; j++)
        {
            if (i % j == 0)
            {
                s += j;
            }
        }
        return s;
    }

I create an array and then try to use SetValue() or evaluate directly to fill the array with the results.

for example, Array[1]=Sum(1);

However, none of them works.

=====================================================

[Update for more details]

I need use an array with large amount.

int[] arr = new int[10000]

I try use below codes

    foreach (int i in arr)
    {

        arr.SetValue(Sum(i),i);
    } 

And

    for(int i = 0; i<10001; i++)
    {
        arr[i]=Sum(i);
    }

None of them works.

======================================================

Is there a way to fill the array with functions?

Sorry for my poor English. :(

Thanks

SuT

3
  • 3
    Could you show us the code that declares and creates the array? Commented Jan 30, 2011 at 8:26
  • Chances are, the problem is in how you declared the array. You can't add members to the array dynamically; you need a different storage class for that. Commented Jan 30, 2011 at 8:29
  • After you added the code, I see only two small problems (using the “for” array, not “foreach”, that is completely wrong): 1. If you create an array with new int[10000], the items are indexed 0 to 9999, not 10000, i.e. you need to do i<10000, not i<10001 (or, better, just do i<arr.Length). 2. You do not declare and initialize s in your Sum() function. Otherwise, the code works fine. Commented Jan 30, 2011 at 9:21

4 Answers 4

2

The code that you showed works just fine. You just have to make sure that the array actually exists, and that it's large enough. Avoid the name Array as there is a class with that name already.

Example:

int[] myIntegers = new int[2]; // creates an array with indexes 0 and 1
myIntegers[1] = Sum(1); // calls the function and puts the result in the second item

You can use a loop to set all the items in an array:

int[] myIntegers = new int[42];
for (int i = 0; i < myIntegers.Length; i++) {
  myIntegers[i] = Sum(i);
}
Sign up to request clarification or add additional context in comments.

1 Comment

I've tried this, but always get error, which says 'An object reference is required for the non-static field, method, or property'...
1

C# is meant to use lists, dictionaries and others instead of raw arrays (see FoxCop error messages for your code for details). So i think your task should be solved as follows:

    class Program
    {
        static int moo(int a)
        {
            for (int i = 2; i < a; i++)
                if (a % i == 0)
                    return i;

            return a;
        }

        static void Main(string[] args)
        {
            List<int> a = new List<int>();

            for (int i = 0; i < 100; i++)
                a.Add(moo(i));

            for (int i = 0; i < a.Count; i++)
                Console.WriteLine("a[{0}] = {1}", i, a[i]);
        }
    }

Note: do not forget to initialize your array/list/whatever (e.g. call its constructor).

2 Comments

I've tried this, but always get error, which says 'An object reference is required for the non-static field, method, or property'...
That means you are trying to execute non-static (object-dependent one) method from within static one. E.g. if your method/function does not depends on class fields you should make it static.
0

well you can create a variable int sum = 0; call the function sum and store the result of function in the variable and use that variable to populate the array as and when you need..for ex:array[1] = sum;

Comments

0

The first problem is that you have the array declaration wrong. The brackets indicating that you're declaring an array need to follow the type, rather than the variable name. It should look like this:

int[] arr = new int[10000];

The second problem is that you're confusing the brackets used for array indexing and method arguments. Square brackets [ ] are used for array indexing, and curved parentheses ( ) are used to surround the arguments passed to a method. So your code should look like this instead:

for (int i = 0; i < arr.Length; i++)
{
    arr[i] = Sum(i);
}

And third is that Length is a property, not a method. That means you don't put parentheses after its invocation. (See the code above.)


You may find the following MSDN article helpful as you're trying to work with arrays: Arrays Tutorial

3 Comments

Thanks for your correction. And I've tried perform the program with correct format, it did not works...
@SuT: What exactly didn't work? I see you've commented to some of the other answers that you got "An object reference is required..." error. That means you're declaring/creating the array in one place (like at the top of your code file?) but then trying to access/fill it from inside of a static method (like Main). A static method is one that is not associated with any particular instance of a class, so it can't access variables that are associated with instances. The simple fix is to declare your array object as static, too: static int[] arr = new int[10000];
It's my mistake...My codes have some other errors. And it works now. Thanks again. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.