Why the errors...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Aerogroove
    FFR Player
    • Jul 2004
    • 140

    #1

    Why the errors...

    Ok I'm having problems with part of a project im working on, so i figure ill paste the problem section of code:

    private void stddevButton_Click(object sender, System.EventArgs e)
    {
    displayBox.Items.Clear();
    double ave,sum,mean,temp = 0;
    int i,i2 = Convert.ToInt32(inputBox.Text);
    int[] init = new int [i];
    Random rand;
    rand = new Random();

    while(i>0)
    {
    mean = init[i-1];
    mean = rand.Next(20);
    ave += mean;
    i--;
    }

    i = Convert.ToInt32(inputBox.Text);

    while(i>0)
    {
    temp = init[i-1];
    temp = temp - mean;
    temp *= temp;
    sum += temp;
    i--;
    }

    sum = Math.Sqrt(sum);
    displayBox.Items.Add(sum);
    }
    Ok so what this does is the user enters a number in the textbox, then an array is made of the length that the user entered, random values fill the array, then it takes the standard deviation of the array values.

    The variables in the bolded spot are giving me the errors "Use of unassigned local variable" I dont know why that shows up for certain variables in only certain spots.
  • korny
    It's Saint Pepsi bitch
    • May 2004
    • 4385

    #2
    RE: Why the errors...

    I'd help if i only knew ONE thing about any of this stuff you guys talk about.

    Comment

    • Squeek
      let it snow~
      • Jan 2004
      • 14444

      #3
      RE: Why the errors...

      Then don't post -.-

      Simple. Assign those values to zero in the class you're using them for before you reassign them. I am pretty sure this is the problem.

      ~Squeek

      Comment

      • QreepyBORIS
        FFR Player
        • Feb 2003
        • 7454

        #4
        RE: Why the errors...

        If you don't do that, then I think you just get whatever was in the memory space from before the program was run. Then weird things happen.

        UnlessI am way off here. >_>

        Signature subject to change.

        THE ZERRRRRG.

        Comment

        • Aerogroove
          FFR Player
          • Jul 2004
          • 140

          #5
          RE: Why the errors...

          yeah i corrected the problem earlier, a friend told me you cant initialize and set all the doubles to zero in the same line like i had them. they need their own lines.

          and my standard deviation code was off, guess no one noticed :P
          anyway thats fixed too now
          thanks for your help

          Comment

          • chickendude
            Away from Computer
            FFR Simfile Author
            • Sep 2003
            • 1901

            #6
            RE: Why the errors...

            I'm pretty sure you could do this

            double ave=0,sum=0,mean=0,temp = 0;

            so they don't have to be on different lines, but It may look nicer that way.
            I put variables that are related in the same declaration, but if they are completely unrelated, I use different lines.

            That's just my way though

            Comment

            Working...