# Year with greatest pop. change

• (2 Pages)
• 1
• 2

## 15 Replies - 1138 Views - Last Post: 24 September 2013 - 06:19 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=329931&amp;s=3ca2d700683c20bc8beb7e571d331326&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Wizmeister

Reputation: 1
• Posts: 44
• Joined: 17-October 12

# Year with greatest pop. change

Posted 24 September 2013 - 04:00 PM

Find the year with the greatest population change, Given a list of 41 populations each representing a year. I have to find the year with the greatest increase in population during the time period. I don't completely understand the question.

```        private void btnGo_Click(object sender, EventArgs e)
{
read = File.OpenText("USPopulation.txt");                   // Open the file
List<int> popList = new List<int>();                        // New list, type of double

/*
*  Loop that adds the text-file numbers to the list
*/
{

} // end of loop

int numberOfYears = popList.Count();                        // Number of years

int averageChange = AverageChange(popList, numberOfYears);  // Calling method to get average change, passing list and number of years
lblChange.Text = averageChange.ToString();                  // Assign the variable to the appropriate label, change visibility to true
lblChange.Visible = true;

int biggestChange = BiggestChange(popList, numberOfYears);

} // End of button

private int BiggestChange(List<int> popList, int years)
{
int year = 0;
int max
return year;

}

private int AverageChange(List<int> popList, int years)
{
int lastItem = popList.Last();
int firstItem = popList.First();
int average = (lastItem - firstItem) / years;

return average;
}

}
}
```

I was going to use a method (BiggestChange) to do so, Im guessing they mean find between what two years was the biggest change. At first I was just going to find thee biggest average change in population from the initial year. using,

``` int max = popList.max() / popList.First();
```

then I realised that, that is not what they are asking. the list of populations is

151868
153982
156393
158956
161884
165069
168088
171187
174149
177135
179979
182992
185771
188483
191141
193526
195576
197457
199399
201385
203984
206827
209284
211357
213342
215465
217563
219760
222095
224567
227225
229466
231664
233792
235825
237924
240133
242289
244499
246819
249623

Is This A Good Question/Topic? 0

## Replies To: Year with greatest pop. change

### #2 Michael26

• May the Schwartz be with you

Reputation: 395
• Posts: 1,601
• Joined: 08-April 09

## Re: Year with greatest pop. change

Posted 24 September 2013 - 04:07 PM

Quote

Find the year with the greatest population change, Given a list of 41 populations each representing a year.

Shouldn't there be a year besides your population?
Like
Population Year
151868 2002

``` int numberOfYears = popList.Count(); //Count() returns number of elements in List
```

what is that supposed to do?

This post has been edited by Michael26: 24 September 2013 - 04:09 PM

### #3 Wizmeister

Reputation: 1
• Posts: 44
• Joined: 17-October 12

## Re: Year with greatest pop. change

Posted 24 September 2013 - 04:12 PM

Line one of the list of populations is considered the starting year, (1950) and the last line of the populations is considered the final year (1990). The line you pointed out just gets the total number of items in the list, so the total number of years. I used it for my AverageChange method when finding the average change in population throughout the whole list.

The question im asking is how do i find the two populations with the greatest change between them

### #4 Michael26

• May the Schwartz be with you

Reputation: 395
• Posts: 1,601
• Joined: 08-April 09

## Re: Year with greatest pop. change

Posted 24 September 2013 - 04:18 PM

Count method doesn't add up or total. If you have range between 1950 and 1990 Count won't add those number for your average method. It just counts elements, if there are 10 elements it will return 10, if there are 100 elements in array it will return 100.

This post has been edited by Michael26: 24 September 2013 - 04:19 PM

### #5 Wizmeister

Reputation: 1
• Posts: 44
• Joined: 17-October 12

## Re: Year with greatest pop. change

Posted 24 September 2013 - 04:22 PM

1950 and 1990 were just examples, Count gets the number of elements, yes. Which would represent the number of years

### #6 Michael26

• May the Schwartz be with you

Reputation: 395
• Posts: 1,601
• Joined: 08-April 09

## Re: Year with greatest pop. change

Posted 24 September 2013 - 04:35 PM

What i would do is get first element and next one, check the difference, write it, now do that for every element in list, then pick largest percentage in there and that would be your answer.

### #7 Wizmeister

Reputation: 1
• Posts: 44
• Joined: 17-October 12

## Re: Year with greatest pop. change

Posted 24 September 2013 - 04:46 PM

what im trying to do is something like this.

secondnumber - firstnumber = change

change would then become the highest

then test

thirdnumber - secondnumber =nextchange ( or something else like that) then test if

nextchange > then change, then nextchange will become change. if it is less than change, itll skip over to next set and leave change untouched.

then do this so on like

4th number - 3rd number = nextchange, then test it again..

do this until we have tested all the numbers then change should be the biggest number

### #8 Michael26

• May the Schwartz be with you

Reputation: 395
• Posts: 1,601
• Joined: 08-April 09

## Re: Year with greatest pop. change

Posted 24 September 2013 - 05:13 PM

Did you try what you just wrote in code.
firstnumber would be index 0, second number would be index 1, change and nextChange could be temp int values. Do the
```//If true change, if false increment firstnumber and secondnumber to the next indecies in the array and do the comparison again, do until you reach last elements
nextChange > change ? true : false
```

BTW ? expression is explained here

This post has been edited by Michael26: 24 September 2013 - 05:15 PM

### #9 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 4998
• Posts: 9,049
• Joined: 08-June 10

## Re: Year with greatest pop. change

Posted 24 September 2013 - 05:21 PM

The simple answer is to store a maximum year/change inside your loop and compare each to it.

So, let's break down the logic. We know it can't be the first year, because there is no change (you don't have a previous year to compare it to). So we can exclude the year at index [0].

Next, we know that if we use a for-loop, the year of the population we're looking at is equal to (1950 + i). So from that we can know the year we're currently checking. year = 1950 + i

Since we can exclude the first year, we can start our loop from 1 instead of 0. This has the added bonus of meaning we can always look back to the previous year's population, which we need to calculate the change.

```change = population[i] - population[i - 1]
```

Now that we have the information we need, we have to store it in a variable that won't get re-created each time the loop iterates. That means you have to declare variables to store the max-year and max-change outside the loop. You can start both of these at 0. Each time after you calculate the change, compare the change to the max-change. If the new change is higher, store the new change in the max-change variable, and remember to also store the current year in the max-year.

Once the loop exits, you will have the biggest change in your max-change variable, and the year that goes with it.

I hope that helps, because we cannot provide code for assignments.

### #10 Michael26

• May the Schwartz be with you

Reputation: 395
• Posts: 1,601
• Joined: 08-April 09

## Re: Year with greatest pop. change

Posted 24 September 2013 - 05:28 PM

Curtis you just proven to me that my brain suck after 2AM
I understand your solution, maybe i would have done something similar during normal hours.

### #11 Wizmeister

Reputation: 1
• Posts: 44
• Joined: 17-October 12

## Re: Year with greatest pop. change

Posted 24 September 2013 - 05:29 PM

scratch this post. Didn't realize more was posted

This post has been edited by Wizmeister: 24 September 2013 - 05:31 PM

### #12 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 4998
• Posts: 9,049
• Joined: 08-June 10

## Re: Year with greatest pop. change

Posted 24 September 2013 - 05:30 PM

@Michael26, If I had the right degree I would take up a part time job teaching programming at a local community college. I love to simplify concepts and break complicated tasks down into simple logic, and I love helping other people learn to do so as well.

### #13 Wizmeister

Reputation: 1
• Posts: 44
• Joined: 17-October 12

## Re: Year with greatest pop. change

Posted 24 September 2013 - 06:00 PM

```private int BiggestChange(List<int> popList, int years)
{
int maxChange = 0;
int changeYear = 0;
int i;
int change = 0;
int year = 0;

for (i = 1; i <= popList.Count(); i++)
{

change = popList[i] - popList[i - 1];
year = i;

if (change > maxChange)
{
maxChange = change;
changeYear = i;
}

} // end of for

return changeYear;

} // end of method BiggestChange
```

Getting index was out of range. mus be non negative and less than size of collection.

in debugging, i noticed within the loop that i was 41 and then [i-1] was 40. Im pretty sure i was started at 1, so the list should of picked up 1, the second pop. count

### #14 Wizmeister

Reputation: 1
• Posts: 44
• Joined: 17-October 12

## Re: Year with greatest pop. change

Posted 24 September 2013 - 06:06 PM

Okay so i fixed it to display right by changing the line
```for (i = 1; i <= popList.Count(); i++)
```

to

```for (i = 1; i < popList.Count(); i++)
```

My code works, I have no idea how

### #15 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 4998
• Posts: 9,049
• Joined: 08-June 10

## Re: Year with greatest pop. change

Posted 24 September 2013 - 06:08 PM

Quote

```i <= popList.Count()
```

The problem is here. Remember that in C#, arrays start at zero. Because of this, the last index in the array is one less than the number of items in the array. Which means that in a 41 item array, the last index is 40.

What most programmers that use C-style languages do is write loops like this:

```for(int i = 0; i < array.Length; i++)...
```

The difference is that we only go while the loop counter is less than the max.

However, in this case, we want to skip the first item in the array. So instead of i = 0, we use i = 1. Everything else stays the same.