EddyBeor
EddyBeor

Reputation: 11

C programming: Using structures and functions to determine the number of days between two dates

I have made the corrections suggested by Craig Estey. The code now compiles and work. This is the revised code:

/* Programme to determine the number of days between two dates ex8.2.c
This is done with the formula
N = ( ((1461 * (f(year, month))) / 4) + ((153 * (g(month))) / 5) + day )

with:
f(year, month) = year - 1 if month <= 2; otherwise year

g(month) = month + 13 if month <=2; otherwise month + 1

The formula is applicable for dates after 1 March 1900;
    add 1 to N for dates between 1 March 1800 to 28 February 1900
    add 2 to N for dates between 1 March 1700 to 28 February 1800

ALGORITHMS
N.B.: Use ternary operators to help with different evaluations

Declare structure(s) to store date
Write functions to evaluate f
Write function to evaluate g
Compare date periods to know if modification of formula will be used

 */

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

struct date
{
    int day;
    int month;
    int year;
};

struct date date1, date2;
int month, year, duration1, duration2, diff;

// Function prototypes
int number_of_Days (struct date d);
bool is_Leap_Year (struct date d);
int year_Func (int month, int year);
int month_Func (int month);
int date_Elapsed (struct date d);

int main(void)
{
    printf("This is a programme to find the number of days between two dates!\n");

    printf("\nEnter first date (dd mm yyyy): ");
    scanf(" %i%i%i", &date1.day, &date1.month, &date1.year);
        month = date1.month;
        year = date1.year;

    duration1 = date_Elapsed (date1);

    printf("\nEnter second date (dd mm yyyy): ");
    scanf(" %i%i%i", &date2.day, &date2.month, &date2.year);
        month = date2.month;
        year = date2.year;

    duration2 = date_Elapsed (date2);

    diff = duration2 - duration1;

    printf("Number of elapsed days are: %i.\n", diff);
}

// Function to find the number of days in a month
int number_of_Days (struct date d)
{
    int days;
    bool is_Leap_Year (struct date d);
    const int days_Per_Month[13] = {0, 31, 28, 31, 30, 31, 30,
                                        31, 31, 30, 31, 30, 31};

    if (is_Leap_Year (d) == true && d.month == 2)
    {
        days = 29;
    }
    else
    {
        days = days_Per_Month[d.month];
    }
    return days;
}

// Function to determine if it is a leap year
bool is_Leap_Year (struct date d)
{
    bool leap_Year_Flag;

    if ((d.year % 4 == 0 && d.year % 100 != 0) || (d.year % 400 == 0))
    {
        leap_Year_Flag = true;
    }
    else
    {
        leap_Year_Flag = false;
    }
    return leap_Year_Flag;
}

// Function to find f in formula
int year_Func (int month, int year)
{
    int yrRet;
    yrRet = (month <= 2) ? (year - 1) : (year);
    return yrRet;
}

// Function to find g in formula
int month_Func (int month)
{
    int mntRet;
    mntRet = (month <= 2) ? (month + 13) : (month + 1);
    return mntRet;
}

// Function to calculate N in formula
int date_Elapsed (struct date d)
{
    int number_of_Days (struct date d);
    bool is_Leap_Year (struct date d);
    int year_Func (int month, int year);
    int month_Func (int month);

    int yCalc, mCalc, nCalc;

    yCalc = year_Func (d.month, d.year);
    mCalc = month_Func (d.month);

    // Calculates number of elapsed days
    nCalc = ( ((1461 * (yCalc) / 4) + ((153 * (mCalc))) / 5) + d.day );

    if ((d.day < 1) && (d.month < 3) && (d.year < 1700))
    {
        printf("Invalid date input!\n");
        printf("Date must be from 1 March 1700.\n");
        exit (999);
    }
    else if (((d.day >= 1) && (d.month >= 3) && (d.year >= 1700)) && ((d.day <= 28) && (d.month <= 2) && (d.year <= 1800)))
    {
        nCalc = nCalc + 2;
    }
    else if (((d.day >= 1) && (d.month >= 3) && (d.year >= 1800)) && ((d.day <= 28) && (d.month <= 2) && (d.year <= 1900)))
    {
        nCalc = nCalc + 1;
    }
    else
    {
        return nCalc;
    }
}

================================================================================

I am a newbie learning computer programming. This is an exercise from Programming in C by Stephen G. Kochan. I need help in pinpointing the errors so far. I have been on it for about a week.

My Code::Blocks compiler is showing the following error:

||=== Build file: "no target" in "no project" (compiler: unknown) ===|
|In function ‘N’:|
|134|error: lvalue required as left operand of assignment|
|144|error: lvalue required as left operand of assignment|
|148|error: lvalue required as left operand of assignment|
|152|warning: return makes integer from pointer without a cast [-Wint-conversion]|
|154|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build failed: 3 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|

This is my code

/* Programme to determine the number of days between two dates ex8.2.c
This is done with the formula
N = ( ((1461 * (f(year, month))) / 4) + ((153 * (g(month))) / 5) + day )

with:
f(year, month) = year - 1 if month <= 2; otherwise year

g(month) = month + 13 if month <=2; otherwise month + 1

The formula is applicable for dates after 1 March 1900;
    add 1 to N for dates between 1 March 1800 to 28 February 1900
    add 2 to N for dates between 1 March 1700 to 28 February 1800

ALGORITHMS
N.B.: Use ternary operators to help with different evaluations

Declare structure(s) to store date
Write functions to evaluate f
Write function to evaluate g
Compare date periods to know if modification of formula will be used

*/

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

struct date
{
    int day;
    int month;
    int year;
};

struct date date1, date2;
int month, year, N1, N2, diff;

// Function prototypes
int number_of_Days (struct date d);
bool is_Leap_Year (struct date d);
int F (int month, int year);
int G (int month);
int N (struct date d);

int main(void)
{
    //
    printf("This is a programme to find the number of days between two dates!\n");

    printf("\nEnter first date (dd mm yyyy): ");
    scanf(" %i%i%i", &date1.day, &date1.month, &date1.year);
        month = date1.month;
        year = date1.year;

    N1 = N (date1);

    printf("\nEnter second date (dd mm yyyy): ");
    scanf(" %i%i%i", &date2.day, &date2.month, &date2.year);
        month = date2.month;
        year = date2.year;

    N2 = N (date2);

    diff = N2 - N1;

    printf("Number of elapsed days are: %i.\n", diff);
}

// Function to find the number of days in a month
int number_of_Days (struct date d)
{
    int days;
    bool is_Leap_Year (struct date d);
    const int days_Per_Month[13] = {0, 31, 28, 31, 30, 31, 30,
                                        31, 31, 30, 31, 30, 31};

    if (is_Leap_Year (d) == true && d.month == 2)
    {
        days = 29;
    }
    else
    {
        days = days_Per_Month[d.month];
    }
    return days;
}

// Function to determine if it is a leap year
bool is_Leap_Year (struct date d)
{
    bool leap_Year_Flag;

    if ((d.year % 4 == 0 && d.year % 100 != 0) || (d.year % 400 == 0))
    {
        leap_Year_Flag = true;
    }
    else
    {
        leap_Year_Flag = false;
    }
    return leap_Year_Flag;
}

// Function to find f
int F (int month, int year)
{
    int F;
    F = (month <= 2) ? (year - 1) : (year);
    return F;
}

// Function to find g
int G (int month)
{
    int G;
    G = (month <= 2) ? (month + 13) : (month + 1);
    return G;
}

// Function to calculate N
int N (struct date d)
{
    int number_of_Days (struct date d);
    bool is_Leap_Year (struct date d);
    int F (int month, int year);
    int G (int month);

    int f, g;

    f = F (d.month, d.year);
    g = G (d.month);

    //N = ( ((1461 * (f(d.month, d.year))) / 4) + ((153 * (g(d.month))) / 5) + d.day );
    N = ( ((1461 * (f) / 4) + ((153 * (g))) / 5) + d.day );

    if ((d.day < 1) && (d.month < 3) && (d.year < 1700))
    {
        printf("Invalid date input!\n");
        printf("Date must be from 1 March 1700.\n");
        exit (999);
    }
    else if (((d.day >= 1) && (d.month >= 3) && (d.year >= 1700)) && ((d.day <= 28) && (d.month <= 2) && (d.year <= 1800)))
    {
        N = N + 2;
    }
    else if (((d.day >= 1) && (d.month >= 3) && (d.year >= 1800)) && ((d.day <= 28) && (d.month <= 2) && (d.year <= 1900)))
    {
        N = N + 1;
    }
    else
    {
        return N;
    }
}

Upvotes: 1

Views: 1363

Answers (1)

Craig Estey
Craig Estey

Reputation: 33631

The offending line is:

N = (((1461 * (f) / 4) + ((153 * (g))) / 5) + d.day);

But, this is inside the function N.

You can't assign to a function name.

Either change the function name (e.g. N --> Ncalc) or use a different name for the variable that holds the return value (e.g. ret).

Assigning to N where N is the containing function is a thing in fortran [IIRC] for setting the return value of the function. That won't work in c too well.

And, you'd have to provide definition of the variable (e.g.):

int ret;

Some style items ...

By convention, in c, using all uppercase is usually reserved for constants (e.g.):

#define X 12345

Although it's perfectly legal to pass a struct by value, most code passes a pointer to the struct (possibly with const) because it's faster.

int
N(struct date *d)
{
}

Imagine what would happen if your struct was:

struct date {
    ...
    int array[10000];
};

It would push about 40,000 bytes onto the stack.

Also, it's helpful to use [longer] more descriptive names than single character ones. You've got functions to "calculate" f, g, and n named F, G, and N.

Well, what is [an] f [or g or n]??? Anybody [else] reading your code will want to know.

Use more descriptive comments that the equivalent of:

// set x to the value of 10
x = 10;

Upvotes: 1

Related Questions