BFMracing

General Category => General Board => Homework Haven => Topic started by: Dusaro on October 01, 2012, 11:12:35 PM

Title: More help needed with C# :(
Post by: Dusaro on October 01, 2012, 11:12:35 PM
ok, i have the general idea of how to do it but i cant figure out how to fix this...

Code: [Select]
            //birth Week
            List<int> bW = new List<int>();
            //Weight
            List<int> W = new List<int>();
            //Averag
            List<int> Av = new List<int>();

            //Number Weights Average
            int nwa = 0;
            //Average Weight
            int Avw = 0;

            //Adding values to birth Week
            bW.Add(37);
            bW.Add(37);
            bW.Add(37);
            bW.Add(40);
            bW.Add(39);
            bW.Add(38);
            bW.Add(40);
            bW.Add(39);
            bW.Add(38);
            bW.Add(36);

            //Adding values to Weight
            W.Add(3791);
            W.Add(3893);
            W.Add(3893);
            W.Add(3041);
            W.Add(2625);
            W.Add(3737);
            W.Add(2929);
            W.Add(3378);
            W.Add(2149);
            W.Add(2250);

            //foreach value in bW
            foreach (int i in bW)
            {
                //if value in bW is more than 37 or less than 39
                if (i >= 37 && i <= 39)
                {
                    //add one to Number Weight Average
                    nwa++;
                }
            }

            //foreach value in bW
            foreach (int i in bW)
            {
                //if value in bW is more than 37 or less than 39
                if (i >= 37 && i <= 39)
                {
                    //foreach value in W
                    foreach (int i1 in W)
                    {
                        //MessageBox.Show(i1.ToString());
                        //Add to Av List
                        Av.Add(i1);
                    }
                }
            }
            //set Avw to average of Avw
            Avw = Convert.ToInt16(Av.Average());

            //return results
            textBox1.Text = "Number of weights averaged = " + nwa.ToString() + "\r\n" + "Average weight = "+Avw;

            //MessageBox.Show(Av.Count().ToString());

Basically im trying to get the average weight in the list W where the birthWeek in bW is more than 37 and less than 39.

Please help! :)
Title: Re: More help needed with C# :(
Post by: jim360 on October 02, 2012, 02:04:11 AM
Again, on a general note please give your variables word names if possible - makes it so much easier to follow. So "Birthweek.list" rather than "bw.list", etc. Unless there is some horrible reason I'm unaware of that this would not be allowed in C#.

For the actual questions, has it been established anywhere that the two lists correspond? So that in the first birth week listed = the first weight given. I can't see that it does.

Once you've fixed that, why not merge the two foreach commands? so:

Code: [Select]
            //foreach value in bW
            foreach (int i in bW)
            {
                //if value in bW is more than 37 or less than 39
                if (i >= 37 && i <= 39)
                {
                    //foreach value in W
                    foreach (int i1 in W)
                    {
                        //MessageBox.Show(i1.ToString());
                        //Add to Av List
                        Av.Add(i1);
                    }
               nwa++; //Add one to number weight average
                }
            }

I think you should be able to do that and save on about ten or so lines of code.

But as to the main problem, I think it might just be that the computer doesn't understand that the two lists are paired up. You might want to look into using something called a Tuple, which can be used to created ordered pairs. Then instead of having the two separate lists 37, 37, 37... and 3791, 3893..., they are merged into one as (37, 3791), (37, 3893) ... That way you could have a code that asks, "if the first number in the ordered pair is 37, 38 or 39, then add the second number to the list of numbers to be averaged."

I think that should work.
Title: Re: More help needed with C# :(
Post by: BFM_Fénix on October 02, 2012, 07:00:24 AM
If the values of BirthWeek and Weight correspond to each other, a simple (shorter code) way of getting the W for any bW>37 && <39 is to get the index of the bW list and then use it to find the corresponding element in the W list, finally adding that to the Av list. I don't know C# but there must be a method to do so (just google it :D ).

Also, as you said, the program has to get the bW values for more than 37 and less than 39, so the conditional if should have i>37 && i<39.
Title: Re: More help needed with C# :(
Post by: Dusaro on October 02, 2012, 02:23:05 PM
Yeah, what Fenix said is what i thought of this morning as i woke up lol, im about to try it now.
Title: Re: More help needed with C# :(
Post by: Dusaro on October 02, 2012, 03:31:21 PM
Fixed it:
Code: [Select]
for (int i = 0; i < bW.Count(); i++)
            {

                if (bW[i] >= 37 && bW[i] <= 39)
                {
                    weightSum += W[i];
                    count++;
                }
            }
            weightSum /= count;