How to Insert value in array

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 2700 Views - Last Post: 01 December 2012 - 02:34 AM Rate Topic: -----

#16 paan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-November 12

Re: How to Insert value in array

Posted 26 November 2012 - 04:33 PM

what i just said before, i want value from foreach loop insert automatically in array...so, that i can call it by array[0] = firstElement, array[1] = secondElement, array[2] = thirdElement, array[3] = fourthElement and so forth...

this is dummy situation i can tell you...if you all doesn't catch up what i told you, i didn't know how to describe you guys....let's say, during foreach loop, i can get two value for example which is 5 and 3....from this phase, these two value, i want it automatically insert into array that names terms[]....simple enough to understand right....my purpose it insert into array because i want do comparison between that value to find a maximum value....about value i get from foreach loop just now, i extract from external source which is string[] l = strGeneratedCandidate.Split (' ');....

this strGeneratedCandidate value is 12...after passing process splittng, the value bacome 1 and 2...are you clear right now....then these value, i use in another method to find maximum value...

see code below for better understanding
 private Dictionary<string, double> GenerateCandidates(Dictionary<string, double> dic_FrequentItems, Dictionary<string, double> dic_FrequentItemsL1)
        {
            List<clssRules> lstSingleElementReturn = new List<clssRules>();
            
            Dictionary<string, double> dic_CandidatesReturn = new Dictionary<string, double>();

            for (int i = 0; i < dic_FrequentItems.Count - 1; i++)
            {
                string strFirstItem = Alphabetize(dic_FrequentItems.Keys.ElementAt(i));
                for (int j = i + 1; j < dic_FrequentItems.Count; j++)
                {
                    string strSecondItem = Alphabetize(dic_FrequentItems.Keys.ElementAt(j));
                    string strGeneratedCandidate = GetCandidate(strFirstItem, strSecondItem);
                    string[] l = strGeneratedCandidate.Split(' '); //1,2 --> 1 and 2
                   
                    GetSingleElement(l, dic_FrequentItemsL1, strGeneratedCandidate);
                   
                }
            }
            return dic_CandidatesReturn;
        }



 double sqq;
        private Dictionary<string,string> GetSingleElement(string[] l, Dictionary<string, double> dic_FrequentItemsL1, string strGeneratedCandidate) 
        {
            Dictionary<string, double> value = new Dictionary<string, double>();
            Dictionary<string, string> dic_MaxReturn = new Dictionary<string, string>();
            //string firstItem = l[0]; //12,13,14,15............foreach
           // string[] q = l; // <-- here problem...
            string qq = "";
            string[] terms = new string[l.Length];

            //int[] terms = new int[10];
            foreach (string v in l)
            {
                string a = v.Replace("\r\n", " ");
                foreach (var s in a)
                {
                    qq = s.ToString();
                    MessageBox.Show(qq, "q");
                    //qq = match with dictionary(dic_FrequentItemsL1) single set...example if 1 the outcome is 5 , meanwhile if 2 its comes out with 3...
                    sqq = dic_FrequentItemsL1[qq];
                    MessageBox.Show(sqq.ToString());

                    for (int index = 0; index < l.Length; index++)
                    {
                        terms[index] = sqq.ToString(); // just to give it a unique value you can see
                        MessageBox.Show(terms[index], "terms[index]");
                    }
                }
               
                
            }
            MessageBox.Show(terms[0].ToString(), "arrays[0]");
            MessageBox.Show(terms[1].ToString(), "arrays[1]");
            getMaxValue(terms);
            dic_MaxReturn.Add(strGeneratedCandidate, getMaxValue(terms));
             return dic_MaxReturn;
        }




public static string getMaxValue(string[] numbers)
        {
            string maxValue = numbers[0];
            MessageBox.Show(maxValue, "maxValue");
            for (int i = 1; i < numbers.Length; i++)
            {
                if (numbers[i] > maxValue)
                {
                    maxValue = numbers[i];
                }
            }
            return maxValue;
        } 



are you guys clear right now, what i want to do???
Was This Post Helpful? 0
  • +
  • -

#17 paan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-November 12

Re: How to Insert value in array

Posted 30 November 2012 - 08:43 PM

here sample code that i be done but i cant find a best way

foreach (string v in l)
            {
                MessageBox.Show(v.Length.ToString(), "v");
                double[] terms = new double[v.Length];
                string a = v.Replace("\r\n", " ");
                foreach (var s in a)
                {
                    qq = s.ToString();
                    MessageBox.Show(qq, "q");
                    //qq = match with dictionary(dic_FrequentItemsL1) single set...example if 1 then 5 , meanwhile 2 then 3...
                    sqq = dic_FrequentItemsL1[qq];
                    

                    for (int index = 0; index < v.Length; index++)
                    {
                        terms[index] = sqq; // just to give it a unique value you can see
                        MessageBox.Show(terms[0].ToString(), "terms[0]");
                        MessageBox.Show(terms[1].ToString(), "terms[1]");
                       
                    }

                }
                getMaxValue(terms);
            }



:gunsmilie:
Was This Post Helpful? 0
  • +
  • -

#18 paan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-November 12

Re: How to Insert value in array

Posted 30 November 2012 - 08:48 PM

Get some value from foreach loop. Let’s say, the value is 5 and 3. Then I want these values insert in arrays named terms as below. For details, I want value 5 insert in terms 0. Then, for the second loop, value 3 insert in terms 1. See illustrations below…My purposes to do that because, I want compare the value in array later to find maximum value…the number of loop depends with number of value get from foreach loop. Let’s say, value from foreach loop is, 5,3, and 2. So, that’s mean, the program must do three times of loop. And the result is, terms[0] = 5, terms[1] = 3, terms[2] = 2.

terms[0] [___5___] [___5___] [_______]
terms[1] [_______] [___3___] [_______]
terms[2] [_______] [_______] [_______]
terms[3] [_______] [_______] [_______]
terms[4] [_______] [_______] [_______]
[terms] [terms] [terms]
First-loop second-loop third-loop
Was This Post Helpful? 0
  • +
  • -

#19 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3666
  • View blog
  • Posts: 11,497
  • Joined: 05-May 12

Re: How to Insert value in array

Posted 30 November 2012 - 08:53 PM

Terms on line 4 is declared as a one dimensional array, and you want:

Quote

And the result is, terms[0] = 5, terms[1] = 3, terms[2] = 2
,
but your illustration

Quote

terms[0] [___5___] [___5___] [_______]
terms[1] [_______] [___3___] [_______]
terms[2] [_______] [_______] [_______]
terms[3] [_______] [_______] [_______]
terms[4] [_______] [_______] [_______]

shows a 2 dimensional array.

What do you really want?

BTW, do not open new threads when you have an existing topic: http://www.dreaminco...g-foreach-loop/
Was This Post Helpful? 0
  • +
  • -

#20 paan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-November 12

Re: How to Insert value in array

Posted 30 November 2012 - 08:59 PM

i have ilustration that want give to you guys...



Get some value from foreach loop. Let’s say, the value is 5 and 3. Then I want these values insert in arrays named terms as below. For details, I want value 5 insert in terms 0. Then, for the second loop, value 3 insert in terms 1. See illustrations below…My purposes to do that because, I want compare the value in array later to find maximum value…the number of loop depends with number of value get from foreach loop. Let’s say, value from foreach loop is, 5,3, and 2. So, that’s mean, the program must do three times of loop. And the result is, terms[0] = 5, terms[1] = 3, terms[2] = 2.

terms[0] [___5___] [___5___] [_______]
terms[1] [_______] [___3___] [_______]
terms[2] [_______] [_______] [_______]
terms[3] [_______] [_______] [_______]
terms[4] [_______] [_______] [_______]
[terms] [terms] [terms]
First-loop second-loop third-loop

owh, it's just small misunderstanding. Actually that's not 2-dimensional array. i'm just assigned the first column as first-loop. Then, for second column, is second-loop, and so on. it's actually one single array only. i just make two and three for show you guys what happen for the next loop. the value will insert into next index in array..get it??
Was This Post Helpful? 0
  • +
  • -

#21 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: How to Insert value in array

Posted 30 November 2012 - 11:50 PM

Short example that does what you want
static class Example {
    static int[] array;
    static int i = 0;

    static Example() {
        array = new int[10];
    }

    static void Add(int n) {
        array[i] = n;
        i++;
    }
}


Calling Add() will insert the value into the array, at the next available location.

WARNING: There is no error checking. Everything is static so I don't have to show instantiating a class. If you don't understand static, don't use it in your code. Void where prohibited. Wear safety glasses while operating.
Was This Post Helpful? 1
  • +
  • -

#22 paan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-November 12

Re: How to Insert value in array

Posted 01 December 2012 - 02:20 AM

thank you very much. but, i already find the solution by itself and it worked as i plan it..this is a simple code that fulfill my requirement.

            int index = 0;
            foreach (string v in l)
            {
                MessageBox.Show(v.Length.ToString(), "v");
                double[] terms = new double[v.Length];
                string a = v.Replace("\r\n", " ");
                
                foreach (var s in a)
                {
                    qq = s.ToString();
                    MessageBox.Show(qq, "q");
                    //qq = match with dictionary(dic_FrequentItemsL1) single set...if 1 get  5 , meanwhile 2 get 3...
                    sqq = dic_FrequentItemsL1[qq];
                    terms[index] = sqq;
                    index++;
                }
                MessageBox.Show(terms[0].ToString(),"terms[0]");
                MessageBox.Show(terms[1].ToString(), "terms[1]");
                getMaxValue(terms);
            }



Simple enough right. Anyway, I'm appreciate your kindness when want help me solve this problem. I already get what i want... :bananaman:
Was This Post Helpful? 0
  • +
  • -

#23 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: How to Insert value in array

Posted 01 December 2012 - 02:34 AM

Your code is the same as mine :)
My line 3 is your line 1
My line 2,6 is your line 5
My line 10 is your line 14
My line 11 is your line 15
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2