Currently, I have an assignment to make a piece of code that asks for the user's input before converting it to an ordinal clock, as well as an exception in the case of a leap year. The only issue is that I don't know what I'm doing in the case of putting arrays in functions.
/*The purpose of this program is to make the user enter a date and return the info as ordinal*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#define MAXDAYSINMONTH 12 /*Define maximums for dates*/
#define MAXDAYS 365
int main()
{
void findDate(int[], int *, int *); /*Prototype*/
int daysInMonth[MAXDAYSINMONTH]; /*Variables for function*/
int days, months, year, dayTest; /*Variables for user*/
printf("This Program will ask you for the date\n");
printf("Please enter the Day: "); /*Get User input*/
scanf("%d", &days);
printf("\nPlease enter the Month: ");
scanf("%d", &months);
printf("\nPlease enter the Year: ");
scanf("%d", &year);
findDate(days, months, &dayTest);/*call the function*/
/**
* Input: Actual Date
* Process: Conversion
* Output: Ordinal Date
*/
printf("The ordinal date for the date entered is: ", dayTest);/*Get Output*/
if (year / 4 == 0) { /*Look for Leap Year*/
if (dayTest > 59) {
dayTest = dayTest + 1;
}
printf("This date is a leap year");
}
else
printf("This date is not a leap year");
return 0;
}
/*Convert Regular Date to Ordinal*/
void findDate(int days, int months, int* dayTest, int daysInMonths[]) {
int daysInMonth[MAXDAYSINMONTH], dayTest; /*Variables for function*/
daysInMonth[0] = 31;
daysInMonth[1] = 28;
daysInMonth[2] = 31;
daysInMonth[3] = 30;
daysInMonth[4] = 31;
daysInMonth[5] = 30;
daysInMonth[6] = 31;
daysInMonth[7] = 31;
daysInMonth[8] = 30;
daysInMonth[9] = 31;
daysInMonth[10] = 30;
daysInMonth[11] = 31;
if (months > 31) {
*dayTestaddr = days + daysInMonth;
}
}
New code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#define MAXDAYSINMONTH 12 /*Define maximums for dates*/
#define MAXDAYS 365
void findDate(int days, int months, int* dayTest, int daysInMonths[]); /*Prototype*/
int main()
{
int daysInMonth[MAXDAYSINMONTH]; /*Variables for function*/
int days, months, year, dayTest; /*Variables for user*/
printf("This Program will ask you for the date\n");
printf("Please enter the Day: "); /*Get User input*/
scanf("%d", &days);
printf("\nPlease enter the Month: ");
scanf("%d", &months);
months = months - 1;
printf("\nPlease enter the Year: ");
scanf("%d", &year);
findDate(days, months, &dayTest, daysInMonth); /*call the function*/
/**
* Input: Actual Date
* Process: Conversion
* Output: Ordinal Date
*/
printf("The ordinal date for the date entered is: %d%d", year, dayTest);/*Get Output*/
if (year % 4 == 0) { /*Look for Leap Year*/
if (dayTest > 59) {
dayTest = dayTest + 1;
}
printf("\nThis date is a leap year");
}
else
printf("\nThis date is not a leap year");
return 0;
}
/*Convert Regular Date to Ordinal*/
void findDate(int days, int months, int* dayTest, int daysInMonths[]) {
daysInMonths[0] = 31;
daysInMonths[1] = 28;
daysInMonths[2] = 31;
daysInMonths[3] = 30;
daysInMonths[4] = 31;
daysInMonths[5] = 30;
daysInMonths[6] = 31;
daysInMonths[7] = 31;
daysInMonths[8] = 30;
daysInMonths[9] = 31;
daysInMonths[10] = 30;
daysInMonths[11] = 31;
if (months > 11 || months < 0)
{
// Illegal call
printf("Your month is over 12 or under 0, try again\n");
}
if (months == 0) {
*dayTest = days;
}
else
*dayTest = days + (daysInMonths[months] - daysInMonths[1]);
}
I'm just saving this so my progress can be more apparent, I do think I have a general handle, but I still need to keep going forward in regards to making an ordinal date in the format of YYYY-DDD. Help would be appreciated, and thank you.