ISSUE WITH PROBLEM 12 - MODULO AND TIME DIFFERENCE

Back to General discussions forum

varunsisodia95     2018-04-20 08:28:48

I have written the following program in C language:

//Modulo and time difference

include<stdio.h>

include<stdlib.h>

int main() { int d1,d2;//Days int h1,h2;//Hours int m1,m2;//Minutes int s1,s2;//Seconds int nodates; int i; long int totalsec1,totalsec2,diffday,diff; int date_arr,hourarr,*minarr,*sec_arr; int rem,min,sec,hr;

printf("\nEnter the number of dates:\n");
    scanf("%d",&no_dates);//3

    date_arr = (int*)malloc(sizeof(int)*no_dates);
    hour_arr = (int*)malloc(sizeof(int)*no_dates);
    min_arr = (int*)malloc(sizeof(int)*no_dates);
    sec_arr = (int*)malloc(sizeof(int)*no_dates);

    printf("\nEnter %d dates:\n",no_dates);

    for(i=0;i<no_dates;i++)
    {
            scanf("%d %d %d %d %d %d %d %d",&d1,&h1,&m1,&s1,&d2,&h2,&m2,&s2);//

            diff_day = d2-d1;
            date_arr[i] = diff_day;

            total_sec1 = (h1*3600)+(m1*60)+s1;

            total_sec2 = (h2*3600)+(m2*60)+s2;

            if(total_sec1>total_sec2)
                diff = total_sec1 - total_sec2;
            else
                diff = total_sec2 - total_sec1;

            hr = diff/3600;
            hour_arr[i] = hr;

            rem = diff%3600;

            min = rem/60;
            sec = rem%60;
            min_arr[i] = min;

            sec_arr[i] = sec;

            min = 0;
            hr = 0;
            sec = 0;
            rem = 0;


    }

    printf("\nThe results are as follows:\n");

    for(i=0;i<no_dates;i++)
    {
        printf("(%d %d %d %d)\t",date_arr[i],hour_arr[i],min_arr[i],sec_arr[i]);
    }


return 0;

}

My output was: Enter the number of dates: 12

Enter 12 dates: 4 1 57 54 18 2 46 31 7 11 20 3 15 19 51 13 2 14 34 58 10 20 0 1 13 1 36 35 28 23 1 32 7 4 50 6 24 22 37 3 10 15 47 25 29 17 6 12 5 13 36 48 26 7 53 38 20 0 37 43 28 18 23 55 0 13 15 27 19 10 23 32 6 13 31 40 20 13 13 3 25 1 41 25 28 6 33 38 6 0 38 25 28 15 22 53

The results are as follows: (14 0 48 37) (8 8 31 10) (8 5 25 3) (15 21 24 57) (17 17 46 57) (19 1 18 47) (21 5 43 10) (8 17 46 12) (19 2 51 55) (14 0 18 37) (3 4 52 13) (22 14 44 28)

All values except (21 5 43 10) (19 2 51 55) (14 0 18 37) are CORRECT......

If there was an issue with the logic of my program, the entire output must be wrong...

However, despite all my efforts I am unable to figure out what is the issue with the code.

Any suggestions for improvement will be greatly apperciated.

Quandray     2018-04-20 09:34:10
User avatar

A) It is simpler, if your code prints the output for each test case as you process the test case. You don't need those arrays to store the output in.
B) Your code fails on 3 test cases. The first test case it fails on is 5 13 36 48 26 7 53 38 where your answer is (21 5 43 10) but the correct answer is (20 18 16 50). To get from 48 to 38 takes 50 seconds, not 10 seconds.

Juanchobanano     2018-08-24 22:23:30
User avatar

I'm having the same issue with this problem: when I'm trying to get the time difference between 2 dates, I apply the round() function onto the remainder of the multiple divisions my program applies to the total number of seconds between both dates. I also tried to apply int() to the remainder, but it didnt work either.

Result expected: (14 4 0 54) (18 3 11 22) (6 1 9 31) (2 14 11 35) (6 7 16 44) (20 6 44 32) (1 8 41 10) (7 22 56 17) (8 17 46 5) (6 11 40 49) (11 21 54 46) (12 17 29 16) (15 12 3 9) (15 12 54 40) My answer: (14 4 1 54) (18 3 11 22) (6 1 10 31) (2 14 12 35) (6 7 17 44) (20 7 45 32) (1 9 41 10) (7 23 56 17) (8 18 46 5) (6 12 41 49) (11 22 55 46) (12 17 29 16) (15 12 3 9) (15 13 55 40)

In the firt case, the expected output was: (14 4 0 54), however, my program printed (14 4 1 54) based on the following remainders:

round(54.00000000008731) = 54, round(0.8999999999991815) = 1 (however, expected value was 0, how is it possible? ), round(4.014999999999972) 4, round(14) = 14

I hope you can help me.

Please login and solve 5 problems to be able to post at forum