Using pointers(?)

This is a discussion on Using pointers(?) within the C Programming forums, part of the General Programming Boards category; I've got this program that's suppose to calculate Julian date into the actual date. ex. 1 = January 1st, 32 ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    21

    Using pointers(?)

    I've got this program that's suppose to calculate Julian date into the actual date. ex. 1 = January 1st, 32 = February 1st, 33 = February 2nd, 365 = December 31st and so on....

    I'm stuck on what equation I should use and how to use pointers in this program...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* computes month and day for Julian date */
    void convert_jdate(int julian_date, int *month, int *day) {
    
    	/* YOUR CODE GOES HERE */
    
    }
    
    int main(void) {
    
    	int jdate;
    	int month;
    	int day;
    	char* month_name[12] = {
    		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
    		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    	};
    
    	printf("enter Julian date (a number between 1 and 365)\n");
    	if (scanf("%d", &jdate) != 1) {
    		printf("not a number\n");
    		return EXIT_FAILURE;
    	}
    	else if ((jdate < 1) || (jdate > 365)) {
    		printf("not between 1 and 365\n");
    		return EXIT_FAILURE;
    	}
    	else {
    		/* YOUR CODE GOES HERE (to call convert_jdate) */
    
    		printf("Julian date %d is %s %d\n", jdate, month_name[month-1], day);
    
    		return EXIT_SUCCESS;
    	}
    
    }
    Thanks for the input

  2. #2
    Banned
    Join Date
    Dec 2008
    Posts
    49
    I sooooooo have nifty code to do this for you. But it would be wrong of me to do this for you. So I will make a compromise. You try, and I will give you my code.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    21
    i actually don't have the faintest idea where to even start, sorry. i'm not that great at pointers.

  4. #4
    Banned
    Join Date
    Dec 2008
    Posts
    49
    One way of doing this would be to keep track of what months fall on what days of the year. Just be sure to account for leap years.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    21
    so while loops would help isolate which julian dates fall under the correct month? how would i then calculate the julian date to the actual date?

  6. #6
    Banned
    Join Date
    Dec 2008
    Posts
    49
    Yeah that is a good way to do it. There is actually an algorithm that is oh so cool for doing this exact thing, but methinks your professor would know it isn't of your design. I find it to be a very nifty little algorithm since it is one of those things that "just works."

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    21
    could i see that algorithm to calculate it? i don't plan on copying the program, i just would like to see the thought process that goes into converting julian date to gregorian date. being in a principles class there's no way i could actually pass off anything i've learned online into what i've done in class, haha.

  8. #8
    Banned
    Join Date
    Dec 2008
    Posts
    49
    Sure Obviously you would need to do some modification to that code since its for Windows structures.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    21
    I'm obviously not too bright at this computer science thing so this is really what I can muster at the moment. Would this potentially work if I stick with this process?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* computes month and day for Julian date */
    void convert_jdate(int julian_date, int *month, int *day) {
    
               if (julian_date <= 31)
               {
                     *month = Jan;
                     *day = julian_date;
               }
      
               if (julian_date > 31 && julian_date <=59)
               {
                     *month = Feb;
                     *day = julian_date - 31;
                }
    }
    
    int main(void) {
    
    	int jdate;
    	int month;
    	int day;
    	char* month_name[12] = {
    		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
    		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    	};
    
    	printf("enter Julian date (a number between 1 and 365)\n");
    	if (scanf("%d", &jdate) != 1) {
    		printf("not a number\n");
    		return EXIT_FAILURE;
    	}
    	else if ((jdate < 1) || (jdate > 365)) {
    		printf("not between 1 and 365\n");
    		return EXIT_FAILURE;
    	}
    	else {
    		/* YOUR CODE GOES HERE (to call convert_jdate) */
    
    		printf("Julian date %d is %s %d\n", jdate, month_name[month-1], day);
    
    		return EXIT_SUCCESS;
    	}
    
    }

  10. #10
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    Wut if u do dis

    Code:
    /* Make an array wit da munts in dem */
    static const int munts[02][014] =
    {
      {0x1F,0x1C,0x1F,0x1E,0x1F,0x1E,0x1F,0x1F,0x1E,0x1F,0x1E,0x1F},
      {0x1F,0x1D,0x1F,0x1E,0x1F,0x1E,0x1F,0x1F,0x1E,0x1F,0x1E,0x1F}
    };
    
    #define LEAP_YR(year)  (!((year) &#37; 4) && ((year) % 100 != 0 || (year) % 400 == 0))?1:0
    
    void make_str(int jdate, int year)
    {
      int i, count;
      int day = -1;
      int yr = LEAP_YR(year);
      char *month_name ="Jan\0Feb\0Mar\0Apr\0May\0Jun\0Jul\0Aug\0Sep\0Oct\0Nov\oDec";
      char *mptr = 0;
    
      for(count = 0, i = 0; i < 014; ++i)
        if((count += munts[yr][i]) > jdate)
        {
           mptr = month_name + (--i << 2);
           count -= munts[i];
           day = jdate - count;
           printf("Julian date %d is %s %d\n", jdate, mptr, day);
           break;
        }
    }
    Happy?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21