I want to Write a program that determines the day number (1 to 336) in a year for a date that is provided as input data. As an example, January 1, 1994, is day 1. December 31, 1993, is day 365. December 31, 1996, is day 336, since 1996 is a leap year. A year is a leap year if it is divisible by four, expect that any year divisible by 100 is a leap year only if it is divisible by 400. my program should accept the month, day and year as integers. (Include a function leap that returns 1 if called with a leap year, 0 otherwise).

:S i can't do it by myself :sad:

Can you do any part of it? Because Daniweb's rules state that you can't just post program requirements and expect someone to give you the code. You'll get more help by trying something and then asking specific questions if you have problems.

The trick to writing complex programs is to break it down into much smaller parts. For example the first thing you want to do is write a program that lets you enter the date as a character array. Once you have that working, take the char array and break it down to its integer parts -- day, month, and year.

Most programs that calculate that sort of thing use an array for very quick lookup of the number of days from 1 Jan to the 1st day of the desired month. We know that January always has 31 days, as do several other months. So we can just create an array like this:

int days_of_months[] = {31, // January
                        28, // February
                        31, // March
                    // etc for all 12 months
};

And for days in year

int days_in_year[] = {0, // January
                      31, // 1 Jan to 1 Feb
                      59, // 1 Jan to 1 March (ignoring leap year)
                      89, // 1 Jan to 1 apr
                // etc. etc for the remaining months
};

With the above, its pretty simple to figure out the number of days from 1 Jan to the 1st day of any given month by just using the month number as the index into the array. int days = days_in_year[ month ]; Once you have done that, then just add the day of the current month. So if you have a date string "15 February 2012" (where January = 0, February = 1, etc) int days = days_in_year[1] + 15; Now all you have to do is determine whether you need to add 1 for Leap year or not. You already posted the algorithm to do that, so just take the year and test it against the algorithm you posted.

Ancient Dragon thx for your help

#include <stdio.h>

void main()
{
    int d,m,y;
    int days=0;
    int k;                            // return var
    printf("input date dd.mm.yyyy\n");
    scanf("%d.%d.%d",&d,&m,&y);
    m--;
    switch(m)
    {
        case 11:days=30;
        case 10:days+=31;
        case 9:days+=30;
        case 8:days+=31;
        case 7:days+=31;
        case 6:days+=30;
        case 5:days+=31;
        case 4:days+=30;
        case 3:days+=31;
        case 2:if((!(y%4))&&(y%100))
                   days+=29;
               else if(!(y%400))
                   days+=29;
               else
                   days+=28;
        case 1:days+=31;
        case 0:break;
    }
    k=leap(y);
    printf("days:%d\nleap year:%d\n\n",days+d,k);
}

int leap(int year)
{
    int k;
    if((!(year%4))&&(year%100))
        k=1;
    else if(!(year%400))
        k=1;
    else
        k=0;
    
    return k;
}

actually you dont need a function for leap year detection...

just take a variable and put it 1 or 0 in the if/else if/else part in case 2

int whatever;
               case 2:if((!(y%4))&&(y%100)){
                   days+=29;
                   whatever=1;
                   }
               else if(!(y%400)){
                   days+=29;
                   whatever=1;
                   }
               else{
                   days+=28;
                   whatever=0;
                   }

sorry for double posting, i wasnt able to edit my previous post again

#include <stdio.h>

void main()
{
    int d,m,y;
    int days=0;
    int k;                            // return var
    printf("input date dd.mm.yyyy\n");
    scanf("%d.%d.%d",&d,&m,&y);
    m--;
    switch(m)
    {
        case 11:days=30;
        case 10:days+=31;
        case 9:days+=30;
        case 8:days+=31;
        case 7:days+=31;
        case 6:days+=30;
        case 5:days+=31;
        case 4:days+=30;
        case 3:days+=31;
        case 2:if((!(y%4))&&(y%100))
                   days+=29;
               else if(!(y%400))
                   days+=29;
               else
                   days+=28;
        case 1:days+=31;
        case 0:break;
    }
    k=leap(y);
    printf("days:%d\nleap year:%d\n\n",days+d,k);
}

int leap(int year)
{
    int k;
    if((!(year%4))&&(year%100))
        k=1;
    else if(!(year%400))
        k=1;
    else
        k=0;
    
    return k;
}

Nice try, but it only calculates the days for one month. What it needs is the number of days from 1 Jan to the beginning of the specified month.

#include <stdio.h>

void main()
{
    int d,m,y;
    int days=0;
    int k;                            // return var
    printf("input date dd.mm.yyyy\n");
    scanf("%d.%d.%d",&d,&m,&y);
    m--;
    switch(m)
    {
        case 11:days=30;
        case 10:days+=31;
        case 9:days+=30;
        case 8:days+=31;
        case 7:days+=31;
        case 6:days+=30;
        case 5:days+=31;
        case 4:days+=30;
        case 3:days+=31;
        case 2:if((!(y%4))&&(y%100))
                   days+=29;
               else if(!(y%400))
                   days+=29;
               else
                   days+=28;
        case 1:days+=31;
        case 0:break;
    }
    k=leap(y);
    printf("days:%d\nleap year:%d\n\n",days+d,k);
}

int leap(int year)
{
    int k;
    if((!(year%4))&&(year%100))
        k=1;
    else if(!(year%400))
        k=1;
    else
        k=0;
    
    return k;
}

please refrain from doing the OP's work for him
read this:http://www.daniweb.com/software-development/computer-science/threads/573
and use int main instead of void main
read this: http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044841143&id=1043284376

Nice try, but it only calculates the days for one month. What it needs is the number of days from 1 Jan to the beginning of the specified month.

wrong. it calculates all days of the year up to the current date. as you can see the switch cases do not get breaked, therefore it sums up all previous months.

commented: right :) +17

wrong. it calculates all days of the year up to the current date. as you can see the switch cases do not get breaked, therefore it sums up all previous months.

Oh yes :) I see it now.

Check out of this. Hope this help.

#include "stdafx.h"


int CheckLeapYear(int year){

	// Check out leap year
	if (year % 4 == 0 && year % 100 != 0)
		return 1;
	if (year % 400 == 0)
		return 1;
	return 0;
}

int DayMaxOfMonth(int day, int month, int year) {
	
	// Find out the max value of the month
	if (day > 0 && month > 0 && year > 0) {
		
		switch(month)
		{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			return 31;
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			return 30;
			break;
		case 2:
			{
				if(CheckLeapYear(year) == 1)
					return 29;
				return 28;
			}
		}
	}
	return 0;
}

int CheckDay(int day, int month, int year) {
	
	// Check the input is suitable or not
	if (month >= 1 && month <= 12) {
		if (day >= 1 && day <= DayMaxOfMonth(day, month, year))
			return 1;
		return 0;
	}
	return 0;
}

void Input(int &day, int &month, int &year) {
	
		
		printf("Input day: ");
		scanf("%d", &day);
		printf("Input month: ");
		scanf("%d", &month);
		printf("Input year: ");
		scanf("%d", &year);
	
}



void Output(int day, int month, int year) {
	
		printf("Day / Month / Year: %d / %d / %d\n", day, month, year);
}

void main(){
	int day;
	int month;
	int year;
	
	do
	{
		Input(day, month, year);
	}
	while(CheckDay(day, month, year) == 0);

	Output(day, month, year);
}

thx for all irre & Ancient Dragon & zeroliken but its not homework its a challenge

The reason this looks like homework is because:
1) It's in C.
2) You want someone else to do it.
3) Another poster is asking for exactly the same thing at the same time.
4) It's a Leap Year calculator.
5) It has been a homework assignment for a while.

The reason it doesn't seem like just a challenge:
1) See all the reasons above.

There are plenty of examples available on the net.

Get help with homework is not the problem.
Please try something.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.