- /* Add days to a date structure and obtain new date */
-
-
#include<cstdio>
-
#include<cstdlib>
-
-
struct date
-
{
-
int day;
-
int month;
-
int year;
-
};
-
-
const int mdays[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
-
-
void addDays(struct date *, const int);
-
int isleapyear(const int);
-
-
int main(void)
-
{
-
struct date dt = {31, 1, 2000};
-
-
for(int i = 1000; i < 5000; i += 1000)
-
{
-
addDays(&dt, i);
-
printf("%d-%d-%d\n", dt.day, dt.month, dt.year);
-
}
-
-
return 0;
-
}
-
-
void addDays(struct date *dt, const int days)
-
{
-
(*dt).day += days;
-
-
while((*dt).day > mdays[(*dt).month])
-
{
-
if(isleapyear((*dt).year) && (*dt).month == 2)
-
{
-
(*dt).day = (*dt).day - mdays[(*dt).month] - 1;
-
-
/* only for displaying 29 instead of 0 */
-
if(!(*dt).day)
-
(*dt).day = 29;
-
}
-
else
-
(*dt).day = (*dt).day - mdays[(*dt).month];
-
-
(*dt).month += 1;
-
-
if((*dt).month > 12)
-
{
-
(*dt).year += 1;
-
(*dt).month %= 12;
-
}
-
}
-
}
-
-
int isleapyear(const int year)
-
{
-
if((!(year % 4) && !(year % 10)) || !(year % 400))
-
return 1;
-
else
-
return 0;
-
}
-
-
in the following code i am trying to add days to the structure date and trying to change the values of the structure to a new date. Problem is for every 1000 days added to the actual date, the error in the value of day is as follows
-
-
//ouput
-
-
1000 days added - 0 days error in the modified structure
-
2000 days added - 2 days extra in the modified structure
-
3000 days added - 4 days extra in the modified structure
-
4000 days added - 5 days extra in the modified structure
-
-
//end of output
-
i checked by coding in C# and the above extra days are relative to the values obtained from this C# program(ofcourse i used built in library functions). I know i am doing something wrong somewhere, i wonder if its in the concept of days calculated for a year 365.25xxx or is it logic problem.
4 1210
There's a typo in your isleapyear function; make that % 100 instead of % 10.
kind regards,
Jos
thank you. what a silly mistake
but still i am having the same output
@tsatyadev
Your isleapyear function is still incorrect: centuries are never leapyears unless the year can be divided by 400.
kind regards,
Jos
thank you for your time
i understand what went wrong
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Market Mutant |
last post by:
www.51.ca is a nice site, and I want to build something similar and wonder
if anyone knows if they are using PHPNuke or other nukes or they wrote
everything themselves. What is the signature thing...
|
by: typingcat |
last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so
on. I've tried many PHP IDEs today, but almost non of them supported
Unicode (UTF-8) file.
I've found that the only Unicode...
|
by: atv |
last post by:
Whatis the proper way to handle errors from function calls?
For example, i normally have a main function, with calls to mine
or c functions. Should i check for errors in the functions called...
|
by: Jason Huang |
last post by:
Hi,
Would someone explain the following coding more detail for me? What's the
( ) for?
CurrentText = (TextBox)e.Item.Cells.Controls;
Thanks.
Jason
|
by: William |
last post by:
VB6 had the ability to pause your execution while debugging, messing with
the code, and continue executing right from where you left off. Is DotNet
ever going to have this capability? I really...
|
by: Xah Lee |
last post by:
in March, i posted a essay “What is Expressiveness in a Computer
Language”, archived at:
http://xahlee.org/perl-python/what_is_expresiveness.html
I was informed then that there is a academic...
|
by: Howard Kaikow |
last post by:
I'm using the code below, but an error is not getting trapped.
Where can such errors occur?
In another process?
Try
SomeCode
Catch ex As Exception
Dim strMsg() As String = Split(ex.ToString,...
|
by: clemrock |
last post by:
Help w/ errors.add_to_base between controller and model
Hello,
I'm having trouble in routing some errors between model and
controller. The errors produced in the controller...
|
by: Tarik Monem |
last post by:
OK! I've gone through a few tutorials and I cannot understand what I'm doing wrong
casting_registration.php
<table>
<tr>
<td>
<form enctype="multipart/form-data" action="thankyou.php"...
|
by: adam.timberlake |
last post by:
I've just finished reading the article below which goes into some
depth about exceptions. The article was rather lucid and so I
understand how to implement it all, the thing I'm having trouble with...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |