CS1010

1 / 13

# CS1010 - PowerPoint PPT Presentation

CS1010. Week 6 Discussion. Q5. (a) Write a function numDays to compute the number of days elapsed on a given date ( expressed in ddmmyyyy ) since 1 January 1900 . int numDays ( int ddmmyyyy );. /* Function numDays computes the number of days elapsed on ddmmyyyy since

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'CS1010' - bayard

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### CS1010

Week 6 Discussion

Q5

(a) Write a function numDays to compute the number of days elapsed on a given date (expressed in ddmmyyyy) since 1 January 1900.

intnumDays(intddmmyyyy);

/*

Function numDays computes the number of days elapsed on ddmmyyyy since

01 January 1900.

Parameter: ddmmyyyy -- date in day/month/year format

Returns: the number of days elapsed

*/

intnumDays(intddmmyyyy)

{

intday, month, year, i, days=0;

day= ddmmyyyy/1000000;

year= ddmmyyyy%10000;

month= (ddmmyyyy/10000)%100;

i = 1900;

while(i < year)

{

days= days + 365 + isLeapYear(i);

i = i + 1;

}

i = 1;

while(i < month)

{

days= days + daysInMonth(i,year);

i = i + 1;

}

days = days + day - 1;

return days;

}

Q5

(b) Rewrite the main function so as to print out the corresponding day given a date as input expressed in the form ddmmyyyy.

(c) Extend the functionality of the main function in question 5b such that the program repeatedly requests for input from the user until a 0 is read as input.

intdate;

printf("Enter date (ddmmyyyy; 0 to end): ");

scanf("%d", &date);

while (date > 0)

{

printf("Number of days elapsed: %d\n", numDays(date));

switch(numDays(date)%7)

{

case 0: printf("The date falls on a Monday.\n");

break;

case 1: printf("The date falls on a Tuesday.\n");

break;

case 2: printf("The date falls on a Wednesday.\n");

break;

case 3: printf("The date falls on a Thursday.\n");

break;

case 4: printf("The date falls on a Friday.\n");

break;

case 5: printf("The date falls on a Saturday.\n");

break;

case 6: printf("The date falls on a Sunday.\n");

}

printf("\nEnter date (ddmmyyyy; 0 to end): ");

scanf("%d", &date);

}

printf("Program terminated.\n");

Structure Review
• Definition

structstruct_identifier

{

declarations

} ; // Note the semi-colon

Structure Review
• Declaration

struct Fraction fracA = {1,4};

struct Fraction fracA = {1};

struct Fraction fracA = {0};

struct Fraction fracA;

Structure Review
• Member Operator

fracA.num = 1;

fracA.den = 4;

Structure Review
• Using Functions with structures

struct Fraction b)

{

structFraction sum;

sum.num = (a.num * b.den) + (a.den * b.num);

sum.den = a.den * b.den;

return sum;

}

MaxVertex(6,8)

MinVertex(1,2)

Step 1: Define a C structure struct Vertex with the coordinates, x and y, as members.

• Step 2: Define a C structure structRect with the two vertices as members.
• Step 3: Write a function

struct Rectangle newRectangle(struct Vertex v1, struct Vertex v2);

• Step 4:Write a boolean function overlap that takes in two rectangles as arguments and returns true if they overlap, false otherwise.

int overlap(structRect r1, structRect r2);