Ben Lawlor
Ben Lawlor

Reputation: 15

Nested For loop eternally running?

I'm having a little trouble figuring out a nested for loop here. Here's the problem:

  1. The population of Ireland is 4.8 million and growing at a rate of 7% per year. Write a program to determine and display the population in 10 years time. Your program should also display a count of the number of years that the population is predicted to exceed 5 million.

And here's what I've coded so far:

    double pop = 4.8;
    int years = 0;
    for (int i = 0; i < 10; i++)
    {
        pop += (pop / 100) * 7;

        for (int j = 0; pop >5; j++)
        {
            years += j;
        }
    }

    Console.WriteLine("Pop in year 2030 is " + Math.Round(pop, 2) + " million and the population will be over 5 million for " + years + " years.");
}

Now to be clear, I need the sum of the years in which population exceeds 5(million) (the answer is 10) and I must only use for loops to solve this problem. I'm thinking the answer is a nested for loop, but have tried and tried with no success.

Can anyone help me on where I've screwed this up?

Upvotes: 0

Views: 102

Answers (3)

PierreDuv
PierreDuv

Reputation: 124

If I well understand your question. Technically if you add 7% each year your program should look like this

bool passed = true;
double pop = 4.8;
int year = 0;
for (int i=0; i<10; i++)
{
 pop *= 1.07;
if (pop > 5 && passed)
{
 year = i;
 passed = false;
}
}

Upvotes: 0

Michał Turczyn
Michał Turczyn

Reputation: 37347

First, you need to clearer variable names in my opinion. Of course, don't go to far with that, but IMO population will be better than just pop.

Furhter, you could use Dictionary to store years alongside with preidcted population.

Also, write comments to better see what's going on :)

With this suggestions, I would code this like below:

double population = 4.8;
int currentYear = 2020;
Dictionary<int, double> predictions = new Dictionary<int, double>();
// Adding current state
predictions.Add(currentYear, population);

// I changed bounds of a loop to handle years in more comfortable way :)
for (int i = 1; i <= 10; i++)
{
  population *= 1.07;
  predictions.Add(currentYear + i, population);
}

Now you have data in Dictionary that looks like this:

2020  4.8
2021  ...
2022  ...
...   ...

Now you can get all years, where population was over 5 million easily with predictions.Where(kvp => kvp.Value > 5).

Upvotes: 0

cHao
cHao

Reputation: 86506

Look at your inner loop.

    for (int j = 0; pop >5; j++)
    {
        years += j;
    }

Your condition being pop > 5, you need pop to shrink if you ever want to exit the loop. But the body of the loop never alters pop, so if it's greater than 5, you'll loop forever.

The problem definition suggests that you don't need an inner loop at all. Just check pop, and if it's greater than 5, increment years.

    if (pop > 5)
    {
         ++years;
    }

If you're really under such an insane restriction that you can't use if, you could do something boneheaded like create a for loop that only runs once if your other condition is right.

    for (int j = 0; j < 1 && pop > 5; ++j)
    {
        ++years;
    }

but no sane person does this.

Upvotes: 1

Related Questions