Date Lab final assignment PDF

Title Date Lab final assignment
Author Keyur Maru
Course Intro to Programming in C
Institution Palomar College
Pages 6
File Size 67.7 KB
File Type PDF
Total Downloads 3
Total Views 168

Summary

What is today's date and its significance...


Description

Date Lab Write a C program that implements the Date Lab from Chapter 10 of Stegman (p. 245), but with the following modifications. The output should go to standard output, not “csis.txt”. Arrays are not introduced until chapter 11, so please do not use arrays for this lab, even though arrays could be a nice way to handle some issues in the lab. Instead, one can use a switch statement or if statements. A key piece of advice is to use the functions developed in the Date Prep Lab. Most of the logic is handled by calling functions we already have from the Date Prep Lab. Only a small amount of additional complexity needs to be added. Use one function to determine whether a given year is a leap year, and then use that function as needed. Do not replicate the leap year logic anywhere outside that function. The function should be a Boolean predicate function, i.e., it returns data type int, and the returned value is always 0 or 1, indicating false or true, respectively. The constants appearing in the program should match the basic facts involved in the problem domain. For example, the number of days in March is 31, so 31 can appear in the program if it is clear that 31 is there as the number of days in March. However, a number such as 59 should not appear if that 59 is representing the number of days in February (28, for a non-leap year) plus the number of days in March (31). The 28 and the 31 are constants representing “basic facts”, but 59 is something calculated from basic facts and is not itself a “basic fact”. One piece of additional logic that is required is to put the two dates in order for selection 3, which is computing the number of days between two calendar dates. If the first date is earlier than the second date, leave them unmodified. However, if the first date is later than the second date, swap the two dates. One hint concerns the easiest way to compute the number of days between two calendar dates. There are many ways to do, but many of them get complicated. Here is one idea that helps keep things simple. Suppose we want to know the number of days between September 6, 1949 and March 27, 1983. One way that is obvious but a bit more complicated to figure out how many days are covered, always moving forward in four steps: (1) September 6, 1949; (2) January 1, 1950; (3) January 1, 1983; and (4) March 27, 1983. How many days from (1) to (2)? The existing functions do not directly answer that question. Instead, step 2 should be to January 1, 1949. So, we have the steps: (1) September 6, 1949; (2) January 1, 1949; (3) January 1, 1983; and (4) March 27, 1983. The total number of days is: the number of days between January 1, 1949 and January 1, 1983, plus the number of days from January 1, 1983 to March 27, 1983, minus the number of days from January 1, 1949 to September 6, 1949. Each of these quantities a computed by functions from the Date Prep Lab, so the logic is very simple: just call the right Date Prep Lab functions.

Place your C program here, in place of the example program below: #define _CRT_SECURE_NO_WARNINGS #include #include int displayMenu(int* option); void getJulianDate(int* jyear, int* year); void getCalendarDate(int* cmonth, int* cday, int* cyear); int isLeapYear(int year); int toJulian(int month, int day, int year, int* option); void toCalendar(int jyear, int year, int option); void swap(int*, int*); int daysInYear(int year); void daysBetweenYears(); int main(void) { int jyear, year; int option, month, day; displayMenu(&option); do { if (option == 1) { getCalendarDate(&month, &day, &year); toJulian(month, day, year, &option); } else if (option == 2) { getJulianDate(&jyear, &year); toCalendar(jyear, year, option); } else if (option == 3) { daysBetweenYears(); } else { exit(1); } } while (displayMenu(&option) && 1); getchar(); return 0; } int displayMenu(int* option) { printf("DATE SELECTION MENU\n\n"); printf("(1) printf("(2) printf("(3) printf("(4)

Convert calendar date into Julian date\n"); Convert Julian date into calendar date\n"); Compute days between two calendar dates\n"); Exit program\n");

printf("\nENTER SELECTION(1-4):"); scanf("%d", option); if (*option >= 1 && *option < 4) { printf("%d\n", *option);

} else if (*option == 4) { printf("User has exited the program.\n"); } else { printf("User entered invalid selection.\n"); } return *option; } void getJulianDate(int* jyear, int* year) { scanf("%d %d", jyear, year); } void getCalendarDate(int* cmonth, int* cday, int* cyear) { scanf("%d %d %d", cmonth, cday, cyear); } int isLeapYear(int year) { return ((!(year % 4) && year % 100) || !(year % 400)); } int toJulian(int month, int day, int year, int* option) { int days = day; switch (month) { case 12: days += 30; case 11: days += 31; case 10: days += 30; case 9: days += 31; case 8: days += 31; case 7: days += 30; case 6: days += 31; case 5: days += 30; case 4: days += 31; case 3: days += 28 + isLeapYear(year); case 2: days += 31; } if (*option == 1) { printf("%d %d\n", days, year); } return days; } void toCalendar(int jyear, int year, int option) { int month, day;

for (month = 1; month < 12; month++) if (jyear < toJulian(month + 1, 1, year, &option)) break; day = jyear - toJulian(month, 1, year, &option) + 1; printf("%d %d %d\n", month, day, year); } void swap(int* a, int* b) { int t; t = *b; *b = *a; *a = t; } int daysInYear(int year) { int numDays; if (isLeapYear(year) == 1) numDays = 366; else numDays = 365; return numDays; } void daysBetweenYears() { int year1, year2, option; int numDays = 0; int day1 = 0; int day2 = 0; int month1 = 0; int month2 = 0; scanf("%d %d %d", &month1, &day1, &year1); scanf("%d %d %d", &month2, &day2, &year2); if (year1 > year2) { swap(&month1, &month2); swap(&day1, &day2); swap(&year1, &year2); } else if (year1 == year2 && month1 > month2) { swap(&month1, &month2); swap(&day1, &day2); swap(&year1, &year2); } else if (year1 == year2 && month1 == month2 && day1 > day2) { swap(&month1, &month2); swap(&day1, &day2); swap(&year1, &year2); }

while (year1 < year2) { numDays += daysInYear(year1); year1++; } numDays += toJulian(month2, day2, year2, &option); numDays -= toJulian(month1, day1, year1, &option); printf("%d\n", numDays); }

Based on all of the test data given in the chart on page 246 in Stegman, format the input data so it can be read by your program, and place the input to your program here: 1 11 15 1922 1 2 29 1984 1 7 7 2000 2 53 1947 2 211 1995 2 360 2006 3 5 12 1949 8 16 1900 3 12 15 1985 3 1 1986 3 1 1 1900 7 7 1993

Place the output of the program here: 319 1922 60 1984 189 2000 2 22 1947 7 30 1995 12 26 2006 17801 76 34155...


Similar Free PDFs