How to Insert value in array

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 paan  Icon User is offline

  • New D.I.C Head

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

How to Insert value in array

Posted 25 November 2012 - 09:14 PM

Hello everyone, Im a new buddy here...Today, I already tried to making a good inserting value in array via foreach loop...but it kinda seem it not successful yet and i totally dunno how to solve it...it maybe error due the putting item in wrong blocks...can u guys see whats happen...im in beginner in c#...below, i display my code to you...

in this code, i try to insert element in array by foreach loop....lucky i tell you, it success insert in array but, its just insert a new element after one shot of foreach loop..for example, in foreach loop, its return two element just in code below...for the first time, it success into array and when i invoke it by MessageBox.Show, its tells us that terms[0] = 5....then for the second foreach loop, its return to terms[0] = 3....i dont want these but i want kinda array that in final, its outcome is terms[0] = 5 3....just imagine, the value for string[] l is 12...my intention to display terms[0] = 5 3 is because, i want these result insert into getMaxValue(terms)....so, that, value from that array will play around in that method to find a max value....thats my goal...find a maximum value...can u guys help me....its awesome if u can help me out from this problem...im stuck here for 2 weeks...


this is a parent code before a child code at below

 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);
             
                    MessageBox.Show("juon");
                 


                    if (strGeneratedCandidate != string.Empty)
                    {
                        strGeneratedCandidate = Alphabetize(strGeneratedCandidate);
                        double dSupport = GetSupport(strGeneratedCandidate);
                        dic_CandidatesReturn.Add(strGeneratedCandidate, dSupport);
                        
                    }
                }
            }
            return dic_CandidatesReturn;
        }






this a child code
double sqq;
        private Dictionary<string,double> GetSingleElement(string[] l, Dictionary<string, double> dic_FrequentItemsL1, string strGeneratedCandidate) 
        {
            Dictionary<string, double> value = new Dictionary<string, double>();
            Dictionary<string, double> dic_MaxReturn = new Dictionary<string, double>();
          
            string qq = "";

            foreach (string v in l)
            {
                double[] terms = new double[l.Length];
                string a = v.Replace("\r\n", " ");
                foreach (var s in a)
                {
                    qq = s.ToString();
                    MessageBox.Show(qq, "q");
                   
                    sqq = dic_FrequentItemsL1[qq];
                    MessageBox.Show(sqq.ToString());


                    terms[0] = sqq;
                    

                    MessageBox.Show(terms[0].ToString(), "terms[0]");
                    getMaxValue(terms);
                }
                dic_MaxReturn.Add(strGeneratedCandidate, getMaxValue(terms));
            }
             return dic_MaxReturn;
        }


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



haha..sorry, i do some little mistakes in topics...its not How to Insert value in string using foreach loop but its more to How to Insert value in array using foreach loop...sorry guyss.. :bigsmile:

Is This A Good Question/Topic? 0
  • +

Replies To: How to Insert value in array

#2 heinbau  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 22
  • Joined: 08-November 12

Re: How to Insert value in array

Posted 25 November 2012 - 10:14 PM

I would love to help, but I can't understand the question.Not to be rude but the grammatical errors make it hard to follow your post.

Could you give us a (short) example of sample input and output you want?

Thanks
Was This Post Helpful? 0
  • +
  • -

#3 prashant.gudadhe  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 3
  • Joined: 08-June 09

Re: How to Insert value in array

Posted 25 November 2012 - 11:09 PM

hi,
i want to answer it,but unable to understand the problem u r facing!
whatever i m getting, that ur problem is very easy but u r presenting it in a harder way. So please explain it in the simplest manner, so that i can help u out of it.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,659
  • Joined: 02-June 10

Re: How to Insert value in array

Posted 26 November 2012 - 07:33 AM

View Postprashant.gudadhe, on 26 November 2012 - 12:09 AM, said:

hi,
i want to answer it,but unable to understand the problem u r facing!
whatever i m getting, that ur problem is very easy but u r presenting it in a harder way. So please explain it in the simplest manner, so that i can help u out of it.


Its kind of ironic that you would say you can't understand the original poster, then fill your post with a bunch of SMS speak. We don't do leet-speak here.

Quote

This can't be stressed enough in today's world of cell phone messaging:
Don't use txt/sms/leet/T9 speak like: u no, u r, dnt, wut i m do-n, coz, al gud, b4, ny1, sum1, pleez and so on like this guy:

This post has been edited by tlhIn`toq: 26 November 2012 - 07:33 AM

Was This Post Helpful? 0
  • +
  • -

#5 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 - 07:59 AM

sorry guys, my bad...hehe...ok, about my problem before this...i can solve it with my friend's help but i am stuck at one part which is to insert value in array...from code below, i just put value in terms[0] = 5 3 for example..just assume for this case value for a is 1 and 2...then, dicFrequentItemsL1[1] = 5 meanwhile dicFrequentItemsL1[2] = 3...

Then, when i'm trying invoke this MessageBox.Show(terms[0].ToString(),"arrays");, it's comes out with 5 3...now, i'm facing a problem when tried to call it from getMaxValue(terms); where in this method, when i invoke MessageBox.Show(maxValue, "maxValue");, it turns out 5 3 as well...supposely, i want it just outcome with index at no.1 which is 5...from that, when program go to if statement, numbers[i] which is assigned with 3 supposely will called...then, program will compare both value and came out with maximum value...then, i will finished..

so, how to do..is it wrong when i assign as terms[0] += sqq + " ";???

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 qq = "";
            string[] terms = new string[l.Length];

            foreach (string v in l)
            {
                string a = v.Replace("\r\n", " ");
                foreach (var s in a)
                {
                    qq = s.ToString();
                    MessageBox.Show(qq, "q");
                    sqq = dic_FrequentItemsL1[qq];
                    MessageBox.Show(sqq.ToString());

                    terms[0] += sqq + " ";
                }
                MessageBox.Show(terms[0].ToString(),"arrays");
                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;
        } 


Was This Post Helpful? 0
  • +
  • -

#6 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: How to Insert value in array

Posted 26 November 2012 - 08:14 AM

You have multiple problems that seem to stem from a basic misunderstanding: Strings are not numbers.
You build a long string (poorly) that contains characters. You then expect these strings to be in different parts of an array even though you only ever reference the [0] array element. You then try to find the maximum value, still treating them as strings, when there is only one string.

You need to review Arrays and how to convert strings to numbers. Go to your instructor now, tell them you don't understand arrays.
Was This Post Helpful? 0
  • +
  • -

#7 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 - 08:26 AM

i understand array but i forget how to put the value of string in arrays...from that, i can call it by arrays[0], arrays[1], arrays[2] and so on...but, i totally lost for this night...thats why, for this post, i just put value in arrays[0] straightaway..i know its wrong...i display it to public so with this clue, you can help me out from this problem..

after i getting this, i will convert string to double or int..now, i just want put the value into arrays...not arrays[0]

This post has been edited by tlhIn`toq: 26 November 2012 - 08:47 AM

Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,659
  • Joined: 02-June 10

Re: How to Insert value in array

Posted 26 November 2012 - 08:45 AM

Quote

from that, i can call it by arrays[0], arrays[1], arrays[2]


Wow - you know enough that you have to refer to each element by an index number... but you can't put anything into those?

myArray[0] = "One"
myArray[1] = "Two"
myArray[2] = "Three"
myArray[3] = "Four"


In a loop you would use some variable for the index

for (int index = 0; index < 10 ; index++)
{
   myArray[index] = index * 2; // just to give it a unique value you can see
}



TIP: You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.
Was This Post Helpful? 0
  • +
  • -

#9 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 - 09:01 AM

yes, you look know when tried to declare array as

myArray[0] = "One"
myArray[1] = "Two"
myArray[2] = "Three"
myArray[3] = "Four"

but, back to my topic, it's sound, how to insert value in array using foreach loop...its means, it done automatically insert into array via foreach loop...not by declaring as you done it.. :scooter:
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,691
  • Joined: 05-May 12

Re: How to Insert value in array

Posted 26 November 2012 - 09:28 AM

This code:
myArray[0] = "One";
myArray[1] = "Two";
myArray[2] = "Three";
myArray[3] = "Four";



Using a foreach:
string [] source = { "One", "Two", "Three", "Four" };
int index = 0;
foreach(string s in source)
    myArray[index++] = s;



I really suspect that you don't have a firm grasp of arrays. Are you coming from a PHP background whose arrays are actually more like the .NET Dictionary?
Was This Post Helpful? 0
  • +
  • -

#11 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 - 09:44 AM

its not like that...i felt want to cried now..hehe...i dont want situation that giving array, then from that, program call it by foreach loop...its not like i wanted....

from the title of topic is, How to Insert value in array using foreach loop is means, from value i got in foreach loop that i got from external source such as two value which is 5 and 3, this value try to insert automatically in new array...

for example, see the code below..the value a is 5 and 3...so, for the first loop, memory will hold value 5...then, i want this value 5 insert in arrays...then for the second loop, memory will insert value 3 in arrays...so that, in arrays now, i have two values which is 5 and 3....where arrays[0] = 5 meanwhile arrays[1] = 3...

foreach (var s in a)
                {
                    qq = s.ToString();
                    MessageBox.Show(qq, "q");
                    sqq = dic_FrequentItemsL1[qq];
                    MessageBox.Show(sqq.ToString());

                    terms[0] += sqq + " ";;
                }



maybe i do some mistakes when declaring

terms[0] += sqq + " ";

i tried with code = terms[] += sqq + " "; but it have error.... :hang:
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,659
  • Joined: 02-June 10

Re: How to Insert value in array

Posted 26 November 2012 - 09:46 AM

View Postpaan, on 26 November 2012 - 10:44 AM, said:

its not like that...i felt want to cried now
Grow up


View Postpaan, on 26 November 2012 - 10:44 AM, said:

terms[0] += sqq + " ";;


Then instead of 0 use a counter variable. An index variable. Like we keep showing you.

This is quickly becoming a "gimme teh codez" post.
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,691
  • Joined: 05-May 12

Re: How to Insert value in array

Posted 26 November 2012 - 09:52 AM

This code will result in arrays[0] containing 5, and arrays[1] containing 3.

int [] a = { 5, 3 };
int [] arrays = new int[a.Length];
int index = 0;
foreach(var s in a)
    arrays[index++] = s;


Was This Post Helpful? 1
  • +
  • -

#14 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 - 10:09 AM

ok, now i already try modify little changes...here, i show two situation that i can get stroke from this..haha...

first situation :
foreach (var s in a)
                {
                    qq = s.ToString();
                    MessageBox.Show(qq, "q");
                    sqq = dic_FrequentItemsL1[qq];
                    MessageBox.Show(sqq.ToString());

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



here, the problem is value 5 can be insert well..but for value 3 is said error arrayoutofbounds...

second situation :
foreach (var s in a)
                {
                    qq = s.ToString();
                    MessageBox.Show(qq, "q");
                    sqq = dic_FrequentItemsL1[qq];
                    MessageBox.Show(sqq.ToString()); 
                }
for (int index = 0; index < a.Length; index++)
                    {
                        terms[index] = sqq.ToString(); // just to give it a unique value you can see
                        MessageBox.Show(terms[index], "terms[index]");
                    }



the problem here, is the value 5 is not inserted because program must finish foreach first then will jump to for loop...it cause only value 3 can be inserted in array due value 3 is a latest value for foreach loop..

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

#15 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,691
  • Joined: 05-May 12

Re: How to Insert value in array

Posted 26 November 2012 - 02:45 PM

Assuming the outer context of the code you posted for situation 1 in post #14 looks like this:
string[] terms = new string[l.Length];

foreach (string v in l)
{
    string a = v.Replace("\r\n", " ");

    foreach (var s in a)
    {
        qq = s.ToString();
        MessageBox.Show(qq, "q");
        sqq = dic_FrequentItemsL1[qq];
        MessageBox.Show(sqq.ToString());

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


Notice how line 1 allocates an array of l.Length items yet on line 14 indexes through a.Length items. Do you have any guarantees that l.Length will always be greater than or equal to a.Length?

I highly recommend stepping back and describing in words what you are trying to achieve. Your code looks like a mish-mash of stuff and it's not clear what you are trying to do. Let me be specific, describe what you are trying to achieve. Don't tell us what your code is trying to do.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2