3

Is there a simple method with a loop I could use to walk through both arrays at once? I've been working on this for days and I feel like it's a lot simpler than I'm making it out to be.

Something like..

While statsArray and perDayArray subscripts are less than ten, divide statsArray[x] by days and assign to perdayArray[x]

Also, the statsArray subscripts have previously been assigned by textBoxes.

    private double CalculatePerDay(double stats, int days)
    {
        return stats / days;
    }

        perDayArray[0] = CalculatePerDay(statsArray[0], daysPassed);
        perDayArray[1] = CalculatePerDay(statsArray[1], daysPassed);
        perDayArray[2] = CalculatePerDay(statsArray[2], daysPassed);
        perDayArray[3] = CalculatePerDay(statsArray[3], daysPassed);
        perDayArray[4] = CalculatePerDay(statsArray[4], daysPassed);
        perDayArray[5] = CalculatePerDay(statsArray[5], daysPassed);
        perDayArray[6] = CalculatePerDay(statsArray[6], daysPassed);
        perDayArray[7] = CalculatePerDay(statsArray[7], daysPassed);
        perDayArray[8] = CalculatePerDay(statsArray[8], daysPassed);
        perDayArray[9] = CalculatePerDay(statsArray[9], daysPassed);
        perDayArray[10] = CalculatePerDay(statsArray[10], daysPassed);
1
  • Will they always contain the same number of items? Commented Apr 19, 2016 at 14:33

3 Answers 3

7
for (int i = 0; i < perDayArray.Length; i++)
{
    perDayArray[i] = CalculatePerDay(statsArray[i], daysPassed);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Why not perDayArray.GetLength(0)? (or perDayArray.GetUpperBound(0) - 1)
Yes, it will be even better. Thanks @AlfieGoodacre
This worked perfectly. I feel like I've tried it so many different ways and this was one of them. Must have screwed something up somewhere. Thanks Roma.
7

You could use Linq as follows

perDayArray = statsArray.Select( a => CalculatePerDay( a, daysPassed ) ).ToArray();

1 Comment

@Fabjan - I think leaving it as a function is better, because 1. It's clearer as to what the calculation is doing and 2. there is room in the future to expand the function.
1

I think you want to do this:

for(int i=0; i<= 10; i++)
{
  perDayArray[i] = CalculatePerDay(statsArray[i], daysPassed);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.