Print 

Author Topic: More help needed with C# :(  (Read 4144 times)

Offline Dusaro

  • Junior Poster
  • **
  • Posts: 150
  • AWPS are for n00bs
More help needed with C# :(
« 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! :)






Dusaro/FM's Known HB History
Nov 13, 2010: Joined HB
UNKNOWN PERIOD
Jul 28, 2011: Reapplied for HB
Jul 29, 2011: ReAccepted into HB
Jul 31, 2011: RePromoted to Forum Moderator(Field Marshall)
Jan 30, 2012: Promoted to Co-Leader
July 27, 2012: HB Disbanded
Dec 2, 2012: HB Reopened
Dec 2, 2012: Became Advisor for Anarchy(new leader)



Offline jim360

  • Posts Too Much
  • *****
  • Posts: 6847
Re: More help needed with C# :(
« Reply #1 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.
Check out my Short introduction... corner and my "Historical figures who should perhaps be better-known" thread!!

Exciting videos: 1.1 / 1.2 / 2 / 3 / 4 / 5 / 6



              

Offline BFM_Fénix

  • BFM Admin
  • *
  • Posts: 1396
  • I shall be reborn from the ashes
Re: More help needed with C# :(
« Reply #2 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.
« Last Edit: October 02, 2012, 03:14:18 PM by bfm_Fénix »
2000th member of the BFM forums.
According to Hlao, and Hlao's word is law.


Offline Dusaro

  • Junior Poster
  • **
  • Posts: 150
  • AWPS are for n00bs
Re: More help needed with C# :(
« Reply #3 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.






Dusaro/FM's Known HB History
Nov 13, 2010: Joined HB
UNKNOWN PERIOD
Jul 28, 2011: Reapplied for HB
Jul 29, 2011: ReAccepted into HB
Jul 31, 2011: RePromoted to Forum Moderator(Field Marshall)
Jan 30, 2012: Promoted to Co-Leader
July 27, 2012: HB Disbanded
Dec 2, 2012: HB Reopened
Dec 2, 2012: Became Advisor for Anarchy(new leader)



Offline Dusaro

  • Junior Poster
  • **
  • Posts: 150
  • AWPS are for n00bs
Re: More help needed with C# :(
« Reply #4 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;






Dusaro/FM's Known HB History
Nov 13, 2010: Joined HB
UNKNOWN PERIOD
Jul 28, 2011: Reapplied for HB
Jul 29, 2011: ReAccepted into HB
Jul 31, 2011: RePromoted to Forum Moderator(Field Marshall)
Jan 30, 2012: Promoted to Co-Leader
July 27, 2012: HB Disbanded
Dec 2, 2012: HB Reopened
Dec 2, 2012: Became Advisor for Anarchy(new leader)



Print