Hi There,

I need assistance with the assert function. The question is convert a date to Julian date and then check which one is the smallest and subtract to show the different days. That is all done. The part I have no idea is how to use the assert function to verify that the dates entered are legitimate dates.

Is there someone that can assist me please.

//Converting date to Julian format

#include <iostream>
#include <assert.h>

using namespace std;

//definition for the number of days in
//each month
int daysPerMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int calcJulian(int dd, int mm)
{
    int t = 0;
    int numDays;
    
    for (int i = 1; i < mm; i++)
    {
        switch(i)
        {
                 case 1: numDays = daysPerMonth[0];
                 case 3: numDays = daysPerMonth[2];
                 case 5: numDays = daysPerMonth[4];
                 case 7: numDays = daysPerMonth[6];
                 case 8: numDays = daysPerMonth[7];
                 case 10: numDays = daysPerMonth[9];
                 case 12: numDays = daysPerMonth[11];
                      t += 31;
                      break;
                 
                 case 4: numDays = daysPerMonth[3];
                 case 6: numDays = daysPerMonth[5];
                 case 9: numDays = daysPerMonth[8];
                 case 11: numDays = daysPerMonth[10];
                      t += 30;
                      break;
                     
                 case 2: numDays = daysPerMonth[1];
                      t += 28;
                      break;                                             
        }
    }
    t += dd;
    return t;
}

int main()
{
    int dd, mm;
       
    cout << "Enter day (dd)" << endl;
    cin >> dd;
    cout << "Enter month (mm)" << endl;
    cin >> mm;
    
    int f = calcJulian(dd, mm);
    
    int dd2, mm2;
    cout << "Enter day (dd)" << endl;
    cin >> dd2;
    cout << "Enter month (mm)" << endl;
    cin >> mm2;
    
    int g = calcJulian(dd2, mm2);
    cout << "Julian day: " << f << endl;
    cout << "Julian day: " << g << endl;
    
    //check which is the smaller date and then subtract 
    //to get the different between the days
    float ans;    
    if ( f < g ) {
        ans = (g-f);
    }
    else {
        ans =(f-g);
    }        

    cout << "Numbers of days different between the two days is: " << ans << endl;
    
    system("PAUSE");
    return 0;
}

Have you seen the reference on assert function? It says that it needs an expression, and if the value of the expression is '0' , it terminates the program. Now since what you have is just 2 ints that represent the day and the month, it is upto you to decide how to evaluate the correctness. What is your criteria for a legitimate date?

Well the Question is:

Write a program that converts a date (dd mm) to julian format. The julian format is the day of the year the ‘dd mm’
date represents, e.g. the julian format of ’27 03’ (i.e. the 27th of March) is 31 + 28 + 27 , i.e. 31 days in January plus
28 days in February plus 27, which gives a julian date of 86. Use the following definition for the number of days in
each month:

int daysPerMonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31};

You can ignore leap years. The program must ask for 2 dates – read the day and month of each date separately. It
must then convert both dates to their julian format, display them, and then display the number of days difference
between the two dates. The program must check which is the smaller date before subtracting – the user may enter the
dates in any order. Verify that the dates entered are legitimate dates, using the assert function and the above
defined array, e.g. 31 6 is not a valid date.

My Coding with the Assert:

//Converting date to Julian format

#include <iostream>
#include <assert.h>
#include <cassert>

using namespace std;

//definition for the number of days in
//each month
int daysPerMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int calcJulian(int dd, int mm)
{
    int t = 0;
    int numDays;
    
    for (int i = 1; i < mm; i++)
    {
        switch(i)
        {
                 case 1: numDays = daysPerMonth[0];
                 case 3: numDays = daysPerMonth[2];
                 case 5: numDays = daysPerMonth[4];
                 case 7: numDays = daysPerMonth[6];
                 case 8: numDays = daysPerMonth[7];
                 case 10: numDays = daysPerMonth[9];
                 case 12: numDays = daysPerMonth[11];
                      t += 31;
                      assert(t == 31);
                      break;
                 
                 case 4: numDays = daysPerMonth[3];
                 case 6: numDays = daysPerMonth[5];
                 case 9: numDays = daysPerMonth[8];
                 case 11: numDays = daysPerMonth[10];
                      t += 30;
                      assert(t == 30);
                      break;
                     
                 case 2: numDays = daysPerMonth[1];
                      t += 28;
                      assert(t == 28);
                      break;                                             
        }
    }
    t += dd;
    return t;
}

int main()
{
    int dd, mm;
       
    cout << "Enter day (dd)" << endl;
    cin >> dd;
    cout << "Enter month (mm)" << endl;
    cin >> mm;
    
    int f = calcJulian(dd, mm);
    
    int dd2, mm2;
    cout << "Enter the 2nd day(dd)" << endl;
    cin >> dd2;
    cout << "Enter month the 2nd month (mm)" << endl;
    cin >> mm2;
    
    int g = calcJulian(dd2, mm2);
    cout << "Julian day: " << f << endl;
    cout << "Julian day: " << g << endl;
    
    //check which is the smaller date and then subtract 
    //to get the different between the days
    float ans;    
    if ( f < g ) {
        ans = (g-f);
    }
    else {
        ans =(f-g);
    }        

    cout << "Numbers of days different between the two days is: " << ans << endl;
    
    system("PAUSE");
    return 0;
}

.

The asserts on lines 30 and 38 will not work because the value of t is the julian date so it will equal the specified value in the assert statement only on the very first iteration of the loop.

you could do something like this on line 17 of the code you posted, and then don't use assert at all in the remainder of the code:

assert(mon > 0 && mon < 13 && day > 0 && day < daysPerMonth[mon-1]);

lines 74-80. Its not necessary to actually perform that check. Just subtract the two numbers and get their absolute value diff = abs(f - g);

Thank you very much, so that mean I need to remove line 30, 38, 43.

Just tested and I give error message if I enter the day's more than 31 and month more that 12.

Thank you very much, so that mean I need to remove line 30, 38, 43.

Yes

Just tested and I give error message if I enter the day's more than 31 and month more that 12.

Good :) I see a small bug in the assert statement I posted. The last test should use <= operator, not just < so that the last day of the month will be considered value. day <= daysPerMonth[mon-1])

Cool thank you seems like it is running smoothly :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.