0

Is there a way to have diffrent statement for each loop in a while or for loop? Example: I have an int array, lenght n, and I want to add diffrent specific values (same type of course) at each index ...

so that when index is equal n-1 it stops. I know how to do it if their are a few statements but what if there are 100 specific value to be added.

More specific:

public static int[] ArrayPlay(int n) 
{ 
    int[] arr = new int[n]; 
    while (arr[x] < n) 
    arr[0] = 3; 
    arr[1] = 12; 
    arr[2] = 176; 
    arr[3] = 4;
    //so on...
    return arr;
} 

would look like somthing like this:

public static int[] ArrayPlay(int n)//4
{
    int a = 0;
    int[] arr = new int[n];
    while ( a <= n)  //lets say n is 4
        arr[a] = 3;  //arr[0]
    a++;
        arr[a] = 12; //arr[1]
    a++;
        arr[a] = 4;  //arr[2]
    a++;
        arr[a] = 8;  //arr[3] 
    a++;             //(a=4 Stop!  and return arr)
        arr[a] = 7;
    a++;
        arr[a] = 12;
    a++;
        arr[a] = 22;
    a++;
        arr[a] = 18;

    return arr;
}

it never passes to a++ because the first statement is Always true.

Is there a way to break, check condition and continue between each statement? and what property or method to check an arrays index nr (where I wrote arr[x]) x being the value to be compared with n.

public static int[] ArrayPlay2(int n)//4
{

    int[] arr = new int[n];
    for (int a = 0; a < arr.Length; a++)
    {
        arr[a] = 3;  //arr[0]
        arr[a] = 12; //arr[1]
        arr[a] = 4;  //arr[2]
        arr[a] = 8;  //arr[3] (a=4 Stop!  and return arr)
        arr[a] = 7;
        arr[a] = 12;
        arr[a] = 22;
        arr[a] = 18;

    }
    return arr;
}

Why dont the code above work??

Shelby115
  • 2,816
  • 3
  • 36
  • 52
Matilda
  • 9
  • 4
  • 3
    What means `there are 100 specific value to be added`? It's simply not clear what you're trying to achieve so it's difficult to help. But what speaks against an `if` or `switch`? – Tim Schmelter Mar 18 '16 at 10:43
  • 1
    Don't use arrays. Instead use List<> where you can just add to the end rather than with arrays where you have to specify the size of the array when it gets constructed. – jdweng Mar 18 '16 at 10:44
  • 1
    Stop talking about loops, if-statements, conditions. Explain in plain english what you want to do. – Lasse V. Karlsen Mar 18 '16 at 12:28
  • I want to fill an array with up to 30 specific values(elements), the amount of elements depends on the int n argument because it decides the length of the array. – Matilda Mar 18 '16 at 12:42

4 Answers4

1

It sounds like you're just trying to copy part of an array.

// TODO: Handle the case "n > magicIntegers.Length" how you see fit.
int[] magicIntegers = new int[] { 3, 12, 4, 8, 7, 12, 22, 18 }; // etc.
int[] copiedArray = new int[n];
return Array.Copy(magicIntegers, 1, b, 0, n);
Community
  • 1
  • 1
Shelby115
  • 2,816
  • 3
  • 36
  • 52
0

There is a bug in your code - while will do only arr[0] = 3; or is that what u are trying to do?

If u are trying to loop through all elements why just not use:

int[] arr = new int[n];
foreach (int i in arr)
{
}

If you are trying to fulfill it than maybe:

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

I belive you understand it wrong how loop works all of yours: arr[a] = 3; //arr[0] and arr[a] = 12; //arr[1] and arr[a] = 4; //arr[2] are still inside single loop, so a is stil one single number. It is incremented by a++ that you wrote in for (int a = 0; a < arr.Length; a++) part.

makro88
  • 102
  • 8
  • I know, I rewrote my question so it focus on my main issue how to make my condition loop pass arr[0]=3 to a++ and then check condition again and again after each statement. – Matilda Mar 18 '16 at 12:19
  • Look at my edit It probably explains in what you are wrong. – makro88 Mar 18 '16 at 12:41
0

I would do some thing like this:

// your source array
int[] source = new int[]{3,12,4,8,7,12,22,18}; 
int n = 4; // or what ever  N you want
int[] arr = source.TakeWhile(x => x!=n),ToArray(); // with x!=n as your condition to keep taking

Hope this helps

marco wassmer
  • 421
  • 2
  • 8
  • Thank u, I didnt find the TakeWhile or Take method in msdn Array class library, i guess they are some kind om interface methods? could u explain (x => x !n) more for me? its a linq? – Matilda Mar 18 '16 at 14:09
  • Yes, it is a [Lambda](https://msdn.microsoft.com/en-us/library/bb397687.aspx) Operator, a anonymous function. The syntax is `(input parameters) => expression`, in the TakeWhile the x is the element, so this methode goes through the array testing each element with the Lambda expression until one returns true – marco wassmer Mar 18 '16 at 14:47
  • the code above didnt work but the similar code:public static int[] ArrayPlay(int n) { return new []{ 3, 12,4, 8, 7,12,22,18 } .Take(n) .ToArray(); } works, why is that? – Matilda Mar 18 '16 at 15:48
  • Yeah, my bad. It is the toArray(), the take(n) returns a iEnumrable, not an array – marco wassmer Mar 18 '16 at 16:14
  • Yes I added ToArray() but I am confused why it only picks out the two first elements 3, 12 if I give argument n=4 – Matilda Mar 18 '16 at 16:38
0

You can use a source array which contains all of the possible values and copy only the needed number to a new one.

With linq this is quite easy:

public static int[] ArrayPlay(int n)
{
    return new []{ 3, 12,4, 8, 7,12,22,18 }
        .Take(n)
        .ToArray();
}

Or you could use iterators which gives you the values one after the other:

private static IEnumerable<int> GetValues()
{
    yield return 3;  //arr[0]
    yield return 12; //arr[1]
    yield return 4;  //arr[2]
    yield return 8;  //arr[3] 
                     //(a=4 Stop!  and return arr)
    yield return 7;
    yield return 12;
    yield return 22;
    yield return 18;
}

public static int[] ArrayPlay(int n)
{
    return GetValues()
        .Take(n)
        .ToArray();
}
abto
  • 1,583
  • 1
  • 12
  • 31