1 / 13

CS1010

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

Download Presentation

CS1010

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1010 Week 6 Discussion

  2. 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);

  3. /* 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; }

  4. 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.

  5. 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");

  6. Structure Review • Definition structstruct_identifier { declarations } ; // Note the semi-colon

  7. Structure Review • Declaration struct Fraction fracA = {1,4}; struct Fraction fracA = {1}; struct Fraction fracA = {0}; struct Fraction fracA;

  8. Structure Review • Member Operator fracA.num = 1; fracA.den = 4;

  9. Structure Review • Using Functions with structures struct Fraction add(struct Fraction a, struct Fraction b) { structFraction sum; sum.num = (a.num * b.den) + (a.den * b.num); sum.den = a.den * b.den; return sum; }

  10. Q1

  11. MaxVertex(6,8) MinVertex(1,2)

  12. 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);

  13. intnumTiles(int m, int n, structRect pillar);

More Related