Closed Thread
Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

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

  1. #11
    Mathematix is offline Programmer
    Join Date
    Jun 2009
    Posts
    112
    Rep Power
    0

    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. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #12
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143

    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.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #13
    Join Date
    Mar 2009
    Posts
    1,375
    Rep Power
    24

    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

  5. #14
    Mathematix is offline Programmer
    Join Date
    Jun 2009
    Posts
    112
    Rep Power
    0

    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.

  6. #15
    Join Date
    Mar 2009
    Posts
    1,375
    Rep Power
    24

    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 Attached Thumbnails Please help me I have a very big problem!!-screenie-ran-exe.jpg  
    Attached Files Attached Files
    Last edited by ArekBulski; 06-26-2009 at 08:07 AM.

  7. #16
    Mathematix is offline Programmer
    Join Date
    Jun 2009
    Posts
    112
    Rep Power
    0

    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.

  8. #17
    Mathematix is offline Programmer
    Join Date
    Jun 2009
    Posts
    112
    Rep Power
    0

    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.

  9. #18
    Join Date
    Mar 2009
    Posts
    1,375
    Rep Power
    24

    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.

  10. #19
    Mathematix is offline Programmer
    Join Date
    Jun 2009
    Posts
    112
    Rep Power
    0

    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.

Closed Thread
Page 2 of 2 FirstFirst 12

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. If problem or cout problem?
    By chaoticape in forum C and C++
    Replies: 4
    Last Post: 06-10-2011, 10:29 AM
  2. C: Problem with solving problem
    By rakche in forum C and C++
    Replies: 15
    Last Post: 03-28-2010, 01:24 PM
  3. Replies: 0
    Last Post: 04-26-2007, 05:33 PM

Tags for this Thread

Bookmarks

Posting Permissions

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