14 Replies - 754 Views - Last Post: 17 January 2011 - 05:21 AM Rate Topic: -----

#1 Guest_duck*


Reputation:

is there a way to make my code more efficient?

Posted 16 January 2011 - 06:54 AM

hey
I got the following homework assignment -
"create a program which receives 3 inputs from the user - day month and year, and check if it is a valid date.

this has to be done with switch-case

so I know that I need to check if the month is between 1 and 12, and for every month to check if it has the right amount of days in it -
February - 28 days
months 4,6,9,11 - 30 days
months 3,5,7,8,10,12 - 31 days.

this is what I did -
static void Main(string[] args)
        {
            int month, day, year;
            Console.Write("Enter day: ");
            day = int.Parse(Console.ReadLine());

            Console.Write("Enter month: ");
            month = int.Parse(Console.ReadLine());

            Console.Write("Enter year: ");
            year = int.Parse(Console.ReadLine());

            switch(month)
            {
                case 2:
                    if (day < 0 || day > 28)
                    {
                        Console.WriteLine("False date");
                    }

                    // #######################################################################################################

                    break;
                case 4:
                    if (day < 0 || day > 30)
                    {
                        Console.WriteLine("False date");
                    }
                    break;
                case 6:
                    if (day < 0 || day > 30)
                    {
                        Console.WriteLine("False date");
                    }
                    break;
                case 9:
                    if (day < 0 || day > 30)
                    {
                        Console.WriteLine("False date");
                    }
                    break;
                case 11:
                    if (day < 0 || day > 30)
                    {
                        Console.WriteLine("False date");
                    }
                    break;

                    // ##############################################################################################################

                case 3:
                    if (day < 0 || day > 31)
                    {
                        Console.WriteLine("False date");
                    }
                    break;

                case 5:
                    if (day < 0 || day > 31)
                    {
                        Console.WriteLine("False date");
                    }
                    break;

                case 7:
                    if (day < 0 || day > 31)
                    {
                        Console.WriteLine("False date");
                    }
                    break;

                case 8:
                    if (day < 0 || day > 31)
                    {
                        Console.WriteLine("False date");
                    }
                    break;

                case 10:
                    if (day < 0 || day > 31)
                    {
                        Console.WriteLine("False date");
                    }
                    break;

                case 12:
                    if (day < 0 || day > 31)
                    {
                        Console.WriteLine("False date");
                    }
                    break;
            }



is there a way to make the code a bit shorter? it seems to me like I'm missing the whole point of the assignment, because I'm quite sure I did it in a primitive way...
I mean it's just checking for every month... the code seems to be really encumbered...

help would be appreciated.. thanks!

Is This A Good Question/Topic? 0

Replies To: is there a way to make my code more efficient?

#2 Fungle  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 256
  • View blog
  • Posts: 337
  • Joined: 06-March 10

Re: is there a way to make my code more efficient?

Posted 16 January 2011 - 07:02 AM

Well, What I did is make an if statement, for the 30 day months, You could simply use three of these to cut down the amount of cases you have. there could be a better way of doing it though I guess..


if (month == 4 | month == 6 | month == 9 | month == 11 && day < 0 || day > 30)
            {
                Console.WriteLine("False Date");
            }



I tested it and it works just as well ^^

This post has been edited by Fungle: 16 January 2011 - 07:04 AM

Was This Post Helpful? 0
  • +
  • -

#3 Guest_duck*


Reputation:

Re: is there a way to make my code more efficient?

Posted 16 January 2011 - 07:06 AM

hey
thanks for your reply!

teacher told us we must do this task via switch and case instead of if...
Was This Post Helpful? 0

#4 Fungle  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 256
  • View blog
  • Posts: 337
  • Joined: 06-March 10

Re: is there a way to make my code more efficient?

Posted 16 January 2011 - 07:07 AM

Then you should always do as your teacher says :)

You could try to clean up your comments a bit to reduce the lines :P

This post has been edited by Fungle: 16 January 2011 - 07:08 AM

Was This Post Helpful? 0
  • +
  • -

#5 Guest_duck*


Reputation:

Re: is there a way to make my code more efficient?

Posted 16 January 2011 - 07:11 AM

haha
I added them so I could tell between each 'group' of months.. the 28, 30 and 31 days...

so you can't think of another way of making it shorter & simpler to do with switch & case?
Was This Post Helpful? 0

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: is there a way to make my code more efficient?

Posted 16 January 2011 - 07:12 AM

if (month == 4 | month == 6 | month == 9 | month == 11 && day < 0 || day > 30)


You're mixing bitwise-OR and boolean OR here.
Was This Post Helpful? 1
  • +
  • -

#7 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: is there a way to make my code more efficient?

Posted 16 January 2011 - 07:15 AM

Disclaimer: Always do what your teacher says when you're in class.

You can also create an extension method that wraps all that fugly code away from your main train-of-thought-code:

public static bool IsValidDate(this int date)
{
    //Do your magic here and return true or false.
}


Then in your main code:

day = int.Parse(Console.ReadLine());
if(day.IsValidDate)
{
    //blabla
}


It would be best to just create a DateTime object and then check that. I even think the DateTime object even does that for you! :)
Was This Post Helpful? 0
  • +
  • -

#8 Guest_duck*


Reputation:

Re: is there a way to make my code more efficient?

Posted 16 January 2011 - 07:20 AM

agh
I really can't understand your suggestions, sorry...
I've been programming for less than a month at school, and you guys are suggesting things that I don't know how to do... sorry!

if I'll use those objects and methods mentioned above teacher will know I didn't do it & didn't understand it :P

I was pretty much trying to find out if there is an easier way to do it using switch-case, or is this the only way? writing cases for each month?
Was This Post Helpful? 0

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: is there a way to make my code more efficient?

Posted 16 January 2011 - 07:39 AM

You could put all the 31-day months and 30-day months into one multi-case statement each:
switch(month)
{
    case 4:
    case 6:
    case 9:
    case 11:
        // Handle 30-day month
        break;
    etc.
}

Was This Post Helpful? 2
  • +
  • -

#10 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: is there a way to make my code more efficient?

Posted 16 January 2011 - 07:46 AM

One quick & easy way to shorten the code would be to use fall through with empty case labels:
case 4:
case 6:
case 9:
case 11:
    if (day < 0 || day > 30)
    {
    Console.WriteLine("False date");
    }
    break;

This will catch cases 4, 6, 9, and 11 in the code for case 11.

Edit: Doh! JackOfAllTrades typed faster than me

This post has been edited by Tryparse: 16 January 2011 - 07:47 AM

Was This Post Helpful? 0
  • +
  • -

#11 Nick-Nurgen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-January 11

Re: is there a way to make my code more efficient?

Posted 16 January 2011 - 11:55 AM

Actually I would suggest you to create an enumeration but i think u dont know it yet. And one more thing. By the way for now, correct the If method statement, day cannot be 0. Write <=0 or <1
Was This Post Helpful? 0
  • +
  • -

#12 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: is there a way to make my code more efficient?

Posted 16 January 2011 - 12:26 PM

View PostNick-Nurgen, on 16 January 2011 - 02:55 PM, said:

Actually I would suggest you to create an enumeration but i think u dont know it yet. And one more thing. By the way for now, correct the If method statement, day cannot be 0. Write <=0 or <1


Why would you use an enum to determine if a given month/day/year is a valid date?

The simplest way would be to use the DateTime constructor. If it's not a valid date, it will throw an exception.
Was This Post Helpful? 0
  • +
  • -

#13 Nick-Nurgen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-January 11

Re: is there a way to make my code more efficient?

Posted 16 January 2011 - 12:47 PM

Quote

The simplest way would be to use the DateTime constructor

Quote

Why would you use an enum to determine if a given month/day/year is a valid date?



I understand it is the simplest way and has already been suggested. And many efficient ways can be found by digging into the details. However this person said he's been programming for less than 1 month and the teacher will find it unacceptable if he/she sees the code he/she hasn't yet taught.
So the simplest solution is to forget about the sophisticated tools right now and help him find an efficient code within his collected knowledge. I also rejected enumeration, since im not sure if he's aware of.

This post has been edited by Nick-Nurgen: 16 January 2011 - 12:48 PM

Was This Post Helpful? 0
  • +
  • -

#14 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: is there a way to make my code more efficient?

Posted 16 January 2011 - 01:46 PM

He's already been given an efficient answer for what he has been required to use(switch statements).

I would like to know how I could use enum(s) to determine the validity of a month/day/year combination.
Was This Post Helpful? 0
  • +
  • -

#15 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: is there a way to make my code more efficient?

Posted 17 January 2011 - 05:21 AM

The best way to use an enum here would be to get rid of the magic numbers.

enum Months { January = 1, February, March, April, May, June, July, August, September, October, November, December };

switch(month)
{
    case Months.April:
    case Months.June:
    case Months.September:
    case Months.November:
        // Handle 30-day month
        break;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1