# Thread: passing structure to function

1. ## passing structure to function

Hey all, thanks for the help (again)

I'm just returning link.year to try and get a working number after the function is called but it won't work. Do I somehow need to add link.year inside the struct_function function to initialize link.year, it's returning as '0' when i run the program no matter what yyyy I insert.

I will be coding the forumla inside the struct_function function once i can see that all the structure variables date_one.month, date_one.day, date_one.year can be activated through the printf function after I run the program. Thanks for the help

This

Code:
```// EX_2.c  Chapter 8 Exercise #2
// compute two date
// August 8, 2014
// February 22, 2015

#include <stdio.h>

struct my_struct
{
int month;
int day;
int year;
};

int main(void)
{

struct my_struct date_one, date_two;

printf("Enter date #1: (mm dd yyyy): ");
scanf("%i/%i/%i", &date_one.month, &date_one.day, &date_one.year);

/*
printf("Enter date #2: (mm dd yyyy): ");
scanf("%i/%i/%i", &date_two.month, &date_two.day, &date_two.year);
*/

// return integer

printf("%i\n", struct_function(date_one));

//    printf("%i", struct_function(date_two));

return 0;
}

{
// N = 1461 * f(year, month) / 4 + 153 * g(month) / 5 + 3;`

int sum;

{
}

{
}

}```

2. You're probably entering the date as mm dd yyyy, but the scanf format ("%i/%i/%i") wants it as mm/dd/yyyy. You can check the return value of scanf() to see how many numbers it actually scanned.

3. Originally Posted by Hodor
You're probably entering the date as mm dd yyyy, but the scanf format ("%i/%i/%i") wants it as mm/dd/yyyy. You can check the return value of scanf() to see how many numbers it actually scanned.

that worked thanks I fixed the scanf function call

4. Here's my finished code right here i think I found a typo in the book there is more than one so far I've found. The end of the formula "+ day" makes the calculation not add up correctly for the dates specified in the book, for august 8 2004 and February 22 2005 (this is date_one.day in struct_function function). Thanks!

Code:
```// EX_2.c  Chapter 8 Exercise #2
// compute two date
// August 8, 2014
// February 22, 2015

#include <stdio.h>

struct my_struct
{
int month;
int day;
int year;
};

int main(void)
{

struct my_struct date_one, date_two;

printf("Enter date #1: (mm dd yyyy): ");
scanf("%i %i %i", &date_one.month, &date_one.day, &date_one.year);

printf("%i\n", struct_function(date_one));

printf("Enter date #2: (mm dd yyyy): ");
scanf("%i %i %i", &date_two.month, &date_two.day, &date_two.year);

printf("%i\n", struct_function(date_two));

printf("RESULT: \n");

int aa, bb, result;

aa = struct_function(date_one);
bb = struct_function(date_two);

result = aa - bb;

printf("%i days\n", result);

return 0;
}

{
// N = 1461 * f(year, month) / 4 + 153 * g(month) / 5 + days;

int sum;

{
}

{
}

int a, b;

a = (1461 * link.year) / 4;

sum = a + b;

return sum;
}```

5. Is it possible that in the computation of "b" you want to divide the first term by 5 + link.day, not just by 5?

By the way you really should be using more descriptive variable and function names.

6. I think he want's to calculate the day of the week given by a date, or the Date of Easter Sunday given by a year. May be according the formula of Zeller's congruence, Butcher or Gauss. But it looks like Zeller's congruence.
Is this correct _jamie?

I hope you can find a PDF-file in the Attachment with several formulas.
The file is written in german. But i still hope, it can help you.

7. my code works, i just switched the

Code:
`result = bb - aa;`
line around because I was getting a negative number. For example, inserting two dates, i.e. 7 27 2016 and 7 28 2016 results in the program executing and displaying "1 days".

rusyoldguy, i didn't understand the Zeller's congruence formula after a quick view, the formula is in the code

Code:
`// N = 1461 ...`
The programming book just names the formula by "formula" and says write a program using the formula that calculates the days between dates using a date structure returning the value of N computed.

Thanks!

8. Originally Posted by rusyoldguy
But it looks like Zeller's congruence.
Is this correct _jamie?
It's a formula to calculate a variation of a Rata Die number but I wouldn't trust it because I'm not sure it's accounting for leap days (1461 is the number of days in 4 years one of which might (is usually) a leap year, but the 100 and 400 year rules don't seem to be accounted for).

Rata Die is basically a Julian Day Number (JDN) (Julian day - Wikipedia)

Clearly if you convert two dates to a Rata Die number (or JDN) you can get the number of days between two dates simply by subtraction.

Edit:
I'd trust this more (at least it gives the correct Rata Die number for 10/10/2019)
Code:
```int n(int year, int month, int day)
{
/* Reference: https://www.researchgate.net/publication/316558298_Date_Algorithms#pf2c */

if (month <= 2) {
month += 12;
year -= 1;
}

N = day + (153 * month - 457) / 5 + 365 * year + year / 4 - year / 100 + year / 400 - 306;

return N;
}```

9. Hodor:

Code:
```// EX_4.c

#include <stdio.h>

struct day
{
int numbers;
char letter[15];
};

struct my_struct
{
int month;
int day;
int year;
};

int main(void)
{
int aa;

struct my_struct date_one;

// array of structures

const struct day days[7] = { { 6, { 'S', 'u', 'n', 'd', 'a', 'y' } },
{ 5, { 'M', 'o', 'n', 'd', 'a', 'y' } },
{ 7, { 'T', 'u', 'e', 's', 'd', 'a', 'y' } },
{ 9, { 'W', 'e', 'd', 'n', 'e', 's', 'd', 'a', 'y' } },
{ 8, { 'T', 'h', 'u', 'r', 's', 'd', 'a', 'y' } },
{ 6, { 'F', 'r', 'i', 'd', 'a', 'y' } },
{ 8, { 'S', 'a', 't', 'u', 'r', 'd', 'a', 'y' } } };

printf("Enter a date: (mm dd yyyy): ");
scanf("%i %i %i", &date_one.month, &date_one.day, &date_one.year); // get date_one (first date)

aa = struct_function(date_one);

printf("%i\n", aa);

int x, i;

x = (aa - 621049) % 7;

for (i = 0; i <= days[x].numbers; ++i)
printf("%c", days[x].letter[i]);

printf("\n");

return 0;
}

{
// N = 1461 * f(year, month) / 4 + 153 * g(month) / 5 + day;

int sum;

{
}

return sum;
}```

Code:
```\$ ./EX_4
Enter a date: (mm dd yyyy): 2 20 2008
733092
Monday```

The output for February of 2008 doesn't show the 20th day is a Monday so i'm thinking your formula is wrong, or what is wrong here? Thanks!

10. Well, first, it's not my formula. As a comment in the code I referenced the paper by Peter Baum.

But where is
Code:
`x = (aa - 621049) % 7;`
Coming from? If it's from the same book as your original formula, then of course it's wrong because the book's formula for calculating the Rata Die number is wrong (and calculates the number of days between two dates incorrectly except in limited circumstances).

The aim of your x = formula is to convert a Julian Day Number (JDN) to a day of the week number. Wikipedia says that to convert Rata Die to a JDN you need to add 1721425 (Julian day - Wikipedia). Using the formula from the same Wikipedia page to convert a JDN to day of week (first converting Rata Die number which is what you're using, to JDN), then, your code should be:

Code:
`x = (aa + 1721425 + 1) % 7;`
Baum's formula is not incorrect. Your book is incorrect and will not work for the number of days between, say 1/1/1998 and 1/1/2101

11. It's probably to also good to note that the formulas in the book give the incorrect day of the week number as well for some years. For example, using the book's formulas it says that 1 March 2100 is a Tuesday, which is incorrect. My implementation using Baum's formula's correctly gives the day as a Monday. (this is also why the book is broken for date ranges, for the other exercise, that include centuries that are not evenly divisible by 400; e.g. the book is treating the year 2100 as a leap year when it's not, but 2000 is)

12. Thank you for time & effort Hodor.