Could anyone please tell me where i went wrong in my code? im trying to make a code where a user enters a number and they enter dates corresponding to that number, then the program sorts the dates in ascending order as the output. My idea for this was the program calculates the days for each entered year from time 0 and then int sorts the dates in ascending order.
Code:
#include <stdio.h>
#include <math.h>
int noofdaysinmonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 ,31 };
int day = 0, month = 0, year = 0;
int totaldays = 0;
int datearray[30] = {};
int amntofdates()
{
int dateamount = 0;
printf("how many dates do you want to sort?\n");
scanf("%d", &dateamount);
return dateamount;
}
/* this function sets the leap year conditions for the first date*/
int leapyear()
{
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
{
return 1;
}
else
{
return 0;
}
}
/*this function checks if date for first date is valid and within the range,
and it changes the day of feb in the array to 29 if the year entered is a leapyear
and finally it calculates the total amount of days from time 0 to the entered date. */
int datecalculation(int dates)
{
for(int date; date <= amntofdates(); date++){
if(date == amntofdates() && (date>= 1 && date<=30)){
printf("enter your dates: ");
scanf("%d/%d/%d", &day, &month, &year);
}
else{
return 0;
}
if(leapyear() == 1){
noofdaysinmonth[1]=29;
}
else{
noofdaysinmonth[1] =28;
}
if((year <= 0 || year > 9999) || (month < 1 || month > 12) ||(day > noofdaysinmonth[month - 1]|| day < 1)) {
fprintf(stderr, "INVALID DATE\n");
}
else{
int i = 0;
totaldays = ((year-1)* 365)+ day ;
for(i = 0; i <month-1; i++)
totaldays = totaldays + noofdaysinmonth[i];
return 0;
}
}
}
int main()
{
while(datecalculation(totaldays) != 0);
printf("%d", totaldays);
return 0;
}