+ Reply to Thread
Page 2 of 2
FirstFirst 1 2
Results 11 to 19 of 19

Thread: Please help me I have a very big problem!!

  1. #11
    Programmer Mathematix is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    104

    Re: Please help me I have a very big problem!!

    I want to post solutions for all, but I won't. The leap year one is very, very easy, though! What is unique about a leap year? It is divisible by four exactly, so if:

    Code:
    if(year % 4 == 0)
       // we have a leap year!
    else
       // we don't have a leap year.
    I will contain myself now...

  2. #12
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,698
    Blog Entries
    57

    Re: Please help me I have a very big problem!!

    The catch being that if it is divisible by 100, it must also be divisible by 400 to be a leap year.
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  3. #13
    Guru ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski's Avatar
    Join Date
    Mar 2009
    Posts
    1,374

    Re: Please help me I have a very big problem!!

    Indeed, Winged. This is why I gave a hint to look into Wikipedia. Mathematix, you earned some -rep for yourself. Giving wrong (unconfirmed) intel is as good as kicking someone's face.

    See some pseudocode: Leap year - Wikipedia, the free encyclopedia

  4. #14
    Programmer Mathematix is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    104

    Re: Please help me I have a very big problem!!

    Quote Originally Posted by ArekBulski View Post
    Indeed, Winged. This is why I gave a hint to look into Wikipedia. Mathematix, you earned some -rep for yourself. Giving wrong (unconfirmed) intel is as good as kicking someone's face.

    See some pseudocode: Leap year - Wikipedia, the free encyclopedia
    And I've had to -rep you right back. Had to because you didn't provide a proper definition of your own nor solution.

    I'll post a solution when I get home tonight - it's easy enough.

  5. #15
    Guru ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski's Avatar
    Join Date
    Mar 2009
    Posts
    1,374

    Re: Please help me I have a very big problem!!

    Here it is. Is this what you would like to see? Honestly, I prefer the DateTime class to do the job. Just between you and me, I do not like to give people cooked-ready solutions. I prefer just to give clues. Oh, forgot to add that this is C#.

    Code:
    static void Main(string[] args)
    {
        for (
    int year 1980year 2009year++)
        {
            
    Console.WriteLine("IsYearLeap says {0} when DateTime says {1}",
                
    IsYearLeap(year), DateTime.IsLeapYear(year));
        }
    }

    private static 
    bool IsYearLeap(int year)
    {
        if (
    year 400 == 0)
            return 
    true;
        if (
    year 100 == 0)
            return 
    false;
        if (
    year == 0)
            return 
    true;
        return 
    false;

    Attached Thumbnails Please help me I have a very big problem!!-screenie-ran-exe.jpg  
    Attached Files
    Last edited by ArekBulski; 06-26-2009 at 10:07 AM.

  6. #16
    Programmer Mathematix is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    104

    Re: Please help me I have a very big problem!!

    Quote Originally Posted by ArekBulski View Post
    Here it is. Is this what you would like to see? Honestly, I prefer the DateTime class to do the job. Just between you and me, I do not like to give people cooked-ready solutions. I prefer just to give clues. Oh, forgot to add that this is C#.

    Code:
    static void Main(string[] args)
    {
        for (
    int year 1980year 2009year++)
        {
            
    Console.WriteLine("IsYearLeap says {0} when DateTime says {1}",
                
    IsYearLeap(year), DateTime.IsLeapYear(year));
        }
    }

    private static 
    bool IsYearLeap(int year)
    {
        if (
    year 400 == 0)
            return 
    true;
        if (
    year 100 == 0)
            return 
    false;
        if (
    year == 0)
            return 
    true;
        return 
    false;



    That isn't the solution! I'll do it later.

  7. #17
    Programmer Mathematix is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    104

    Re: Please help me I have a very big problem!!

    At last I had some time to knock up some code. I used VC++ 2008 to build as those who are curious can build the code without having to purchase a nice IDE!

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <malloc.h>
    
    #define GEORGIAN_LEAP 4
    #define NOT_LEAP_YEAR 100
    #define IS_LEAP_YEAR 400
    
    #define HAS_DAYS_31 31
    #define HAS_DAYS_30 30
    #define HAS_DAYS_28 28
    #define HAS_DAYS_29 29
    
    const char* months[] = 
    {
    	"January",
    	"February",
    	"March",
    	"April",
    	"May",
    	"June",
    	"July",
    	"August",
    	"September",
    	"October",
    	"November",
    	"December"
    };
    
    struct monthDays
    {
    	char* mths;
    	int numdays;
    };
    
    int main()
    {
    
    	// We initialise all months apart from Febuary until we determine a leap year!
    	monthDays yearMonths[12];
    	for(int i = 0; i < 12; i++)
    	{
    		yearMonths[i].mths = (char*)months[i];
    		if(i == 1) // DON'T set February number of days yet!
    		{
    			continue;
    		}
    
    		// Fill the months and associated days!
    		if(strstr(yearMonths[i].mths, months[0]) ||		// January
    		   strstr(yearMonths[i].mths, months[2]) ||		// March
    		   strstr(yearMonths[i].mths, months[4]) ||		// May
    		   strstr(yearMonths[i].mths, months[6]) ||		// July
    		   strstr(yearMonths[i].mths, months[7]) ||		// August
    		   strstr(yearMonths[i].mths, months[9]) ||		// October
    		   strstr(yearMonths[i].mths, months[11]))		// December
    		{
    			yearMonths[i].numdays = HAS_DAYS_31;
    			continue;
    		}
    
    		if(strstr(yearMonths[i].mths, months[3]) ||		// April
    		   strstr(yearMonths[i].mths, months[5]) ||		// June
    		   strstr(yearMonths[i].mths, months[8]) ||		// September
    		   strstr(yearMonths[i].mths, months[10]))		// November
    		{
    			yearMonths[i].numdays = HAS_DAYS_30;
    			continue;
    		}
    
    		printf("%s has %d days.\n", yearMonths[i].mths, yearMonths[i].numdays);
    	}
    
    	// seed the year and days entered by user.
    	int year = 0, days = 0;
    
    	puts("Enter a year: ");
    	scanf("%i", &year);
    	puts("Enter the days: ");
    	scanf("%i", &days);
    
    	int daysSum = 0;
    	int exactDate = 0;
    	char* currentMonth = (char *) malloc(sizeof(char) * 30);
    
    	if((year % GEORGIAN_LEAP) == 0)	// We've fulfilled the condition for a 'Georgian leap'
    	{
    
    		if(((year % NOT_LEAP_YEAR) != 0) || ((year % IS_LEAP_YEAR) == 0))	// Other conditions fulfilled.
    		{
    			yearMonths[1].numdays = HAS_DAYS_29;	// Feb has 29 days in a leap year.
    			printf("We have a leap year! (%d)\n", year);
    
    			// Work out the date.
    			int i = 0;
    			while(i < 11)
    			{
    				if((daysSum + yearMonths[i].numdays) >= days)
    				{
    					break;
    				}
    
    				daysSum += yearMonths[i].numdays;
    
    				i++;
    			}
    
    			currentMonth = yearMonths[i].mths;
    			if((days - daysSum) == 0)
    				exactDate = 31;
    			else
    				exactDate = days - daysSum;
    
    			printf("It is %s %d\n", currentMonth, exactDate);
    		}
    	}
    	else
    	{
    		yearMonths[1].numdays = HAS_DAYS_28;	// Feb has 28 days in a common year.
    		printf("We do not have a leap year, alas... (%d)\n", year);
    
    			// Work out the date.
    			int i = 0;
    			while(i < 12)
    			{
    				if((daysSum + yearMonths[i].numdays) >= days)
    				{
    					break;
    				}
    
    				daysSum += yearMonths[i].numdays;
    
    				if(i < 11)
    					i++;
    			}
    
    			currentMonth = yearMonths[i].mths;
    			if((days - daysSum) == 0)
    				exactDate = 31;
    			else
    				exactDate = days - daysSum;
    
    			printf("It is %s %d\n", currentMonth, exactDate);
    	}
    }
    I've left it monolithic so that it's easier to work through. given that this is a homework assignment I've made the commenting rather scant forcing the OP to do some significant analysis. Am happy to take questions, though.

    Also feel free to point out any bugs and I might fix them.

  8. #18
    Guru ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski's Avatar
    Join Date
    Mar 2009
    Posts
    1,374

    Re: Please help me I have a very big problem!!

    He he, actually none of our solutions is exactly an answer. You see, this is a Managed C++ section and neither of our code is in it.

  9. #19
    Programmer Mathematix is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    104

    Re: Please help me I have a very big problem!!

    Quote Originally Posted by ArekBulski View Post
    He he, actually none of our solutions is exactly an answer. You see, this is a Managed C++ section and neither of our code is in it.
    I beg to differ. If the OP can't handle such problems, I doubt they know the difference between managed and non-managed C++ and have actually posted on the wrong forum.

    Secondly given the it was mentioned at the end that they can only use loops, arrays and if-statements, none of those are specific to managed code.

+ Reply to Thread
Page 2 of 2
FirstFirst 1 2

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. X Y Z problem
    By Disc339 in forum General Programming
    Replies: 4
    Last Post: 01-15-2009, 09:39 AM
  2. Peculiar UI Problem Needs Tackling
    By adriyel in forum C# Programming
    Replies: 2
    Last Post: 04-06-2008, 07:46 AM
  3. Problem read pwd protected Access2K dbase - CR9 & VB6
    By mrbar in forum Visual Basic Programming
    Replies: 2
    Last Post: 03-10-2008, 04:50 AM
  4. How to tackle a programming problem?
    By TcM in forum General Programming
    Replies: 10
    Last Post: 01-07-2008, 11:29 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts