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

int main(void)
{
    // TODO: Prompt for start size
    int s;
    do
    {
        s = get_int("Start size : ");
    }
    while (s < 9);

    // TODO: Prompt for end size
    int e;
    do
    {
        e = get_int("End size : ");
    }
    while (e < s);

    // TODO: Calculate number of years until we reach threshold
    
    int n = s;
    int y = 0;
    while (n < e) 
    {
        n = n + n / 3 - n / 4 ;
        
        y++;
    }
    
    // TODO: Print number of years
    printf("Years: %i\n", y);

}

I am able to run the above code perfectly and get the desired results. However when i try to replace the n's calculation part by simplifying the math the code stops working i.e it does not calculate what its intended to calculate and keeps the program in the input taking mode i.e it lets you type in the terminal without giving output. I replaced the n's calculation part with n = (13 * n) / 12

  • 1
    What is `e`? What is `s`? Please provide complete code as a [minimal verifiable example](https://stackoverflow.com/help/minimal-reproducible-example). The way C integer division works may be a relevant factor here but can't be sure without complete code. – kaylum Mar 09 '21 at 06:29
  • user inputs and s > e . – random8mile Mar 09 '21 at 06:32
  • Complete code please. Also give exact input values. And you can help yourself by doing basic debugging. Run the program in a debugger, step thru it and watch the variable values and program flow. – kaylum Mar 09 '21 at 06:33
  • I edited the main post. Beginner here trying to learn code not sure about debugging. Everything is working fine until i try to simplify the n's calculation part. Updated details in main post. – random8mile Mar 09 '21 at 06:35
  • Done. Sorry new here. – random8mile Mar 09 '21 at 06:40
  • "Stops working" is no problem description. In what way does it not work anymore? – Gerhardh Mar 09 '21 at 08:11
  • You have not described what the program is intended to do. – Eric Postpischil Mar 09 '21 at 08:58

1 Answers1

0

Because of integer arithmetics, the expressions n = n + n / 3 - n / 4; and n = n * 13 / 12; are not equivalent: integer division rounds toward zero so for example the first expression increments n from 3 to 4 but not the second expression.

You should use floating point arithmetics for this problem:

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

int main(void) {
    // TODO: Prompt for start size
    int s;
    do {
        s = get_int("Start size: ");
    } while (s < 9);

    // TODO: Prompt for end size
    int e;
    do {
        e = get_int("End size: ");
    } while (e < s);

    // Calculate number of years until we reach threshold
    double n = s;
    int y = 0;
    while (n < e) {
        n = n * 13.0 / 12.0;
        y++;
    }
    
    // TODO: Print number of years
    printf("Years: %i\n", y);
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • When you say integer division rounds towards zero do you mean to say 5 / 2 gives me 2 and not 2.5 and 15 / 4 gives me 3 and not 3.75. If that's the case shouldn't it not repeat the code and increase the counter till n becomes greater than e? That's not what's happening here. When i implement the second case the codes takes second input and does nothing when i input 9 and 18. I have to kill it using ctrl+c as it does not let me type anything else. – random8mile Mar 09 '21 at 06:56
  • @random8mile: yes, unlike javascript, C has different arithmetics for integer types and floating point types: `5 / 2` evaluates to `2` and `5.0 / 2.0` evaluates to `2.5`. In your code, an initial value of `3` causes an infinite loop with `n = n * 13 / 12;` because `3 * 13 / 12* evaluates to `39 / 12` which is rounded down to `3`. so `n` does not change. – chqrlie Mar 09 '21 at 07:01
  • 1
    My code does not take 3 as input but i see what you mean. Its minimum input is 9 but it goes to infinite loop when i enter 9 as the first input as it rounds 9.75 (13 * 9 / 12) to 9. So basically my code goes into infinite loop when user enters certain numbers due to c throwing away the decimal parts.I hope this understanding is right. Thank you for your time buddy!! – random8mile Mar 09 '21 at 07:13