0

I am new to C programming, but experienced in Java. I am creating a simple console application to calculate time between two chosen values. I am storing the chosen values in an int array like this:

static int timeVals[] = {748,800,815,830,845,914,929,942,953,1001,1010,1026,1034,1042,1048};

I am calling a method diff to calculate the time between to values liek this:

int diff (int start, int slut) {
/* slut means end in danish. not using rude words, but I am danish */
int minutes = 0;
int time = 0;
double hest;
hest = (100/60);
printf("hest = %f", hest);
if(timeVals[start] > timeVals[slut])
{
    minutes = timeVals[start] - timeVals[slut];
    /* printing hest to see what the value is */
    printf("t = %f",hest);
    time = minutes * (100/60);
    printf("minut diff: %d\n", time);
}
else
{
    minutes = timeVals[slut] - timeVals[start];
    tiem = time + (minutes * (100/60));
}

return time;
}

The weird thing is that when I print out hest value I get 1.000000, which I mean isn't right... I have been struggling with this for hours now, and I can't find the issue.. maybe I'm just bad at math :P

hope you can help me

Zuenonentu
  • 302
  • 1
  • 3
  • 16

3 Answers3

1

The issue is

hest = (100/60)

This result will be 1 because 100 / 60 = 1.6666...., but this is integer division, so you will lose your decimals, so hest = 1. Use

hest = (100.0 / 60.0)

Same with

time = minutes * (100/60);

Changed to

time = minutes * (100.0 / 60.0);

In this case again, you will lose your decimals because time is an int.

Some would recommend, if speed is an issue, that you perform all integer calculations and do store all your items as ints in 1/100th's of a second (i.e. 60 minutes in 1/100ths of a second = 60 minutes*60 seconds*100)

EDIT: Just to clarify, the link is for C++, but the same principles apply. But on most x86 based systems this isn't as big of a deal as it is on power limited embedded systems. Here's another link that discusses this issue

Community
  • 1
  • 1
StephenH
  • 1,076
  • 10
  • 23
1

Following statement is a NOP

time = minutes * (100/60);

(100 / 60) == 1 because 100 and 60 are integers. You must write this :

time = (minutes * 100) / 60;

For instance if minutes == 123 time will be calculated as (123 * 100) / 60 which is 12300 / 60 which is 205.

Jabberwocky
  • 40,411
  • 16
  • 50
  • 92
0

As stated, you are mixing floating point and integer arithmetic. When you divide two integers, your result is integer, but you are trying to print that result as float. You might consider using the modulus operator (%) and compute quotient and remainder,

int // you might want to return float, since you are comingling int and float for time
diff (int start, int slut)
{
    /* slut means end in danish. not using rude words, but I am danish */
    int minutes = 0, seconds = 0, diff;
    int time = 0;
    double hest = (100.0) / (60.0); //here
    printf("hest = %f", hest);
    if(timeVals[start] > timeVals[slut])
    {
        diff = timeVals[start] - timeVals[slut];
        time = diff * (100.0/60.0);
        minutes = diff/60;
        seconds = diff%60; //modulus (remainder after division by 60
    }
    else
    {
        diff = timeVals[slut] - timeVals[start];
        time = time + diff * (100.0/60.0);
        minutes = diff/60;
        seconds = diff%60; //modulus (remainder after division by 60
    }
        /* printing hest to see what the value is */
        printf("t = %f",hest);
        printf("minut:seconds %d:%02d\n", minutes, seconds );
        printf("minut diff: %d\n", time);

    return time;
}
ChuckCottrill
  • 3,999
  • 2
  • 22
  • 34