CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2011
    Posts
    1

    Loop wont work correctly

    Hi am currently trying to get my c# console code to read from array and do some calculations. iv done most of it now. I am having a problem finding a year and calculating the standard deviation of the data.

    The problem occurs when it checks the year array to see if it matchs the variable. Its asif it does not because it skips out the rest of the code in the case. My code is below, id be grateful if someone could look at it for any errors that may cause this

    case 12:

    Console.WriteLine();
    Console.WriteLine("Please enter the year?");
    year = Console.ReadLine();


    float rainTotalCase12 = 0;
    for (int i = 1; i < weatherData.GetLength(0); i++)
    {
    //match the year
    if (weatherData[i, 0] == year) // PROBLEM IS HERE, IT WONT FIND THE YEAR
    {
    rainTotalCase12 = rainTotalCase12 + float.Parse(weatherData[i, 5]);
    Console.WriteLine(rainTotalCase12);
    }

    // calculate the mean
    float rainMeanCase12 = (float)rainTotalCase12 / weatherData.GetLength(0);

    // calculate the variance
    double rainVarianceCase12 = 0;
    if (weatherData[i, 0] == year)
    {
    // variance is the squared sum of the differences between the
    // rolled value and the mean of all the dice rolls
    rainVarianceCase12 = rainVarianceCase12 +
    ((float.Parse(weatherData[i, 5]) - rainMeanCase12) * (float.Parse(weatherData[i, 5]) - rainMeanCase12));
    }

    // calculate the standard deviation (square root of the bias
    // corrected variance)
    double rainStdDevCase12 = Math.Sqrt(rainVarianceCase12 / (weatherData.GetLength(0) - 1));
    Console.WriteLine("{0,-15:0}{1,-15:0}{2,-15:0}", "Total Rainfall", "Rainfall Mean", "Rainfall Standard Deviation ");
    Console.WriteLine("{0,-15:0.0}{1,-15:0.0}{2,-15:0.0}", rainTotalCase12, rainMeanCase12, rainStdDevCase12);
    Console.ReadLine();
    }



    break;

  2. #2
    Join Date
    Dec 2008
    Posts
    144

    Re: Loop wont work correctly

    Please put code inside code tags ([ code ] [ /code ] without spaces)- it makes it much easier to read your code.

    The code you provided does not show initialization of weatherData[], so I don't know anything about this beyond the fact that it is an array.

    If you just want to see if the value the user inputs is in the collection, then it would be much easier to use a List than an array- Lists have a Contains method- just call that method and pass the user input and you get a Bool if the List contains the specified parameter. Much simpler than your loop.
    Code:
    if (Issue.Resolved)
    {
         ThreadTools.Click();
         MarkThreadResolved();
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured