delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/07/29/23:33:29

From: "Dann Corbit" <dcorbit AT solutionsiq DOT com>
Newsgroups: comp.lang.c,comp.os.msdos.djgpp
Subject: Re: having trouble with long numbers
Date: 29 Jul 1997 19:48:21 GMT
Organization: PSINet
Lines: 105
Message-ID: <01bc9c58$5796ffa0$b361e426@DCorbit.solutionsiq.com>
References: <01bc9c51$0ceeec80$78ed1fcc AT darkstar>
NNTP-Posting-Host: 38.228.97.179
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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}
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.
-- 
Anonymous ftp sites for C-FAQ:
ftp://ftp.eskimo.com ftp://rtfm.mit.edu ftp://ftp.uu.net
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ Book: ISBN 0-201-84519-9.
Looking for something?  Software?  Algorithms?  Publications?
http://www.altavista.digital.com or http://www.infoseek.com

Michèle C. Dupré <forbinky AT ix DOT netcom DOT com> wrote in article
<01bc9c51$0ceeec80$78ed1fcc AT darkstar>...
> (using DJGPP on a Win95 Box)
> 
> 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.
> 
> Also can someone direct me to the FAQ(so that I can RTFM)?
> Thanx.
> Below is my code.
> 
> Cut------------------------------Cut
> #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.

> secondsperday = 86400;
On some machines, this will be too large to store in an integer.

> 
> 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;
> }
> End------------------------------End
> 

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019