Thread: passing structure to function

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123

    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)
    {    
        int struct_function (struct my_struct link);
    
    
        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;
    }
    
    
    int struct_function (struct my_struct link)
    {
        // N = 1461 * f(year, month) / 4 + 153 * g(month) / 5 + 3;`
    
    
        int sum; 
    
    
        if (link.month <= 2)
        {
            link.year -= 1;
            link.month += 13;
        }
    
    
        else if (link.month > 2)
        {
            link.year = link.year;    
            link.month += 1;
        }
        
    
    
        return link.year;
    }

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    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. #3
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123
    Quote Originally Posted by Hodor View Post
    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. #4
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123
    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)
    {	
    	int struct_function (struct my_struct link);
    
    
    	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;
    }
    
    
    int struct_function (struct my_struct link)
    {
    	// N = 1461 * f(year, month) / 4 + 153 * g(month) / 5 + days;
    
    
    	int sum; 
    
    
    	if (link.month <= 2)
    	{
    		link.year -= 1;
    		link.month += 13;
    	}
    
    
    	else if (link.month > 2)
    	{
    		link.month += 1;
    	}
    	
    	int a, b;
    
    
    	a = (1461 * link.year) / 4;
    	b = (153 * link.month) / 5 + link.day;
    
    
    	sum = a + b;
    
    
    	return sum;
    }

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    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. #6
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    66
    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.
    Attached Images Attached Images

  7. #7
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123
    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. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by rusyoldguy View Post
    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; 
    }
    Last edited by Hodor; 10-09-2019 at 11:38 PM. Reason: Added reference

  9. #9
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123
    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 struct_function (struct my_struct link);
    	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;
    }
    	
    
    
    int struct_function (struct my_struct link)
    {
    	// N = 1461 * f(year, month) / 4 + 153 * g(month) / 5 + day;
    
    
    	int sum;
    
    
    	if (link.month < 3)
    	{
    		link.month += 12;
    		link.year -= 1;
    	}
    
    
    	sum = link.day + (153 * link.month - 457) / 5  + 365 * link.year + link.year / 4 - link.year / 100 + link.year / 400 - 306;
    
    
    	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. #10
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    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
    Last edited by Hodor; 10-26-2019 at 06:15 PM. Reason: clarification

  11. #11
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    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. #12
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123
    Thank you for time & effort Hodor.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing structure poiner to a function
    By aojsfalsie in forum C Programming
    Replies: 2
    Last Post: 08-25-2014, 07:20 PM
  2. passing structure to function
    By danieldcc in forum C Programming
    Replies: 4
    Last Post: 10-10-2011, 11:17 AM
  3. Passing Function to Structure
    By C-bob in forum C Programming
    Replies: 11
    Last Post: 07-22-2011, 07:36 PM
  4. Passing structure to function
    By Sereby in forum C Programming
    Replies: 4
    Last Post: 07-28-2004, 10:05 PM
  5. Passing a structure to a function?
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2001, 04:28 AM

Tags for this Thread