Mail Archives: djgpp/1997/08/03/00:03:03
Dann Corbit wrote:
>
> The main difficulty with your program is your choice of data types. To
> accomplish the same algorithm, I would do something more like this:
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main()
> {
> double daysperyear,
> daystotal,
> secondsperday,
> years,
> totalseconds;
> char string[256];
>
> daysperyear = 365.25;
> secondsperday = 86400;
>
> printf("My second handmade program!\n\nTo tell you the number of
> seconds (estimate) you have lived.\n\n");
> printf("Enter your age in years: ");
> fgets(string, sizeof string, stdin);
> years = atof(string);
> daystotal = (daysperyear * years);
> printf("Total days = %g\n\n", daystotal);
>
> totalseconds = (daystotal * secondsperday);
> printf("The total number of seconds is: %.0f\n", totalseconds);
>
> return 0;
> }
> The thing I don't like about this program is that the number of significant
> digits is still very small.
> Perhaps, it would be better to collect the date parts separately. Here is
> another way to create your program:
> 1. Ask the year in which the person was born
> 2. Ask the month in which the person was born
> 3. Ask the day in which the person was born
> 4. Ask the hour in which the person was born {default 0 in case they do not
> know}
> 5. Ask the minute in which the person was born {default 0 in case they do
> not know}
> 6. Ask the second in which the person was born {default 0 in case they do
> not know}
This is pointless, I do not know of ANYPLACE where the second of birth
is recorded.
> 7. Ask which time zone they were born in. A list would be helpful.
> 8. Get the system time, adjusted for time zone
> 9. Find the difference in the dates
> 10. Calculate the life span in seconds.
> 11. Display result to the user.
> > Can't decide which variable to use with which scanf code.
> >
> > I want to be able to have more than 10 digits print out (if the age were
> > 10,000 years for example).
> > I am new to C (and programming in general) and teaching myself, so any
> > criticism will be appreciated.
> > #include <stdio.h>
> > int main()
> > {
> > int daysper, daystotal, secondsperday, years;
> > long long totalseconds;
> long long is not portable <yet -- yetch!>
> > daysper = 365;
> There are approximately 365.25 days per year. Over 40 years or so, it can
> make quite a difference.
It is a bit closer to 365.246 (Which is why we do NOT have a leap year
on years that are divisable by 400.
> > secondsperday = 86400;
> On some machines, this will be too large to store in an integer.
True, any machine that defines an int to be a 16 bit short int.
> > printf("My second handmade program!\n\nTo tell you the number of seconds
> > (estimate) you have lived.\n\n");
> > printf("Enter your age in years: ");
> > scanf("%d", &years);
> > daystotal=(daysper * years);
> > printf("Total days = %d\n\n", daystotal);
> > totalseconds=(daystotal * secondsperday);
> > printf("The total number of seconds is: %i\n",totalseconds);
> I suspect that %i is not the correct format specifier for long long on your
> machine. Even with the correct specifier, because the resolution is only
> years, there is a false impression in the number of significant digits.
> > return 0;
> > }
--
*********************************************************
* Alicia Carla Longstreet carla AT ici DOT net *
* Supporter of the campaign against grumpiness on c.l.c *
*********************************************************
It used to be:
Spare the rod and spoil the child.
Today it's:
Spare the rod to stay out of jail.
- Raw text -