cs1010 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CS1010 PowerPoint Presentation
Download Presentation
CS1010

Loading in 2 Seconds...

play fullscreen
1 / 13

CS1010 - PowerPoint PPT Presentation


  • 107 Views
  • Uploaded on

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

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

PowerPoint Slideshow about 'CS1010' - bayard


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

CS1010

Week 6 Discussion

slide2
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);

slide3

/*

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;

}

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

slide5

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
Structure Review
  • Definition

structstruct_identifier

{

declarations

} ; // Note the semi-colon

structure review1
Structure Review
  • Declaration

struct Fraction fracA = {1,4};

struct Fraction fracA = {1};

struct Fraction fracA = {0};

struct Fraction fracA;

structure review2
Structure Review
  • Member Operator

fracA.num = 1;

fracA.den = 4;

structure review3
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;

}

slide11

MaxVertex(6,8)

MinVertex(1,2)

slide12

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