-
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
-
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.
-
i actually don't have the faintest idea where to even start, sorry. i'm not that great at pointers.
-
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.
-
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?
-
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."
-
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.
-
Sure :) Obviously you would need to do some modification to that code since its for Windows structures.
-
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;
}
}
-
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) % 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?