Page 1
Page 1
Started By
Message

C# Help. I always have dumb face on.

Posted on 2/4/15 at 1:44 pm
Posted by Hu_Flung_Pu
Central, LA
Member since Jan 2013
22163 posts
Posted on 2/4/15 at 1:44 pm
I need some help with C#. I am in a LSU class and the structure of the class has a lot to be desired. When lab rolls around, we are just asked to do the problems without a sliver of an idea on what to do.

My brain hurts too much after lab. Tech boarders, please be my aspirin.
Posted by jeff5891
Member since Aug 2011
15761 posts
Posted on 2/4/15 at 1:50 pm to
If its intro, most problems can be found online BC many ciriculums follow the same path for learning how to code.

Make flash cards
Posted by MamouTiger65
Baton Rouge, La
Member since Oct 2007
794 posts
Posted on 2/4/15 at 2:15 pm to
Best way to learn is to just code. Write something simple, tweak it a lot until you understand how and why it works, then add something new and start over.
Posted by Hu_Flung_Pu
Central, LA
Member since Jan 2013
22163 posts
Posted on 2/4/15 at 2:19 pm to
quote:

Best way to learn is to just code.


That's the problem though. I don't know how to start it. There are so many different ways of doing it that it's hard to understand what to do.

Most of the stuff online just shows what it does and not how to use it.
Posted by MamouTiger65
Baton Rouge, La
Member since Oct 2007
794 posts
Posted on 2/4/15 at 2:23 pm to
Search online, plenty of sites for beginners

LINK /
LINK
Posted by Hu_Flung_Pu
Central, LA
Member since Jan 2013
22163 posts
Posted on 2/4/15 at 3:36 pm to
Thank you so much.
Posted by Doldil
The Ham
Member since Jan 2010
6214 posts
Posted on 2/4/15 at 3:36 pm to
quote:

I need some help with C#.


not to be that guy...but could you give an example as to what it is you need help with. Is it the basics...such as setting your variables, or is it a certain type of coding issue...say if/then statements or do loops or...?

maybe give an example as to a problem you have to do and what is throwing you for a loop.
Posted by Hu_Flung_Pu
Central, LA
Member since Jan 2013
22163 posts
Posted on 2/4/15 at 3:38 pm to
1. Implement a console application that does the following: Prompt and read in two DateTime values from the user. Calculate and output a) if the first date is before the second or not; b) the difference in hours between the two dates, and c) if the second date falls in a leap year.

2. Write a console program for the following (you can add this to the same program as above if desired). Prompt the user to enter a value.
• If it is a date, write it in long date format (ex.: February 11, 2015).
• If it is numeric, write it twice (on the same line), once as a currency value, and the second in “9,990.000” style format.

3. Write a form application that does all the following (have a separate button for each on the form):
a. Given a number of days N that a user specifies in a textbox, calculate and output to a label the date N days from the current date (in short date format). N can be negative, but must be numeric. Validate user input is numeric before attempting the calculation.
b. Without using the C# date functions, write the number of days in a month to a label given a user specified month and year. In addition to the basic rule of "30 days hath September, April, June, and November; All the rest have thirty-one except February (which has 28)", there are the rules concerning leap years (which adds an extra day):
A year is a leap year if it is divisible by 4
EXCEPT when the year is a centennial
UNLESS it is also divisible by 400.
Also write the result from the C# DateTime .IsLeapYear() function to a separate label.


I was eventually able to do 1 and 2 but needed some help. 3b is where I am stuck. Teacher wants us to do cases.
Posted by Doldil
The Ham
Member since Jan 2010
6214 posts
Posted on 2/4/15 at 3:49 pm to
Well, I'm gonna be perfectly honest with you...it's been damn near 8 years since I did something like that, so I'm not going to be of much help. I think you can read up on case/switch statements to kind of see how to write one...but as others have said, you're just going to have to play with it to really understand it.

Also, if you're having problems with your code not working, make sure your using your step-through options to go through the code line by line to see where it's breaking...or to help understand why it's doing what it's doing.

TL;DR sorry, can't help much
Posted by MamouTiger65
Baton Rouge, La
Member since Oct 2007
794 posts
Posted on 2/4/15 at 4:00 pm to
Sounds like you are looking for something like this.

int days = 0;


switch (month)
{
case "September":
days = 30;
break;
case "April":
days = 30;
break;
case "June":
days = 30;
break;
case "November":
days = 30;
break;
case "February":
if((year % 4 == 0 && year % 100 != 0) || (year % 4 == 0 && year % 400 == 0))
{
days = 29;
}
else
{
days = 28;
}
break;
default:
days = 31;
break;
}
Posted by Korkstand
Member since Nov 2003
28705 posts
Posted on 2/4/15 at 4:08 pm to
quote:

(year % 4 == 0 && year % 400 == 0)
Don't need to check 4 if you are also checking 400, though it will obviously still work.
Posted by MamouTiger65
Baton Rouge, La
Member since Oct 2007
794 posts
Posted on 2/4/15 at 4:11 pm to
quote:

quote:(year % 4 == 0 && year % 400 == 0)Don't need to check 4 if you are also checking 400, though it will obviously still work.


Next Lesson: Refactoring
Posted by Korkstand
Member since Nov 2003
28705 posts
Posted on 2/4/15 at 4:14 pm to
quote:

Next Lesson: Refactoring
Posted by Hu_Flung_Pu
Central, LA
Member since Jan 2013
22163 posts
Posted on 2/4/15 at 4:22 pm to
quote:

MamouTiger65



That looks like what he showed another student. The problem is I don't know why to put
if((year % 4 == 0 && year % 100 != 0) || (year % 4 == 0 && year % 400 == 0))

I know that it is a boolean statement but I wouldn't know how to correctly code it. I would never have guessed to put it in that fashion. If that makes sense.
Posted by MamouTiger65
Baton Rouge, La
Member since Oct 2007
794 posts
Posted on 2/4/15 at 4:41 pm to
This is just translating the requirements he gave you into syntax. Once you get more familiar with the syntax you will start to see it form as you read the requirements.

(year % 4 == 0) means that the year is divisible by 4. If anything but 0 came back it means that there is a remainder and it isn't divisible.

For the centennial we check year % 100 and then year % 400.

This mean that year is divisible by 4 and not a centennial.
(year % 4 == 0 && year % 100 != 0)

This means that year is divisible by 4 and is divisible by 400.
(year % 4 == 0 && year % 400 == 0)

So we say if either of those statements is true, then the month will have 29 days.

if((year % 4 == 0 && year % 100 != 0) || (year % 4 == 0 && year % 400 == 0))
Posted by Korkstand
Member since Nov 2003
28705 posts
Posted on 2/4/15 at 5:44 pm to
quote:

The problem is I don't know why to put
if((year % 4 == 0 && year % 100 != 0) || (year % 4 == 0 && year % 400 == 0))

I know that it is a boolean statement but I wouldn't know how to correctly code it. I would never have guessed to put it in that fashion. If that makes sense.

It can be tricky to just bang out a line like that in one go. It might make more sense to break it down into individual expressions and operations, and then figure out how to work those into boolean logic. And you really need to pay attention to your parentheses, and how they are nested.

This is how I would go about solving this problem. I am only concerned with 4 categories of years: those that are evenly divisible by 400, 100, 4, or none of those. So right off the bat I know I'm going to need these:

year % 400
year % 100
year % 4

Because the modulo operator % returns the remainder, I know that I will need to check if these expressions evaluate to 0 (or not), in order to determine if the year is evenly divisible by these numbers. So let's start like this:

if(year % 4 == 0)

So, if the year is, for example, 2096, 2100, 2104, etc., the remainder when divided by 4 is zero, and 0 == 0 is TRUE, so the if statement will execute and set days = 29. But, of course, 2100 will not be a leap year, so we have to fix that:

if(year % 4 == 0 && year % 100 != 0)

So now we put that AND in there, which means the stuff on both sides of the && have to be TRUE in order for the if to execute. So now for the year 2100, when we divide by 100 the remainder is zero, and 0 != 0 is FALSE. Since this test fails, it doesn't matter that the other side of the && is TRUE, the if statement is still FALSE, so the else will catch it and set days = 28. This is great, except now we have to catch the years that are multiples of 400:

if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))

Now, if the stuff before the || OR the stuff after the || is TRUE, then the if will execute. So what we've done here is just to add the special case for years that are multiples of 400. The years 2000, 2400, etc will make our if TRUE regardless of what happens on the left of our ||. Likewise, the year 1996, which is evenly divisible by 4 and NOT by 100, will make the entire if be TRUE regardless of what's on the right side of the ||.

And, of course, if the stuff on the left and the stuff on the right are FALSE, only then will our else come into play and set days = 28.



I hope all of this helps, at least a little bit, rather than makes you more confused.
Posted by Hu_Flung_Pu
Central, LA
Member since Jan 2013
22163 posts
Posted on 2/5/15 at 10:45 am to
quote:

Korkstand

quote:

MamouTiger65


Y'all are awesome. Thanks! I may need your help again.
Posted by whodatfan
Member since Mar 2008
21328 posts
Posted on 2/5/15 at 12:04 pm to
quote:

Korkstand


first pageprev pagePage 1 of 1Next pagelast page
refresh

Back to top
logoFollow TigerDroppings for LSU Football News
Follow us on Twitter, Facebook and Instagram to get the latest updates on LSU Football and Recruiting.

FacebookTwitterInstagram