Why this function doesent work correctly...??

B

Bore Biko

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 !!
(e-mail address removed)-com.hr
 
A

Alexei A. Frounze

Bore Biko said:
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


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
 
M

Mark McIntyre

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.
 
E

Eric Sosman

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

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 said:
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".
 
R

Robert Bralic

Eric Sosman said:
Bore Biko wrote On 09/20/05 17:22,:

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"
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..!!!
 
A

Antonio Contreras

Robert said:
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.
 
R

Robert Bralic

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);
}
 
A

Antonio Contreras

Robert said:
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.
 
K

Kenneth Brody

Bore said:
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:[email protected]>
 
D

Default User

Robert said:
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
 
M

Mark McIntyre

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

days=time/(24*3600);

it'll still overflow on a platform with 16-bit ints.
 
R

Robert Bralic

Default User said:
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...!!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top