Topic: C programming: classwork.

Content:  classwork (only in class)

 

Classwork assignment (only in class)

Gentle Reminder!

You can get points only if you send us your solutions of [only-in-class] classwork assignments:

You must submit your [only-in-class] solution before the end of a lesson! The solutions, sent after that, will not be graded.

 

You can use any arbitrary materials, if desired, but conversations with each other are prohibited.

If conversation (incl. communication by chats, e-mail etc.) occurs the participants will get “0” (ZERO) points for this assignment.

If you are a member of plagiarists’ team, your solution can be graded as “0” (ZERO).

 

Name your solution file and topic of an e-mail letter as follows:

CW<a consecutive number of a lesson>_<firstname>_<lastname>_<studentID>group<group number><_><Calendar>

for example: CW12_Jane_Doe_123456MVEB21_Calendar.c

 

You must send your solutions to both of us:           

Dr Marina Brik             marina.brik@ati.ttu.ee  

Dr Sergei Kostin      sergei.kostin@gmail.com

 

 


Task  Calendar (3 points):

 

Write a program in C, using a piece of code and UML diagrams shown at the end of the file.

 

Task:

 

Find the difference in days between two dates that are entered by the user.

The date is specified by three integers (data entered from the screen):

 

Þ    d - day number,

Þ    m - month number,

Þ    y - year number.

 

Get the difference in days DateDiff, subtracting from the date2 (d2, m2, y2) the date1(d1, m1, y1).

Algorithm:

 

1.      If y1=y2, m1=m2, d2>=d1, then

DateDiff = d2-d1

2.      If y1=y2, m2>m1, then

DateDiff =,

where days(m) - number of days in given month m

3.      If y2>y1, then

DateDiff =,

 

 

 A piece of code:       

 

#include <stdio.h>

 

// function prototypes

int y366(int year);

int MonthSum(int m1,int m2, int y);

int DateDiff_function(int d1, int m1, int y1, int d2, int m2, int y2);

 

int main (void)

{

      int y1, m1, d1;

      int y2, m2, d2;

 

      printf("Input date1 (dd.mm.yyyy): ");

      scanf("%d.%d.%d", &d1, &m1, &y1);

 

      printf("Input date2 (dd.mm.yyyy): ");

      scanf("%d.%d.%d", &d2, &m2, &y2);

 

 

      printf("Between date2 %d.%d.%d and date1 %d.%d.%d", d2, m2, y2, d1, m1,y1);

      printf(" is %d day(s)\n", DateDiff_function(d1, m1, y1, d2, m2, y2));

   

      return 0;

}

Task:

1.      write a function with following prototype int y366(int year); which takes as an argument current year and using the sign of a leap-year returns 1 if given year is the leap-year; and returns 0 if the year is not the leap-year.

 

            The sign of a leap-year:

 

            A year is a leap year in two cases:

·         either it is a multiple of 4, but is not a multiple of 100;

·         or it is a multiple of 400.

 

            ((y mod 4 ) = 0 ) and (((y mod 100) != 0) or ((y mod 400) = 0)))

 

2.      write function using Function MonthSum() UML diagram shown below with following prototype int MonthSum(int m1,int m2, int y); which takes as arguments months of date1 and date2, i.e. m1 and m2, and given year, and returns number of days between month m2 and m1, considering also extra day if given year is a leap-year and there is February between m2 and m1.  

 

3.      write function using Function DateDiff_function() UML diagram (Variant A or B) shown below with following prototype int DateDiff_function(int d1, int m1, int y1, int d2, int m2, int y2); which takes as arguments two dates and returns number of days between given dates.

 

 

 

Used materials:

 

Created: Marina Brik 08.12.2014

Updated: Sergei Kostin 12.12.2017


UML diagrams:

 

 

NB!  Conditions on the branches of "rhomb" have to be mutually exclusive!

 

Diagrams for functions DateDiff_function (A or B: the variant of diagram choose by yourself) and MonthSum:

 

 

 

Function MonthSum() UML diagram

 

NB! Note that the parallel processes are used here for the sake of illustration. In C code is assumed that they are sequential processes.


 

 

 

 

 

A)    Function DateDiff_function() UML diagram (Variant A)

 


B)    Function DateDiff_function() UML diagram (Variant B)