0

I am stuck at this one. I just need somekind of a hint to move on I guess. I do not want to take a look at "Walkthrough". I need help with for loop. I cannot think of any condition that should be there. I mean I would like to use startSize >= endSize because in that case loop will be true so it will stop right ? Any hint much apreciated!

#include <cs50.h>

#include <stdio.h>

int main(void)

    int startSize;
    do
    {
        startSize = get_int("Start size: ");
    }
    while (startSize <= 9);

    int endSize;
    do
    {
        endSize = get_int("End size: ");
    }
    while (endSize <= startSize);

    for(int n = 1; ; n++)
    {
            int x; // current population
            x = startSize + (startSize / 3) - (startSize / 4); 
            printf("%d\n", x);
    }
}

1 Answer 1

1

first time posting here so pardon me if I don't have the right style. ;)

Your start- and endSize prompts are perfectly fine. Though, the specific task does allow for a startsize greater or equal to 9. And EndSize is allowed to be equal to startsize, just not smaller. You'd see this if you get to the CS50 check-tool.

What is missing in your code is a condition in your for-loop, so your loop will run forever. Hit Ctrl+C to cancel code execution if you get stuck in a loop.

Also, there are some logical flaws in your current population calc. Go look at the video in the Section part where this whole problem is described in detail, including solution: https://cs50.harvard.edu/x/2023/sections/1/

Hints: if needed { track iterations for as long as required to size > end as years } or else..? And remember that as the population increases, so does the newborns and dearly departed.. ;)

EDIT: I see now you were already pointing directly to your missing condition in For-loop. My suggestion would be to iterate for as long as (while) startSize < EndSize instead. I don't see an apparent solution with a for-loop either.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .