-2

I'm doing cs50 code about calculating the population of llamas but it stops in the middle and does not return any thing. the code asks the user to enter number of start size and end size then calculate how many years it needs to reach the end size

note 1: the code uses <cs50.h> header.

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    // TODO: Prompt for start size
    int start_size;
    do
    {
        start_size = get_int("enter the start size\n");
    }
    while (start_size < 9);

    // TODO: Prompt for end size
    int end_size;
    do
    {
         end_size = get_int("enter the end size\n");
    }
      while (end_size < start_size);

    // TODO: Calculate number of years until we reach threshold
    int years = 0;
    do
    {
        start_size = start_size + (start_size/3);
        start_size = start_size - (start_size/4);
        years++;
    }
    while (start_size < end_size);

    // TODO: Print number of years
    printf("the needed number of years is: %i\n",years);
}

note 2: I tried to execute the code in online IDE after replacing the get_int with scanf and still not work

UpAndAdam
  • 4,515
  • 3
  • 28
  • 46
saso yoo
  • 3
  • 3
  • 1
    You probably have an infinite loop. Step through the program with a debugger to see why it's getting stuck. Or add `printf()` statements that show the variables you're testing in the `while` condition. – Barmar Sep 01 '23 at 14:56
  • @Someprogrammerdude The question says what happens -- it gets stuck and doesn't print anything. – Barmar Sep 01 '23 at 14:57
  • 2
    A beginners course like CS50 should really teach the difference between libraries and header files. `cs50.h` is a header file. It's included by the compiler to create a [*translation unit*](https://en.wikipedia.org/wiki/Translation_unit_(programming)). Many libraries have header files, but the library itself is something different, and used to *link* the translation units into the executable program. – Some programmer dude Sep 01 '23 at 14:57
  • @Someprogrammerdude There's a CS50 library that goes with the header. The header declares the functions that are defined in the header, like `get_int()`. – Barmar Sep 01 '23 at 14:58
  • ***Where*** does the code seem to get stuck? Is there no output at all? How do you build your program? How do you run it? – Some programmer dude Sep 01 '23 at 14:59
  • 3
    The problem is almost certainly because you're using integers for `start_size` and `end_size`. When you do `start_size/3` it does integer division, so you don't get any fraction. – Barmar Sep 01 '23 at 14:59
  • it is stuck after ask me the second question @Some programmer dude – saso yoo Sep 01 '23 at 15:00
  • 1
    Put `printf("start_size = %d\n", start_size)` into the loop and see what happens. – Barmar Sep 01 '23 at 15:00
  • @Barmer no because it does not continue after second question, i made printf statement after second question and it is not executed – saso yoo Sep 01 '23 at 15:13
  • You don't *know* what happens after the second question, unless you somehow [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your code. Like adding plenty of `printf` calls everywhere (remember to use a trailing newline in the format string, or use `fflush(stdout)` to make sure it's printed). Or use a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to step through the code line by line to see what really happens, while monitoring variables and their values. – Some programmer dude Sep 01 '23 at 15:18
  • Also please include the actual input you give the program. That will make it easier for us to test ourselves. – Some programmer dude Sep 01 '23 at 15:19
  • For things like that it is invaluable to know how to use a debugger. In my opinion, the free Visual Studio Community edition is an excellent offline IDE with an excellent visual debugger (you can set breakpoints and inspect variable values with a mouse click in the editor window). The newer versions support cmake projects. (Microsoft Visual C++ used to have a bad reputation, for example because it was not conformant, but my recent experiences are excellent.) – Peter - Reinstate Monica Sep 01 '23 at 15:36
  • In this case you should use the included debug50 which I'm pretty sure they teach you about in this exact lab's instructions. debug50 is just a program that generates the appropriate `stuff` for the codespace web-based visual studio code instance's debugger. It's kind of annoying that they dont just show you how to setup the debug target from within the IDE IMHO. – UpAndAdam Sep 01 '23 at 16:06
  • @Barmar Closed for the wrong reason. OP code adds 1/3 to current pop resulting in new pop that is 4/4 of original. Then, OP code subtracts from that new pop 1/4 and restores the original value. Problem statement is actually `n1 = n0 + n0/3 - n0/4`... Not a problem of _integer division_. A problem of implementation... – Fe2O3 Sep 01 '23 at 22:29
  • @Fe2O3 I've reopened, although I suspect that integer division is also part of it. In many cases `n0/3 == n0/4` so the addition and subtraction cancel each other. – Barmar Sep 01 '23 at 22:37
  • @Barmar Thanks for re-opening. Have posted an answer, including addressing the required start value being `>=9`... Cheers! :-) – Fe2O3 Sep 01 '23 at 22:49
  • Know CS50 is not a *Learn To Program in C* course. It is simply a set of problems that let you exercise what you have already learned in C. You should be challenged by the *Logic* of the problems, but not the *Language* you will use to solve them. To learn to code in C, you need a good book and reference. Stop by [The Definitive C Book Guide and List](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) to get started. Then learn [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – David C. Rankin Sep 02 '23 at 01:07
  • 1
    @Fe203 your solution was right and well explained, thanks, – saso yoo Sep 02 '23 at 13:27

1 Answers1

1

The OP has not implemented the actual problem statement that uses the year's starting population for BOTH growth AND shrinkage calculations.)

The problem is here:

        start_size = start_size + (start_size/3);
        start_size = start_size - (start_size/4);

This implements the expression: n = 4/3 * n * 3/4 which any kid will tell you is equivalent to
n = 12/12 * n, meaning no increase in population.

The simple fix is NOT using the lvalue of the 1st statement in the 2nd statement. Perform the entire calculation in a single assignment statement:

        start_size = start_size + (start_size/3) - (start_size/4);

Here, the 3 appearances of start_size on the right all have the same value; the current year's starting population.

The minimum of 9 llamas to start ensures that the herd will grow over time. A smaller seed population will not get traction (an increase of at least 1) and will not grow.

EDIT:
Simplified:

        int incr = (start_size/3); // increase by 1/3
        int decr = (start_size/4); // decrease by 1/4
        start_size = start_size + incr - decr;
Fe2O3
  • 6,077
  • 2
  • 4
  • 20