Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Code in C Objective Create a program that will ask for a birthdate and calculate the age in days. Work with symbolic constants, conversion (format)
- Code in C
- Objective
- Create a program that will ask for a birthdate and calculate the age in days.
- Work with symbolic constants, conversion (format) specifiers, and puts() function.
- Related SLO:
- Develop properly structured multifile programs with automatic compilation.
- Instructions
- For file management purposes, you may want to create separate folders for each programming assignment.
- Create a program named LastnameFirstname05.c, that does the following:
- Ensure your program is able to use the custom getdouble() function from the example code.
- To use the getdouble() function, you need the files: getdouble.c, getdouble.h, and makefile-double.
- Download each file by right-clicking on each link above and Save link As...
- After downloading each file to your computer, upload it to your uhunix account and ensure the files are in the same directory as your LastnameFirstname05.c program.
- Use emacs to edit makefile-double so that it will compile and link getdouble.c, getdouble.h, and LastnameFirstname05.c. In other words, you want to replace all instances of inputdouble with LastnameFirstname05.
- Save makefile-double.
- To use the getdouble() function, you need the files: getdouble.c, getdouble.h, and makefile-double.
- Add the following include statement at the top of your program: #include "getdouble.h"
- Define and use at least 1 symbolic constant with #define
- It is up to you to decide what you want to be a constant. You may need to come back to this bullet later.
- Use at least 1 puts() function call.
- It is up to you what you want to print using puts().
- Use int for all variable types, this will make the calculations and output simpler.
- Output a message telling the user what this program does.
- Ask the user to enter a number representing the month, then the day, and then the year they were born.
- Use separate prompts for each piece of information.
- Use the custom getdouble() function to get the information. Typecast the return value to store in your variables. For example: inputMonth = (int) getdouble();
- Declare and initialize 3 variables to store the current date, one for the month, day, and year.
- Use the assignment due date as the current date.
- Do not pre-calculate the amount of days to the current date, the variables should allow the current date to be easily changed for the future.
- Display the current date.
- Calculate the user's age in days.
- Calculate the number of days since year 1 for today's date and the birthdate, then subtract the number of days since year 1 for the birthdate from the number of days since year 1 for today's date.
- Age in Days = (# of days since year 1 for current date) - (# of days since year 1 for birthdate)
- Calculate the number of days since year 1 for the current date by: (1) multiplying the year by 365.25 to account for leap years every four years, (2) subtract one from the month (so September would be 9 - 1 = 8) and multiply this by 365.25/12.0, and (3) adding both results to the day of the month.
- # of days since year 1 for current date = (current year * 365.25) + ((current month - 1) * 365.25/12.0) + (current day)
- Calculate the number of days since year 1 for the birthdate by: (1) multiplying the year by 365.25 to account for leap years every four years, (2) subtract one from the month and multiply this by 365.25/12.0, and (3) adding both results to the day of the month.
- # of days since year 1 for birthdate = (birth year * 365.25) + ((birth month - 1) * 365.25/12.0) + (birth day)
- Calculate the number of days since year 1 for today's date and the birthdate, then subtract the number of days since year 1 for the birthdate from the number of days since year 1 for today's date.
- You can check your calculations by using this website: Age Calculator
- Set the Age at This Date to the due date of this assignment.
- Don't worry if your calculations are off by a few days when comparing to the website, this is due to rounding. However, they should match the example output shown below.
- Alternative Age Calculator, sometimes the one above goes offline: Age Calculator
- Set the Age at the Date of to the due date of this assignment.
- Print out the number of days with an appropriate label.
- [OPTIONAL] When you display the user's age in days, if the age is greater than or equal 1000, put a comma in the 1000s place by separating the number into two parts using division and modulus by 1000.
- In other words, the 1000's place is calculated by (totalDays / 1000). The first three digits are calculated by (totalDays % 1000).
- To include extra zeroes on integers, use %.3i precision as shown in format.c and demonstrated in the second example output.
- Double-check that you used at least 1 symbolic constant and 1 puts() function call.
- At the end of the main function, be sure to return 0;
- Moving forward, this applies to all assignments, regardless if it is stated in the instructions or not.
- Since main is declared as an int function, it should return an int at some point. It is customary to return 0; at the end of main. 0 is a value signaling that the program terminated successfully. Any non-zero value would indicate that the program was not successful or that an error occurred.
- Be sure to have a program description at the top and in-line comments.
- Be sure to have read through the assignment policy for this class.
- Ensure your program is able to use the custom getdouble() function from the example code.
- Example Output
% make -f makefile-double % ./program This program will calculate your age in days. Enter the month you were born (1-12): 1 Enter the day you were born: 1 Enter the year you were born: 2020 Today's date is: 2/1/2022 Birthday is: 1/1/2020 Your age in days is: 760 % ./program This program will calculate your age in days. Enter the month you were born (1-12): 5 Enter the day you were born: 1 Enter the year you were born: 2019 Today's date is: 2/1/2022 Birthday is: 5/1/2019 Your age in days is: 1,004 % ./program This program will calculate your age in days. Enter the month you were born (1-12): 7 Enter the day you were born: 30 Enter the year you were born: 1975 Today's date is: 2/1/2022 Birthday is: 7/30/1975 Your age in days is: 16,985 % ./program This program will calculate your age in days. Enter the month you were born (1-12): 10 Enter the day you were born: 30 Enter the year you were born: 1950 Today's date is: 2/1/2022 Birthday is: 10/30/1950 Your age in days is: 26,025
- Hints
- In my solution, I used the following:
- 1 symbolic constant
- 9 int variables
- The %i format specifier.
- Implementing the optional comma, I used 2 additional integer variables and an if-statement.
- In my solution, I used the following:
- Submission Guidelines
- Submit your LastnameFirstname05.c and makefile-double files to the Digital Dropbox.
- Be sure to check your hawaii.edu e-mail for confirmation of your submission.
- Due Date: Tuesday, Feb. 1 by 11:55pm
- Submit your LastnameFirstname05.c and makefile-double files to the Digital Dropbox.
getdouble.c
#include#include #include #include "getdouble.h" /*This function reads a double from the input steam. It will read up to the MAX number of characters. And will read until the end of file, newline, blank space, or tab. And will leave the rest of the characters on the input steam. If there is any error, a 0.0 is returned. */ double getdouble(){ /*declare character array to store up to MAX characters, and leave one more space for end of string character*/ char buffer[MAX + 1] = {'\0'}; double number = 0.0; int i = 0; char character = 'c'; /*get input from the user*/ for(i=0; i < MAX; i++){ character = getchar(); /*check for end of file*/ if(EOF == character){ break; } /*check for end of line*/ if(' ' == character){ break; } /*check for end of number (space)*/ if(' ' == character){ break; } /*check for a tab*/ if('\t' == character){ break; } buffer[i] = character; } //printf("buffer = %s ", buffer); /*convert to a double*/ number = atof(buffer); /*if number is too large to be represented as a double, return a 0.0*/ if(number == HUGE_VAL || number == -HUGE_VAL){ number = 0.0; } //printf("number = %f ", number); return number; }
getdouble.h
#define MAX 100 double getdouble();
makefile-double
program: inputdouble.o getdouble.o gcc inputdouble.o getdouble.o -o program -lm inputdouble.o: inputdouble.c getdouble.h gcc -c inputdouble.c getdouble.o: getdouble.c getdouble.h gcc -c getdouble.c
format.c
/* Examples of using flags, printing with precision, and puts() function */ #include#define WORD "aloha" #define NUMBER -5 #define LETTER 'Z' #define PI 3.14159265358979323846 //PI to 20 decimal digits int main(){ puts("Example of using flags, width, and precision:"); //right justified, width of 9 spaces printf("%9s%9d%9c%9f ", WORD, NUMBER, LETTER, PI); //left justified (minus sign -), width of 9, print plus or minus sign (plus sign +) printf("%-9s%-+9d%-9c%+-9f ", WORD, NUMBER, LETTER, PI); /* left justified (minus sign -) width of 9 spaces precision (number following the period) is 3 decimal places for floats precision is 3 numerals for integers, so displays leading zeros precision also displays that number of characters, and no more, so can cut off a string */ printf("%-9.3s%-9.3d%-9.3c%-9.3f ", WORD, NUMBER, LETTER, PI); return 0; } /* Example of using flags, width, and precision: aloha -5 Z 3.141593 aloha -5 Z +3.141593 alo -005 Z 3.142 */
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started