Why this function doesent work correctly...??

Discussion in 'C Programming' started by Bore Biko, Sep 20, 2005.

  1. Bore Biko

    Bore Biko Guest

    Dear,

    I have function that transform time from secconds
    to string formated as dd:hh:mm:ss
    (days:hours:minutes:seconds), but it doesent
    work correctly...
    Here s code compiled with gcc
    it goes (if you name program as "transform"),
    transform 1000, for 1000 seconds...

    #include<stdio.h>

    void transform(char*, int);

    int main(int argc, char* argv[]){
    int time;
    char formated_t[12];
    time=atoi(argv[1]);
    transform(&formated_t, time);
    printf("\n%s", formated_t);
    }

    void transform(char t[], int time){
    int days, hours, minuts, seconds, i, j, k;
    days=time/(24*3600);
    i=time%(24*3600);
    time=time/(24*3600)+i;
    hours=(time/3600);
    i=time%3600;
    time=time/3600+i;
    minuts=time/60;
    i=time%60;
    time=time/60+i;
    seconds= time % 60;
    sprintf(t,"%d:%d:%d:%d",days, hours, minuts, seconds);
    }


    Thanks in advance, Robert !!
    -com.hr
    Bore Biko, Sep 20, 2005
    #1
    1. Advertising

  2. "Bore Biko" <> wrote in message
    news:dgputc$laq$-com.hr...
    > I have function that transform time from secconds
    > to string formated as dd:hh:mm:ss
    > (days:hours:minutes:seconds), but it doesent
    > work correctly...


    Assuming we have equal understanding of what is correct... :)

    > Here s code compiled with gcc
    > it goes (if you name program as "transform"),
    > transform 1000, for 1000 seconds...
    >
    > #include<stdio.h>
    >
    > void transform(char*, int);
    >
    > int main(int argc, char* argv[]){
    > int time;
    > char formated_t[12];
    > time=atoi(argv[1]);
    > transform(&formated_t, time);


    ^^^^^^^^^^^ you must not use & here. Actually, if you invoke gcc with the
    switch -Wall (which you should always put or you're in trouble), then gcc
    will warn you. The idea is simple, pointer to array and pointer to array's
    first element are both pointers (equal pointers) but of somewhat different
    type. Don't ask me why -- it's by design of C...

    > printf("\n%s", formated_t);
    > }
    >
    > void transform(char t[], int time){


    ^^^ here t is effectively a pointer to a char (or first element of an array,
    but not the array itself)

    > int days, hours, minuts, seconds, i, j, k;
    > days=time/(24*3600);


    ok

    > i=time%(24*3600);


    ok

    > time=time/(24*3600)+i;


    and what do you want to say by the above? Whay are you summing up? Cats and
    cans? :)
    Don't you think something's unnecessary here, either cats or cans?

    I hope you'll fix this and any remaining probs yourself.

    Alex
    Alexei A. Frounze, Sep 20, 2005
    #2
    1. Advertising

  3. On Tue, 20 Sep 2005 23:22:43 +0200, in comp.lang.c , "Bore Biko"
    <> wrote:

    >Dear,
    >
    >I have function that transform time from secconds
    >to string formated as dd:hh:mm:ss
    >(days:hours:minutes:seconds), but it doesent
    >work correctly...


    Please define what you mean by incorrect - crashs, produces nonsense,
    is nearly right but sometimes gives off-by-one errors?

    Anyway, turn up warning levels on your compiler and try again....

    >#include<stdio.h>


    >void transform(char*, int);
    >
    >int main(int argc, char* argv[]){
    > int time;
    > char formated_t[12];
    > time=atoi(argv[1]);


    missing header for atoi()

    > transform(&formated_t, time);


    transform takes a char*, you're passing it a char**. Remove the &

    > printf("\n%s", formated_t);
    >}
    >
    >void transform(char t[], int time){
    > int days, hours, minuts, seconds, i, j, k;


    j & k are not used.

    The rest will work as you expect, more or less, for values less than
    one day. Over that, you need to be a bit cleverer.

    You may also want to read up on the formatting string for printf, so
    you can discover how to get two digits.
    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Mark McIntyre, Sep 20, 2005
    #3
  4. Bore Biko

    Eric Sosman Guest

    Bore Biko wrote On 09/20/05 17:22,:
    > Dear,


    Sshh! Not in front of the children!

    > I have function that transform time from secconds
    > to string formated as dd:hh:mm:ss
    > (days:hours:minutes:seconds), but it doesent
    > work correctly...


    "It doesn't work correctly" is no more informative
    than "It's wrong." You haven't described what's wrong,
    why you think it's wrong, or how it differs from what
    you expected. I'll try to guess, but if I'm attacking
    the wrong problem you have only yourself to blame.

    > Here s code compiled with gcc


    ... but with the warnings muted, or else gcc
    would have issued complaints. (In fact, it should
    have complained anyhow; are you sure you've shown
    us exactly the same code you've been compiling?)

    For nearly all code I'd recommend using
    "gcc -W -Wall". For highly portable code, go even
    further to "gcc -W -Wall -ansi -pedantic" (or you
    might replace "-ansi" with "-std=whatever").

    > it goes (if you name program as "transform"),
    > transform 1000, for 1000 seconds...
    >
    > #include<stdio.h>


    You forgot to #include <stdlib.h>.

    > void transform(char*, int);
    >
    > int main(int argc, char* argv[]){
    > int time;
    > char formated_t[12];
    > time=atoi(argv[1]);


    It might have been wise to make sure argv[1] is
    actually present before trying to convert it ... A
    quick look at the value of argc would tell the tale.

    Also, atoi() is not a very good way to convert
    strings to numbers. The problem is that it cannot be
    relied upon to do anything sensible if the input is
    something like "zaphod" instead of a numeric string.
    It also can't be relied upon if the input is something
    along the lines of "99999999999999999999999999999999".
    Functions like strtol() are much safer.

    > transform(&formated_t, time);


    Here's what the compiler should have complained
    about: the function's first argument is supposed to
    be a pointer to a `char', but you're passing a pointer
    to an array of `char' instead. If you don't understand
    the difference, see Question 6.12 in the comp.lang.c
    Frequently Asked Questions (FAQ) list

    http://www.eskimo.com/~scs/C-faq/top.html

    > printf("\n%s", formated_t);


    A subtle bug that might not bother you at the
    moment but that will (by Murphy's Law) bite you at
    some other, more important moment: The last character
    sent to a text output stream before it is closed
    must be a '\n', or the final line might never appear
    or might appear in some muddled way. Some systems will
    let you get away with this; others will not.

    You have (correctly) defined main() as returning
    an `int' value, but you haven't actually returned one.
    There's a special dispensation for this in the latest
    version of the C Standard, but more compilers hew to
    the older version than to the new one, so it's unwise
    to rely on it as yet.

    > }
    >
    > void transform(char t[], int time){
    > int days, hours, minuts, seconds, i, j, k;
    > days=time/(24*3600);


    Here's another subtle trap, less common nowadays
    than it used to be, but still occasionally stumbled
    upon like a forgotten land mine. `24' and `3600' are
    `int' constants, so their product will be computed as
    an `int'. However, the range of `int' varies from one
    compiler to the next, and its upper limit can be as low
    as 32767. Since the product 86400 is well above this
    limit, the computation could overflow. For perfect
    safety, you should carry it out in `long' instead.

    > i=time%(24*3600);


    All right: `i' is now the number of seconds left
    over once you've taken out the days.

    > time=time/(24*3600)+i;


    ... but what's this? You're adding the number of
    days -- not the number of seconds in those days, just
    the days themselves -- to the number of left-over seconds.
    You then pursue the same pattern in the rest of the code,
    but the pattern itself is nonsensical. Seventy-one seconds
    is one minute ten seconds, not one minute eleven. Work a
    few of these conversions with pencil and paper, study the
    steps you take, and reproduce those steps in code.

    > hours=(time/3600);
    > i=time%3600;
    > time=time/3600+i;
    > minuts=time/60;
    > i=time%60;
    > time=time/60+i;
    > seconds= time % 60;
    > sprintf(t,"%d:%d:%d:%d",days, hours, minuts, seconds);


    Two problems here. First, if the original number of
    seconds is large enough you'll generate more characters than
    will fit in the space allotted to `t'. 8726399 seconds would
    be 100:23:59:59, which (with the terminating '\0') would try
    to put thirteen characters in a twelve-character sack, with
    unpredictable consequences.

    Second, the plain "%d" conversion will not always generate
    the two digits you desire. You'll get things like 1:13:0:59:1
    instead of 01:13:00:59:01. To force two positions, use "%2d".
    To fill with leading zeroes instead of leading blanks, use
    "%02d".

    > }


    --
    Eric Sosman, Sep 20, 2005
    #4
  5. "Eric Sosman" <> wrote in message
    news:dgq31r$h9a$...
    >
    >
    > Bore Biko wrote On 09/20/05 17:22,:
    > > Dear,

    >
    > Sshh! Not in front of the children!
    >

    I don't know what to put a young lady who is English teacher
    told me that is usualy put: "Dear"

    > > I have function that transform time from secconds
    > > to string formated as dd:hh:mm:ss
    > > (days:hours:minutes:seconds), but it doesent
    > > work correctly...

    >

    here was a problem:
    void transform(char* t, int time){
    int days, hours, minuts, seconds, i, timi;
    char dd[5],hh[3],mi[3],sec[3];
    timi=time;
    days=time/(24*3600);
    i=time%(24*3600);
    time=time/(24*3600)+i;
    hours=(time/3600);
    i=time%3600;
    time=time/3600+i;
    minuts=time/60;
    time=time/60;
    time=time%60;
    seconds=timi-days*3600*24-hours*3600-minuts*60;

    now its all OK..!!!
    Robert Bralic, Sep 21, 2005
    #5
  6. Robert Bralic wrote:
    > > > I have function that transform time from secconds
    > > > to string formated as dd:hh:mm:ss
    > > > (days:hours:minutes:seconds), but it doesent
    > > > work correctly...

    > >

    > here was a problem:
    > void transform(char* t, int time){
    > int days, hours, minuts, seconds, i, timi;
    > char dd[5],hh[3],mi[3],sec[3];
    > timi=time;
    > days=time/(24*3600);
    > i=time%(24*3600);
    > time=time/(24*3600)+i;

    ^^^^^^^^^^^^^^^^^^^^^^
    This makes no sense

    > hours=(time/3600);
    > i=time%3600;
    > time=time/3600+i;

    ^^^^^^^^^^^^^^^^^
    Neither does this

    > minuts=time/60;
    > time=time/60;
    > time=time%60;
    > seconds=timi-days*3600*24-hours*3600-minuts*60;


    > now its all OK..!!!


    No it isn't. Your computations are still wrong. You may not have
    noticed, but I am pretty sure that the output will be incorrect for
    some input values.

    Take a paper and a pencil and make the computations manually. Then
    describe what you have just done in C. It's not that difficult.
    Antonio Contreras, Sep 21, 2005
    #6

  7. > > now its all OK..!!!

    >
    > No it isn't. Your computations are still wrong. You may not have
    > noticed, but I am pretty sure that the output will be incorrect for
    > some input values.
    >
    > Take a paper and a pencil and make the computations manually. Then
    > describe what you have just done in C. It's not that difficult.
    >

    Here is a simple program for generating random numbers,
    with implemented function...
    Sory becouse of all this smal strings and ifs I forgoted
    to use "%02d" format...
    //---program is compiled with gcc and works correct-------------------------
    //--just compile with "gcc prog.c -o prog.exe and then do prog
    number--------


    #include<stdio.h>
    #include<malloc.h>
    #include<time.h>

    void transform(char*, int);
    struct lst{
    long int value;
    struct lst* next;
    };

    int main(int argc, char* argv[]){
    long int i,j,k, time_begin, time_end, razlika_vremena;
    char trans_value[20];
    struct lst *prvi, *tekuci_prvi, *tekuci_drugi, *zadnji;
    prvi=(struct lst*)malloc(sizeof(struct lst));
    prvi->next=(struct lst*)malloc(sizeof(struct lst));
    prvi->value=1;
    prvi->next->value=2;
    tekuci_prvi=prvi->next;
    zadnji=prvi->next;
    zadnji->next=(struct lst*)NULL;
    k=atoi(argv[1]);
    tekuci_drugi=prvi;
    time(&time_begin);
    for(i=1;i<k;i++){
    exit:
    tekuci_drugi=prvi;
    while(tekuci_drugi->next){
    if((i%(tekuci_drugi->value)==0) && tekuci_drugi->value!=1){
    if(i==1){
    goto exit;
    }
    i++;
    goto exit;
    }
    tekuci_drugi=tekuci_drugi->next;
    }
    tekuci_prvi->next=(struct lst*)malloc(sizeof(struct lst));
    tekuci_prvi=tekuci_prvi->next;
    tekuci_prvi->value=i;
    tekuci_prvi->next=(struct lst*)NULL;
    /*printf("%ld\n",i);*/
    }
    tekuci_prvi=prvi;
    while(tekuci_prvi->next){
    printf("%d\n", tekuci_prvi->value);
    tekuci_prvi=tekuci_prvi->next;
    }
    time(&time_end);
    razlika_vremena=time_end-time_begin;
    transform(&trans_value, razlika_vremena);
    printf("\nGeneriranje i stampanje do %d broja je trajalo:%s", k,
    trans_value);
    exit(0);
    }




    void transform(char* t, int time){
    int days, hours, minuts, seconds, i, timi;
    char dd[5],hh[3],mi[3],sec[3];
    timi=time;
    days=time/(24*3600);
    i=time%(24*3600);
    time=time/(24*3600)+i;
    hours=(time/3600);
    i=time%3600;
    time=time/3600+i;
    minuts=time/60;
    time=time/60;
    time=time%60;
    seconds=timi-days*3600*24-hours*3600-minuts*60;
    if(days<10){
    sprintf(dd,"0%d",days);
    }else{
    sprintf(dd,"%d",days);
    }
    if(hours<10){
    sprintf(hh,"0%d",hours);
    }else{
    sprintf(hh,"%d",hours);
    }
    if(minuts<10){
    sprintf(mi,"0%d",minuts);
    }else{
    sprintf(mi,"%d",minuts);
    }
    if(seconds<10){
    sprintf(sec,"0%d",seconds);
    }else{
    sprintf(sec,"%d",seconds);
    }


    sprintf(t,"%s:%s:%s:%s",dd, hh, mi, sec);
    }
    Robert Bralic, Sep 21, 2005
    #7
  8. Robert Bralic wrote:
    > > > now its all OK..!!!

    > >
    > > No it isn't. Your computations are still wrong. You may not have
    > > noticed, but I am pretty sure that the output will be incorrect for
    > > some input values.
    > >
    > > Take a paper and a pencil and make the computations manually. Then
    > > describe what you have just done in C. It's not that difficult.
    > >

    > Here is a simple program for generating random numbers,
    > with implemented function...
    > Sory becouse of all this smal strings and ifs I forgoted
    > to use "%02d" format...


    <snip some code>

    So your point is what? You have not changed the computations, they're
    still wrong.
    Antonio Contreras, Sep 21, 2005
    #8
  9. Bore Biko wrote:
    >
    > Dear,
    >
    > I have function that transform time from secconds
    > to string formated as dd:hh:mm:ss
    > (days:hours:minutes:seconds), but it doesent
    > work correctly...


    You should probably give a better description of the problem than
    "doesn't work correctly". For example, given input X, I get output
    Y when it should have been Z" is better.

    > Here s code compiled with gcc

    [...]
    > void transform(char t[], int time){
    > int days, hours, minuts, seconds, i, j, k;
    > days=time/(24*3600);
    > i=time%(24*3600);
    > time=time/(24*3600)+i;
    > hours=(time/3600);
    > i=time%3600;
    > time=time/3600+i;
    > minuts=time/60;
    > i=time%60;
    > time=time/60+i;
    > seconds= time % 60;
    > sprintf(t,"%d:%d:%d:%d",days, hours, minuts, seconds);
    > }


    Others have pointed out problems with your code already. However, I
    would like to point out a "cleaner" way of doing this.

    Work "backwards".

    That is, calculate the seconds first:

    seconds = time % 60;
    time /= 60;

    Then minutes:

    minutes = time % 60;
    time /= 60;

    And so on.

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Sep 21, 2005
    #9
  10. Bore Biko

    Default User Guest

    Robert Bralic wrote:

    >
    > "Eric Sosman" <> wrote in message
    > news:dgq31r$h9a$...
    > >
    > >
    > > Bore Biko wrote On 09/20/05 17:22,:
    > > > Dear,

    > >
    > > Sshh! Not in front of the children!
    > >

    > I don't know what to put a young lady who is English teacher
    > told me that is usualy put: "Dear"



    That's known as a "salutation", although missing part, it should have
    said something like, "Dear group members". This should be included in
    formal letters. Traditionally usenet message do not have salutations.
    You can leave it out without offending the vast majority of readers.



    Brian
    Default User, Sep 21, 2005
    #10
  11. On Wed, 21 Sep 2005 09:22:31 +0200, in comp.lang.c , "Robert Bralic"
    <> wrote:

    >
    >"Eric Sosman" <> wrote in message
    >news:dgq31r$h9a$...
    >>
    >>
    >> Bore Biko wrote On 09/20/05 17:22,:
    >> > Dear,

    >>
    >> Sshh! Not in front of the children!
    >>

    >I don't know what to put a young lady who is English teacher
    >told me that is usualy put: "Dear"


    You need "Dear <persons name>. " Just "Dear" by itself is a term you
    typically only use when referring to your spouse or partner.....

    > days=time/(24*3600);


    it'll still overflow on a platform with 16-bit ints.
    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Mark McIntyre, Sep 21, 2005
    #11
  12. "Default User" <> wrote in message
    news:...
    > Robert Bralic wrote:
    >
    > >
    > > "Eric Sosman" <> wrote in message
    > > news:dgq31r$h9a$...
    > > >
    > > >
    > > > Bore Biko wrote On 09/20/05 17:22,:
    > > > > Dear,
    > > >
    > > > Sshh! Not in front of the children!
    > > >

    > > I don't know what to put a young lady who is English teacher
    > > told me that is usualy put: "Dear"

    >
    >
    > That's known as a "salutation", although missing part, it should have
    > said something like, "Dear group members". This should be included in
    > formal letters. Traditionally usenet message do not have salutations.
    > You can leave it out without offending the vast majority of readers.
    >
    >
    >
    > Brian


    Thanks for involving me in better comunication.
    Robert...!!
    Robert Bralic, Sep 22, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Drake
    Replies:
    2
    Views:
    962
    Steve Drake
    Jun 16, 2004
  2. Horace Nunley

    why why why does function not work

    Horace Nunley, Sep 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    448
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Sep 27, 2006
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    858
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,756
    Smokey Grindel
    Dec 2, 2006
  5. Milutin_Popovski

    Why this doesent work on bigger numbers..

    Milutin_Popovski, Jul 15, 2010, in forum: C Programming
    Replies:
    10
    Views:
    443
Loading...

Share This Page