{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture102 Functions that retrun values

# Lecture102 Functions that retrun values - Functions That...

This preview shows pages 1–5. Sign up to view the full content.

Lecture 1 -- 1 Computer Science I - Martin Hardwick Functions That Return Values // Compute the day of the week // for a given date using  // Reverend Zeller's Algorithm #include <iostream> #include <iomanip> #include <string> using namespace std; We use functions to “divide and conquer” a large problem. each function implements a  piece of the larger problem  solution Sometimes the solution to a piece of the problem must be made available to solve other pieces. e.g., the function computes a  value needed by other  functions in the program Functions must be able to return computed values before they terminate. the sqrt function does this Problem:  What day of the  week does a given  date fall on? We will use functions that return values to compute this.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Lecture 1 -- 2 Computer Science I - Martin Hardwick call call Algorithm Basic algorithm: get the date from the user use Reverend Zeller’s algorithm to compute the day of  the week (represented as an integer between 0 and 6) convert the day of the week to a day name (i.e.,  Sunday, …, Saturday) display the day of the week for the given date Program structure: function to convert numeric day of week to the name of the day function to implement Reverend Zeller’s algorithm main function to get date from user, provide control flow, and display final answer And Program Structure
Lecture 1 -- 3 Computer Science I - Martin Hardwick Function To Compute The Week Day (1) int dayOfWeek(int month, int day,             int year, int century) { int EffectOfMonth;    // month days  int LeapYears;          // leap year days int EffectOfCentury;  // century days int TotalDays;          // total day count int WeekDay;           // answer //Step 1: Adjust year so 0 becomes 100 if (year == 0) { year = 100; century = century - 1; } //Step 2: Adjust month so March == 1 year = 100; century = century - 1; } else if (month < 3) { year = year - 1; } This function uses an algorithm attributed to Reverend Zeller for computing the day of the week for a given date. Input: month, day, year and century for the given date all integers Output: day of the week represented as a number between 0 and 6 0 = Sunday The algorithm begins a new year on March 1 ! input must be adjusted

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Lecture 1 -- 4 Computer Science I - Martin Hardwick Function To Compute The Week Day (2) //Step 3: Count the days month = (month + 9) % 12 + 1; EffectOfMonth = (13*month - 1)/5; LeapYears = year / 4; EffectOfCentury = century / 4; TotalDays = EffectOfMonth + LeapYears +  EffectOfCentury +  day + year -  2*century; //Step 4: Compute and return  //day of week WeekDay = TotalDays % 7;
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 18

Lecture102 Functions that retrun values - Functions That...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online