Skip to main content
Raspberry Pi

Mondays Child : Learning to program in C with the Rasberry Pi

By December 1, 2013September 12th, 2022No Comments

In this lesson we now create a fully functioning program written in C on the £30 Raspberry Pi. Basing the program on the traditional Poem “Monday’s Child” we can match a given birthdate to the days in the Poem. Monday’s child is an apt name for this on the Pi, the children have so much opportunity and they just need to apply themselves. Not all children will be programmers but we can help them aspire to be the most they ever can. In this lesson we will use a program with two methods. The main method, as normal, and additional method that is used to work out the day of week from a given date. The main method then involves itself with printing the correct line from the poem for the given day and the looping structure that allows more than one date to be entered. The code contains for loops, if statement and switch statements.

The finished product

It is easiest to explain our project by starting at the finished product. This way you can gain the idea of what should happen. The program runs and the user is asked to add the year, month and day of their or another person’s birthday. The result from the program then may look like this:

You can see that once the answer is supplied the user is prompted if they want to add another date or not. If no is chosen, as in this example the program exits.

GPL License

This code, written by me, is licensed under the Free Software Foundation GPL V2 License. Other may freely use the code and share their development. If you have ever used the internet for ideas on how to code something, to me it would immoral, not to help others. If you own the code consider sharing it under the GPL license or other license supported or endorsed by the Free Software Foundation. My code is all distributed under the GPL v2 License.

The source code for Mondayschild

/*
Simple program to display which day someone was born on relating to poem
Copyright (C) 2013 theurbanpenguin (Peterborough) Ltd Andrew Mallett
andrew.mallett@theurbanpenguin.com
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int getWday(int year, int month, int day)
{
    //Implement Zeller's rule to find day of week for any given date
    //Zeller starts March as month 1 hence adjustments
    int adjustment, mm, yy;
    adjustment = (14 - month) / 12;
    mm = month + 12 * adjustment - 2;
    yy = year - adjustment;
    return (day + (13 * mm - 1) / 5 +
        yy + yy / 4 - yy / 100 + yy / 400) % 7;
}

main () {
  const char *MMONDAY = "Monday's child is fair of facen";
  const char *MTUESDAY = "Tuesday's child is full of gracen";
  const char *MWEDNESDAY = "Wednesday's child is full of woen";
  const char *MTHURSDAY = "Thursday's child has far to gon";
  const char *MFRIDAY = "Friday's child is loving and givingn";
  const char *MSATURDAY = "Saturday's child works hard for a livingn";
  const char *MSUNDAY = "The child born on the Sabbath day is bonny and blithe and good and gayn";
  int y;
  int m;
  int d;
for (;;) {
  printf("Enter the year YYYY: ");
  scanf("%d",&y);
  getchar();
  printf("Enter the month MM: ");
  scanf("%d",&m);
  getchar();
  printf("Enter the day DD: ");
  scanf("%d",&d);
  getchar();
  int dow = getWday(y,m,d);
  system("clear");
  switch(dow) {
        case 1 :
            printf(MMONDAY);
            break;
        case 2 :
            printf(MTUESDAY);
            break;
        case 3 :
            printf(MWEDNESDAY);
            break;
        case 4 :
            printf(MTHURSDAY);
            break;
        case 5 :
            printf(MFRIDAY);
            break;
        case 6 :
            printf(MSATURDAY);
            break;
        case 0 :
            printf(MSUNDAY);
            break;
        default :
            printf("Date not correct/n");    
    }
    char answer;
    printf("Do you have another date to check, y or n : ");
    scanf("%s",&answer);
    getchar();
    answer = tolower(answer);
    if ( answer == 'n' ) {
        printf("Exiting at user requestn");
        exit(0);
    }
 }
}

getWday Function

We can see that we start with a function other than main. We still have main and the order they are defined in does not matter. The code though starts with this function and so shall we.

int getWday(int year, int month, int day){
 //Implement Zeller's rule to find day of week for any given date
 //Zeller starts March as month 1 hence adjustments
 int adjustment, mm, yy;
 adjustment = (14 - month) / 12;
 mm = month + 12 * adjustment - 2;
 yy = year - adjustment;
 return (day + (13 * mm - 1) / 5 +
 yy + yy / 4 - yy / 100 + yy / 400) % 7;
}

This implements Zeller’s rule which is a purely mathematical formula to derive a day number from a give date. We take 3 integers as input the represent the year, month and day and return a single integer which will have a value from 0-6. 0 being Sunday and Saturday 6. This being the workhorse of the application is well placed in its own function.

Function main

With the main function, or method, we prompt for the users birth date, storing the year, month and day in variables to pass through to the getWday function. getWday will retun a day number and we can use the day number in a switch statement. this is similar to an if  statement but allows for many branches in the decision process.

switch(dow) {
        case 1 :
            printf(MMONDAY);
            break;
        case 2 :
            printf(MTUESDAY);
            break;
        case 3 :
            printf(MWEDNESDAY);
            break;
        case 4 :
            printf(MTHURSDAY);
            break;
        case 5 :
            printf(MFRIDAY);
            break;
        case 6 :
            printf(MSATURDAY);
            break;
        case 0 :
            printf(MSUNDAY);
            break;
        default :
            printf("Date not correct/n");    
    }

Here we read the dow variable that was populated from the output of the function. It will have a value from 0 – 6 based on Zeller’s rule. We have defined constants that have messages for each day of the week from the poem, (MMONDAY, MTUESDAY etc) so it is a simple matter of matching each number to the correct day. The use of the break statement it to ensure that processing stops once a match has been found. If we have matched on 1 being Monday we do not need to test anymore and break will exit the code block for the switch statement.

Reading in the date values we have looked at before with scanf; the same applies to the for loop. So new to this main function is the use of constants, same as variables but do not change during the execution and the switch statement.

Watch the video tutorial on YouTube: