C++ program for day of the week (with code example)

Asked

Viewed 526 times

0

It is known that the day of the week of a date provided between 1° March 1700 and 28 February 2100 can be determined by the following method:

n=int(365.25∗g)+int(30.6∗f)−621049+d

ds=round(frac(n/7)∗7)+Δ+1

g={a−1, m≤2
  {a, m>2

f={m+13, m <= 2
  {m+1, m>2

Δ={2,n<36523
  {1,36523≤n<73048
  {0,n≥73048

The day of the week (ds) is represented by 1 if it is Sunday, 2 if it is Monday, and so on. Do a program that reads the day, month and year and provides the day of the corresponding week;

I know that I should use modf() for the frac(n/7) to obtain the fractional part. So far I found no logic, because the day of the week always ends up being 01.

#include <iostream>
#include <cmath>
#include <math.h>


using namespace std;

int main(){

    int dia;
    int mes;
    int ano;
    int ds;
    int g;
    int f;
    int delta;
    int n;
    float frac;
    float intpart;

    cin >> dia >> mes >> ano;

    if(mes > 2){
        g = ano;
        f = mes + 1;
    }else if(mes <= 2){
        g = ano - 1;
        f = mes + 13;
    }

    n = int(365.25 * g) + int(30.6 * f) - 621049 + dia;

    cout << n << endl;

    if(n < 36523){
        delta = 2;
    }else if( 36523 <= n and n < 73048){
        delta = 1;
    }else if(n >= 73048){
        delta = 0;
    }

    cout << delta << endl;
    frac = modf(n/7, &intpart);

    ds = round(frac * 7) + delta + 1;

    cout << ds << endl;

    switch (ds){
        case 1:
            cout << "domingo" << endl;
            break;
        case 2:
            cout << "segunda-feira" << endl;
            break;
        case 3:
            cout << "terca-feira" << endl;
            break;
        case 4:
            cout << "quarta-feira"  << endl;    
            break;
        case 5:
            cout << "quinta-feira" << endl;
            break;
        case 6:
            cout << "sexta-feira" << endl;
        case 7:
            cout << "sabado" << endl;
    }

    return 0;
}
  • 1

    Here: frac = modf(n/7, &intpart); do or frac = modf((float) n / 7, &intpart); or frac = modf(n / 7.0, &intpart); to force the operation to floating point and not an operation between integers.

  • thank you very much

2 answers

1


You always get into the case 1:, for its variable frac is returning 0 to the variable ds.

This is happening because you didn’t make one casting of int for float within the function modf.

Your corrected code will look like this:

frac = modf((float)n/7, &intpart);

Note that the recommended to operate float numbers is the modff function: modf, modff, modfl

But why his frac variable was returned 0 ?

Because in the division between integer numbers( your variable n is of type int) the part fractional is eliminated.

Hence its function modf always returned 0 to its variable ds, because there was no fractional part!

  • Man, thanks... I’m starting in C++... I didn’t know I should force the typing

  • I tried to edit the line and still returns 01, Sunday

  • Tried to change the initial values of day, month and year? Here returned correct

  • 1

    missed to force the 7.0

  • Good! Needing more help, open new questions!

  • 1

    The answer is wrong. In the expression float(n/7), the program first performs an integer Division of n/7 (which will probably give 0) and then builds a float with the value 0.

  • 1

    @v.Holy truth the correct would be (float)n/7. I will already correct

  • The correct one would be either (float) n / 7. Or the simpler one n/7.0. Which I think was the solution adopted by @Moose de Terno.

Show 3 more comments

1

To solve the problem one should use the casting to float, avoiding int in any case

frac = modf(float(n/7.0), &intpart);

both float() casting and force 7.0 as splitter

  • float() is not casting. float() functions as a constructor (although, strictly speaking, it is not a constructor). It only builds a float object with the number passed as parameter. To cast, we use (float) - emphasis, not float() - and this only for compatibility with the C language, because, strictly speaking, C++ has its own casting operators.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.