Pointers, arrays , functions
Ok got a confusing assignment here.
Instructions are to ask user for current date (month, day, year) and program calculates time passed since Jan 1 2000. We are to assume 1) each year has 365 days 2) each month has 30 days.
Limit to use of basic arrays and pointers. Just basic C stuff.
First function prototype called from main:
Code:
void enter_datea(int *, int *, int *);
// this function is to take information about current date entered on keyboard and information about month, day, year is passed back to main()
My question here is why are pointers necessary???
What are each pointer pointing to???
Second funtion prototype called from main:
Code:
void time_elasped(int , int, int , int *, int *);
//this function takes information about the current date and calculates the time elapsed since January 1, 2000. Time should be calculated in DAYS and HOURS and passed back to main()
Now if you're only entering in months, days, years, how would you know the exact hour??? And how come three integers and 2 pointers passed???
I would appreciate just a bit of a start to get me going here.
now you know my confusion
God knows a bit of this is not correct; but I am trying to understand the use of pointers as was mentioned in directions.
(1) function enter_data seems to be where user inputs current date and should somehow realize pointers for month, for day, and for year
(2) function time_elapsed seems to take the above info and calculate days first and then multiply that by 24 (hours in a day)
Biggest problem I guess is getting the right variables labeled and which ones to use in the functions; so not sure about parameters in function (2).
Code:
#include <stdio.h>
/* define year to 365 days and month to 30 days */
#define YEAR 365
#define MONTH_DAYS 30
/* prototypes */
void enter_data(int *, int *, int *);
void time_elapsed(int, int, int, int *, int*);
main()
{
int month, day, year;
int *month, *day, *year;
int *DAYS, *HOURS;
printf("input current date as MM/DD/YY\n");
enter_data(&month,&day,&year)
time_elapsed(&month,&day,&year,DAYS,HOURS)
printf("time elapsed since January 1, 2000\n" };
printf("time elapsed in days", DAYS);
printf("time elapsed in hours", HOURS);
}
void enter_data(int *month, int *day, int *year)
{
scanf("%d %d %d", &month, &day, &year);
year = year -2000;
month =
day =
}
void time_elapsed(int month, int day, int year, int DAYS, int HOURS)
{
year *= 365;
month *=30;
DAYS = month+day+year;
HOURS = DAYS *24;
}
Us e of pointers is necessary.
Use of pointers in extremely important in your code. If you check the function enter_data(...), the values of year, month and day are being altered. We need the altered values in time_elapsed(...) function. Either way is to return a structure or make these variables as global. But your code is not using them as global variables and we have to stick to basic pointers and array.
Hope I have clarified the point.
Regards,
Mangesh.
solution for second query
Sorry, I forgot to attend the second query.
>> Now if you're only entering in months, days, years, how would you know the exact hour??? And how come three integers and 2 pointers passed???
If you look into the main function, we need DAYS and HOURS only. which are the 4th and 5th parameters in the time_elapsed(...) function to display the elapsed time. We don't need first three parameters in the main function. So only the last 2 parameters are passed as pointers.
Use of ctime etc. from time.h can give you the current time. It will help you to find out the hours also.
Regards,
Mangesh.