locked
arrays RRS feed

  • Question

  • Hi,

    i have an array like as below

    int nlph;

    double GDL = new double [10*nlph];

    suppose i have nlph =2

    i want to take the values at the positions 0,1,7,8,9 from GDL in one array and at the positions 20,21,27,28,29 into another array.

    please help me.

    Tuesday, January 8, 2013 10:34 AM

Answers

  • ppdev :

    Why dont you use a list to store your vectors GD1,GD2 as there is no Vector class in c# unlike in Java. I did create a list below and then based on the nlph value from in your function you can get the values from GDL list

      double[] GDL = new double[20] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
                List <object> GDList = new List <object> ();
                GDList.Add(new int[] { 1, 2, 8, 9, 10 }); //GD1
                GDList.Add(new int[] { 11, 12, 18, 19, 20 }); //GD2
                int nlph = 2;//from funtion..can change (list has 2 elements) 
                for(int dim = nlph  ; dim > 0; dim--)
                {
                    int[] GDArray = (int []) (GDList.ElementAt(dim - 1));
                    //This GDArray is your need array  GD1 ,GD2 which is initialized in the list
                    Console.WriteLine("Printing GD {0} Elements ", dim);
                    for (int i = 0; i < GDArray.Length;i++ )
                    {
                        Console.WriteLine(GDArray[i]);
                        //Here you can add your logic as you have the GD1/GD2
                        // your value = GDL ( GdArray[i])
                    }
                }

     


    • Edited by Venkat786 Tuesday, January 8, 2013 2:37 PM
    • Proposed as answer by Jason Dot Wang Wednesday, January 9, 2013 7:53 AM
    • Marked as answer by ppdev Wednesday, January 9, 2013 8:26 AM
    Tuesday, January 8, 2013 2:36 PM

All replies

  • Hi ppdev,

    You access values in an array by using the array indexer like this:

    var value = GDL[0];

    Let us say that you have another array called SomeArray that contains the same types as GDL does, then you can manually set the values in the other array with the value from the GDL array like this:

    SomeArray[20] = GDL[0];
    SomeArray[21] = GDL[1];

    If I understand you correctly, you want to do something like the following:

    SomeArray[(nlph * 10) + i] = GDL[i];

    Where "i" corresponds with the current index you want to move. 

    Hope this helps


    Mark good replies as helpful and correct ones as answers! - http://blog.filipekberg.se
    Check out my recently published book C# Smorgasbord on Amazon

    Tuesday, January 8, 2013 10:44 AM
  • Hi,

    truly, i did'nt understand your question.

    Can you please explain a bit more ?


    One good question is equivalent to ten best answers.

    Tuesday, January 8, 2013 10:48 AM
  • Please check out the Following example.

    int intsize = 10 * 2;
    double[] doubles = new double[intsize];
    doubles[0] = 1;
    doubles[1] = 31;
    doubles[2] = 32;
    doubles[3] = 33;
    doubles[4] = 34;
    doubles[5] = 35;
    doubles[6] = 36;
    doubles[7] = 37;
    doubles[8] = 38;
    doubles[9] = 39;
    doubles[10] = 40;
    doubles[11] = 41;
    doubles[12] = 42;
    doubles[13] = 43;
    doubles[14] = 44;
    doubles[15] = 45;
    doubles[16] = 46;
    doubles[17] = 47;
    doubles[18] = 48;
    doubles[19] = 49;
    //Please give your array size
    double[] dblCopyArry = new double[10];
    int[] intGivenPosition = { 0, 1, 7, 8, 9 };
    int[] intCopyPosition = {12, 13, 15, 18, 19 };
    foreach (int item in intGivenPosition)
    {
              MessageBox.Show(doubles[item].ToString());
     }
     int No =0;
     foreach (int item in intCopyPosition)
     {
                 dblCopyArry[No] = doubles[item];
                 No++;
     }
     


    With Thanks and Regards
    Sambath Raj.C
    click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you
    Happy Programming!

    Tuesday, January 8, 2013 11:05 AM
  • i am giving a bit of code because i have lot of parameters passing in turbine function

    int nlph;

    nlph is not a constant value it varies based on the input.

    double[] GDL = new double[10*nlph]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};

     

    i pass this GDL array in a function like turbine(a,b,c,d,GDL,nlph,out mw)

    In the turbine function

    public void turbine(double a,double b,double c,double d,double [] GDL,int nlph,out double mw)

    {

        // in this function i want to divide that GDL array into GD1 array, GD2 array,...... its based on nlph.

       // i want to dynamically create the no. of GD vectors based on nlph value.

     // suppose if nlph =3 then i want to create dynamically GD1,GD2,GD3 arrays

    // after this i want to assign the values from GDL to GD1 and GD2 as below

    // GD1 = {1,2,8,9,10}

    //GD2 = {11,12,18,19,20}


    }

    Tuesday, January 8, 2013 11:08 AM
  • i need to assign the values by programming not manually
    Tuesday, January 8, 2013 11:11 AM
  • how exactly do you decide how many elements will go into the GD1, GD2 etc arrays?

    and what exactly is the logic to store the numbers in the respective arrays?

    Tuesday, January 8, 2013 11:26 AM
  • copiedArray[21] = originalArray[0];

    This will transfer the values of 0,1,7,8,9 to 20,21,27,28,29 not manually :)

    int nlph = 2;
    double GDL = new double [10*nlph];
    double newGDL = new double [(10*nlph)*2];
    int half = (10*nlph);
    int j = 0;
    
    for(int i = half; i < (10*nlph)*2; i ++)
    {
     newGDL[i] = GDL[j];
     j++;
    }

    I am assuming that if your array size is 10 * 2 = 20 then you want that the second array must have indexes 21, 22, 23 so on and so for. I set the second array size 10 * 2 = 20 * 2 = 40 meaning I have doubled the size of the second array.

    Hope this help :)


    • Edited by aaccee Tuesday, January 8, 2013 11:57 AM
    Tuesday, January 8, 2013 11:41 AM
  • ppdev :

    Why dont you use a list to store your vectors GD1,GD2 as there is no Vector class in c# unlike in Java. I did create a list below and then based on the nlph value from in your function you can get the values from GDL list

      double[] GDL = new double[20] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
                List <object> GDList = new List <object> ();
                GDList.Add(new int[] { 1, 2, 8, 9, 10 }); //GD1
                GDList.Add(new int[] { 11, 12, 18, 19, 20 }); //GD2
                int nlph = 2;//from funtion..can change (list has 2 elements) 
                for(int dim = nlph  ; dim > 0; dim--)
                {
                    int[] GDArray = (int []) (GDList.ElementAt(dim - 1));
                    //This GDArray is your need array  GD1 ,GD2 which is initialized in the list
                    Console.WriteLine("Printing GD {0} Elements ", dim);
                    for (int i = 0; i < GDArray.Length;i++ )
                    {
                        Console.WriteLine(GDArray[i]);
                        //Here you can add your logic as you have the GD1/GD2
                        // your value = GDL ( GdArray[i])
                    }
                }

     


    • Edited by Venkat786 Tuesday, January 8, 2013 2:37 PM
    • Proposed as answer by Jason Dot Wang Wednesday, January 9, 2013 7:53 AM
    • Marked as answer by ppdev Wednesday, January 9, 2013 8:26 AM
    Tuesday, January 8, 2013 2:36 PM