Date Lab: help with c programming, below is the instructions. This assignment wi
ID: 3919022 • Letter: D
Question
Date Lab: help with c programming, below is the instructions.
This assignment will focus on the use of functions and the passing of parameters. You are to construct a C program, date.c, which performs each of the following operations.
Converts a calender date into a Julian date.
Converts a Julian date into a calender date.
Computes the number of days between and two calender dates.
A calender date is simply a date that contains a year, month, and day and a Julian date is an integer between 1 and 366, inclusive, which tells how many days habe elapsed since the first of January in the current year (including the day for which the date is calculated). For example, calender date 4/12/2008 is equivalent to Julian date 103, 2008.
Your program should contain a selection menu (hint: think do-while loop) that allows the user to choose one of the options. An illustration is given below:
DATE SELECTION MENU
1) Convert calender date into Julian date
2) Convert Julian date into calender date
3) Compute days between two calender dates
4) Exit program
ENTER SELECTION (1-4):
Be sure your program contains separate functions for each of the required operations, passing parameters as necessary. Remember that no global variables are allowed in your program except for the file pointer. You should create at least the following functions for your program:
displayMenu - displays selection menu and promps user for selection
getCalenderDate - prompts and gets calender date from user
getJulianDate - prompts and gets Julian date from user
toCalender - converts Julian date into calender date toJulian converts calender date into Julian date
daysBetweenDates - calculates the number of days between two calender dates
Hint to compute the number of days between two calender dates: For each date, figure out the number of days since January 1, 1900 and then subtract.
For this assignment we will define a leap year as any year that is evenly divisble by 4 but not 100, except that years divisble by 400 are leap years. Here's a function you can use to calculate leap years. Try and work through its details
int isLeapYear(int year)
{ return ((!(year %4) && (year % 100) || !(year % 400)); }
Remember that the main () function should appear as the first function in the program. Be sure to use function prototypes for each of the functions that are used in your program.
Test data for the lab is given below.
Convert Calender Date Into Julian Date:
Convert Julian Date into Calender Date:
Compute number of days between two calender dates:
Be sure to turn in output for each of the test data provided above. The information appearing in the parentheses after each piece of the test data are the correct (hopefully) solutions. You may use these solutions to test your program on the supplied test data. Ultimately, however, your program should be able to run on any valid data.
Extra Credit:
Given a calender date and the number of days until some future event, determine the calender date of the future event.
Given any calender date, determine its corresponding day of the week. You may assume that January 1, 1990 was a Monday. (Hint: think mod 7).
Use the following extra credit test data:
Compute future date:
Compute day of week:
Output from your program should be sent to the terminal window (your screen) as well as the requested csis.txt output file. Be sure to read the document on Capturing Program Output. Be sure to include the csis.txt output file in your zip archive.
11 15 1922 (319, 1922) 2 29 1984 ( 60, 1984) 7 7 2000 (189, 2000)Explanation / Answer
#include<stdio.h>
int days_in_month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
typedef struct date{
int d,m,y;
}date;
int countLeapYears(date d)
{
int years = d.y;
if (d.m <= 2)
years--;
return years / 4 - years / 100 + years / 400;
}
int is_leapyear(int year){
if(year% 4 ==0 && year%100 != 0 || year%400 == 0)return 1;
else return 0;
}
int getDifference(date dt1, date dt2)
{
long int n1 = dt1.y*365 + dt1.d;
for (int i=1; i<=dt1.m - 1; i++)
n1 += days_in_month[i];
n1 += countLeapYears(dt1);
long int n2 = dt2.y*365 + dt2.d;
for (int i=1; i<=dt2.m - 1; i++)
n2 += days_in_month[i];
n2 += countLeapYears(dt2);
return (n2 - n1);
}
int main(){
date d1,d2;
int choice;
int day,month,year,total_day;
do{
printf(" 1) Convert calender date into Julian date 2) Convert Julian date into calender date"
" 3) Compute days between two calender dates 4) Exit ");
scanf("%d",&choice);
switch(choice){
case 1:{printf("Enter day : ");
scanf("%d",&d1.d);
printf("Enter month : ");
scanf("%d",&d1.m);
printf("Enter year : ");
scanf("%d",&d1.y);
total_day=0;
for(int i=1;i<d1.m;i++)total_day+=days_in_month[i];
total_day+=is_leapyear(d1.y);
printf("Date in julian is : ( %d, %d ) ",total_day+d1.d,d1.y);
break;
}
case 2:{month=1;
printf("Enter total day : ");
scanf("%d",&total_day);
printf("Enter year : ");
scanf("%d",&year);
while(total_day>days_in_month[month]){
if(is_leapyear(year)&&month==2){
total_day-=29;
}
else total_day-=days_in_month[month];
month++;
}
printf("Date in calendar is : ( %d/ %d/ %d )",total_day,month++,year);
break;
}
case 3:{
printf("Enter date 1 ");
printf("Enter day : ");
scanf("%d",&d1.d);
printf("Enter month : ");
scanf("%d",&d1.m);
printf("Enter year : ");
scanf("%d",&d1.y);
printf("Enter date 2 ");
printf("Enter day : ");
scanf("%d",&d2.d);
printf("Enter month : ");
scanf("%d",&d2.m);
printf("Enter year : ");
scanf("%d",&d2.y);
printf("No of days between date1 and date2 : %d ",getDifference(d1,d2));
break;
}
}
}while(choice!=4);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.