Earliest date

F

Felipe Ribeiro

I have to write a program that asks the user to enter a number of
dates and then determines which one is the earliest.
I solved the problem but I think there might be a better way to do it.
Here's the code I wrote:
-------------------------------------------------------------------------------------------------------------
#include <stdio.h>

int main(void)
{
int day, month, year, date;
/*
* Initially earliest needs to be greater than any date, so
initialize
* it with a large value
*/
int earliest = 10000000;

for (;;) {
printf("Enter a date (mm/dd/yy): ");
scanf("%d / %d / %d", &month, &day, &year);

if (month == 0 && day == 0 && year == 0)
break;

/* Put the date into a number, so it's easier to operate on it */
date = year * 10000 + month * 100 + day;

if (date < earliest)
earliest = date;
}
printf("The earliest date is %.2d/%.2d/%.2d\n",
(earliest - ((earliest / 10000) * 10000)) / 100, earliest % 100,
earliest / 10000);

return 0;
}
-------------------------------------------------------------------------------------------------------------
I'd like to know if I could make any changes to improve the program.
Make it clearer or more elegant... I don't know. :)

For example, if I wrote

printf("Enter a date (mm/dd/yy): ");
scanf("%d / %d / %d", &month, &day, &year);
while (month == 0 && day == 0 && year == 0) {
printf("Enter a date (mm/dd/yy): ");
scanf("%d / %d / %d", &month, &day, &year);
....
code
...
}

would it be better than using an infinite loop? Would it improve
anything on performance?
 
F

Felipe Ribeiro

Felipe Ribeiro said:



How about INT_MAX (which is in <limits.h>)?

I think I should have told that I'm just starting with programming. :)
I'm following K.N. King's book and this problem is in it. The code I
wrote is everything I know so far. :)
Have you tested against inputs such as "April 20, 2009"? How about
1000 1000 1000 ? How would you deal with these?

I don't really know how to deal with string yet. :)
If the date is not known to be a valid date, this is insufficient.
If the date /is/ known to be valid, this only needs to be:

date = year * 366 + month * 32 + day;

By using
date = year * 10000 + month * 100 + day;
I have a legal date in the format yymmdd which I later break in parts
to have the date in the format mm/dd/yy. How would I do this with date
= year * 366 + month * 32 + day; ?
How would they finish? There has to be some way for them to let you
know they've stopped entering dates. I had assumed that 0 0 0 was
that way. Now it seems you consider it to be not a sentinel but an
error.

The problem says that the program will finish when the user enters
0/0/0.
The condition should be while (month != 0 || day != 0 || year != 0),
by the way. I know this condition is just ridiculous but I'm just
following the guidelines given by the problem.
 
C

CBFalconer

Felipe said:
I have to write a program that asks the user to enter a number of
dates and then determines which one is the earliest. I solved the
problem but I think there might be a better way to do it.

Simply use ISO standard date format. Make the user enter dates in
that format. Then you can simply sort the strings resulting.

The format is: YYYY-MM-DD

Note the '-' separators, 4 digit year, etc. MM cannot be 00. Nor
can DD.
 
K

Keith Thompson

CBFalconer said:
Simply use ISO standard date format. Make the user enter dates in
that format. Then you can simply sort the strings resulting.

The format is: YYYY-MM-DD

Note the '-' separators, 4 digit year, etc. MM cannot be 00. Nor
can DD.

That's a great idea -- unless you have a requirement to use a
different date format.
 
C

CBFalconer

Keith said:
That's a great idea -- unless you have a requirement to use a
different date format.

He spelled out the formula (different) in his input prompt.
 
J

john

I have to write a program that asks the user to enter a number of
dates and then determines which one is the earliest.
I solved the problem but I think there might be a better way to do it.
Here's the code I wrote:
-------------------------------------------------------------------------------------------------------------
#include <stdio.h>

int main(void)
{
        int day, month, year, date;
        /*
         * Initially earliest needs to be greater than any date, so
initialize
         * it with a large value
         */
        int earliest = 10000000;

        for (;;) {
                printf("Enter a date (mm/dd/yy): ");
                scanf("%d / %d / %d", &month, &day, &year);

                if (month == 0 && day == 0 && year == 0)
                        break;

                /* Put the date into a number, so it's easier to operate on it */
                date = year * 10000 + month * 100 + day;

                if (date < earliest)
                        earliest = date;
        }
        printf("The earliest date is %.2d/%.2d/%.2d\n",
                        (earliest - ((earliest / 10000) * 10000)) / 100, earliest % 100,
                                earliest / 10000);

        return 0;}

-------------------------------------------------------------------------------------------------------------
I'd like to know if I could make any changes to improve the program.
Make it clearer or more elegant... I don't know. :)

For example, if I wrote

printf("Enter a date (mm/dd/yy): ");
scanf("%d / %d / %d", &month, &day, &year);
while (month == 0 && day == 0 && year == 0) {
        printf("Enter a date (mm/dd/yy): ");
        scanf("%d / %d / %d", &month, &day, &year);
...
code
 ...

}

would it be better than using an infinite loop? Would it improve
anything on performance?

Look, for now, worry about clarity and correctness, as these guys
generally emphasize. Also, you need to realize that scanf is not
usually a part of production code, because it gives the
users too much opportunity for entering crazy data. But you must
consider the edge cases: what's your code going to do when you ask
the user to input a small positive integer, and he or she types
"-3.1415926"? What if the entry is "kneedeep?" or 50000000000000? In
your case, 2/31/1918? Would your code cite February thirty-first,
1918, as the earliest date?

And C is pretty flexible; you can use strings with a dash separator in
one part of your code, then print it out with a slash separator. But
don't worry about performance for a long time. Correctness first.
Clarity second.

John
 
B

BartC

Richard Heathfield said:
CBFalconer said:


He could do that, but then he wouldn't be answering the problem he
was set. He would instead be answering a different problem. That's
fine, except that it still leaves the original problem unanswered.

The problem was to "enter a number of dates", it didn't mention format.
 
K

Keith Thompson

BartC said:
The problem was to "enter a number of dates", it didn't mention format.

We have the OP's summary of the requirements; we don't have the
requirements themselves.
 
B

BartC

The problem requires that the date be entered using the format mm/dd/
yy.

OK. In that case you might be able to just rearrange (as text) into
yyyymmdd. Maybe even compare using strcmp().

But that also introduces an ambiguity when using yy instead of yyyy which I
don't think was addressed in the posts I saw.

For example, is year 97 earlier or later than 07? You have to determine
whether the user intended 19xx or 20xx.
 
K

Kenny McCormack

Absolutely!

You know he wasn't being amusing or funny when he wrote that?[/QUOTE]

Actually, McIntyre/Ambuhl is/are amusing and funny in whatever they write.

It may not be intended as such, but such is the effect. Their posts never
fail to amuse me.
Such is his own self regard. Dwarfed only by Chuck's huge ego which
stalks the corridors of c.l.c hoping to trap a fawning Noob and make
them shake with fear. I had an Electronics teacher like that. Myself
and another student made a pact never to pay any attention to what he
said in class and did our own notes from reputed textbooks. We both got
As. Chuck kind of reminds me him - all bullshit, self regard and
failing memories of past glories and epic achievements, myopic and
totally unable to read the public's reactions to their bumbling
incompetence.

Good story. So apt.
 
C

CBFalconer

Richard said:
CBFalconer said:

He could do that, but then he wouldn't be answering the problem he
was set. He would instead be answering a different problem. That's
fine, except that it still leaves the original problem unanswered.

As usual, you didn't bother to read the original, but just went off
beating your gums about nothing. If you bother to look you will
see that Mr. Ribeiro put his own input date format into his code.
He did not try to convert something that he had been supplied. So
he can easily use ISO format, ease his problem, learn something,
etc.

Again, I suggest less silly meandering, and do try to grow up.
 
K

Keith Thompson

CBFalconer said:
Richard said:
CBFalconer said:

He could do that, but then he wouldn't be answering the problem he
was set. He would instead be answering a different problem. That's
fine, except that it still leaves the original problem unanswered.
[...]
Mr. Ribeiro put his own input date format into his code. He did not
try to convert something that he had been supplied. So he can
easily use ISO format, ease his problem, learn something, etc.

http://groups.google.com/group/comp.lang.c/msg/8d66a5bec1fb2c6a
Message-ID: <c149c6af-c40b-4e8f-8b48-890e5caa97d4@s16g2000vbp.googlegroups.com>

| The problem requires that the date be entered using the format mm/dd/
| yy.

If you were to suggest converting the mm/dd/yy input to yyyy-mm-dd
internally, that might be a sensible approach -- though IMHO it's more
work than is necessary. If it's only used internally, you might as
well use just yyyymmdd -- and in that case, you might as well just use
an integer (as long as it's sufficiently large) rather than a string.
 
J

James Dow Allen

Simply use ISO standard date format.  Make the user enter dates in
that format.  Then you can simply sort the strings resulting.

The format is:  YYYY-MM-DD

Great minds think alike! This is the format I used
in my binary-seconds to date-string converter 40 years ago.
Actually used YYYY/MM/DD HH:MM:SS

Didn't need to write the cumbersome inverse function!
Just did binary search, calling the to-string function
and strcmp(). (Uh... actually used CLC, not strcmp().
C didn't exist then.)

James
 
J

James Kuyper

James said:
Great minds think alike! This is the format I used
in my binary-seconds to date-string converter 40 years ago.
Actually used YYYY/MM/DD HH:MM:SS

Great minds might think alike, but apparently not identically. ISO 8601
mandates a 'T' rather than a space character between the date and the
time fields. Also, it mandates '-' rather than '/' as delimiters for the
month portion.

The reason it does this is that ISO 8601 reserves '/' for use in time
intervals. For instance, 2009-03-15T15/04-27T12 identifies the time
interval from 2009-03-15T15:00:00 to 2009-04-27T12:00:00.
 
R

Richard Bos

BartC said:
The problem was to "enter a number of dates", it didn't mention format.

All the same, he probably has to use the braindead USAlien date format
for reasons of teacher (and population) idiocy.

Richard
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top