A bit of help making an ordinal clock conversion

Viewed 43

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.

1 Answers

The declarations of findDate are inconsistent and the prototype should be placed before main.

Your prototype:

void findDate(int[], int *, int *); /*Prototype*/

Your call:

findDate(days, months, &dayTest);
          ^       ^
          |       Not an int*
          Not an array

Your definition:

void findDate(int days, int months, int* dayTest, int daysInMonths[])

Here you have 4 parameters but previously you only had 3. Also the individual parameter doesn't match previously definitions.

You need to fix this inconsistency.

Before main put:

void findDate(int days, int months, int* dayTest, int daysInMonths[]);

In main call it like:

findDate(days, months, &dayTest, daysInMonth);

And don't redefine variables like daysInMonth and dayTest in the function body. Simply do:

void findDate(int days, int months, int* dayTest, int daysInMonths[]) {
    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 >= 12 || months < 0) 
    {
        // Illegal call
        .. error handling ..
    }
    *dayTest = days + daysInMonth[months];
}

That said, why have the daysInMonths array as a variable that you fill every time you call the function? It's a waste of CPU cycles. Instead make it a global constant.

So before main do:

const int daysInMonth[] = {31, 28, 31, 30, ....};

then you don't need to pass the array at all and your function get much simpler:

void findDate(int days, int months, int* dayTest) {
    if (months >= 12 || months < 0) 
    {
        // Illegal call
        .. error handling ..
    }
    *dayTest = days + daysInMonth[months];
}

or simplify further by using a return value instead of a pointer:

int findDate(int days, int months) {
    assert(months >= 0 && months < 12); 
    return days + daysInMonth[months];
}

and call it like:

dayTest = findDate(days, months);

Much simpler... no pointers, no arrays to be passed to the function.

Final note... Array index starts from zero but your user will probably input 1 for january. So you have an inconsistency that must be handled.

Related