0

this is the program in question:

#include <stdio.h>
#include <tgmath.h>
#include <simpio.h>

long main(){
    long cars_t,years;
    cars_t=80000;
    years=0;
    while (cars_t<=160000){
        cars_t=ceil(cars_t*(1+7/100));
        years+=1;
    }
    printf("Xronia:%ld\n",years);
    printf("Arithmos Autokinhton:%ld\n",cars_t);
}

Just an extremely simple program with a while function. But for some reason it doesn't give any output at all. What i have noticed however is that, the moment i remove the while function(and the code inside of it) the program runs perfectly fine. Can anyone tell me how to fix this? thanks in advance.

6
  • 6
    cars_t*(1+7/100) this is a pretty slow rate of growth to start with, but regardless, 7/100 equals 0 due to integer division, 1 + 0 = 1, and thus you are simply assigning the value of cars_t to itself and it likely isn't even increasing. To avoid this, at least do cars_t=ceil(cars_t*(1+7.0/100)) Commented Nov 1, 2021 at 14:58
  • Yup it worked. Thanks. Believe it or not, this is actually a university project, im on my first year of computer science and they are pretty much starting us off with the basics. Commented Nov 1, 2021 at 15:04
  • 3
    Also, the return type from main() should be int. Commented Nov 1, 2021 at 15:04
  • 1
    You should have put an appropriate printf into the loop and you would have found out yourself. BTW a program compile without error or warnings is by no means a guarantee that it runs as expected. Commented Nov 1, 2021 at 15:14
  • ^^^^^^ 'first year of computer science and they are pretty much starting us off with the basics' - demand to be taught basic debugging before you write one more line of code. If you cannot debug, you cannot program computers:) Commented Nov 1, 2021 at 16:41

1 Answer 1

4

This is because you have declared cars_t as an integer (long) value, and 7/100 which are also also integers and so evaluate to zero. Hence you get stuck in the loop as cars_t does not increase.

Instead you want cars to be a floating value and force 7/100 to be evaluated as floating:

#include <stdio.h>
#include <tgmath.h>

long main(){
    long years;
    double cars_t;
    cars_t=80000;
    years=0;
    while (cars_t<=160000){
        cars_t=ceil(cars_t*(1+7.0/100));
        years+=1;
    }
    printf("Xronia:%ld\n",years);
    printf("Arithmos Autokinhton:%lf\n",cars_t);
}

https://onlinegdb.com/vNusyZ9xD

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.