![]() |
|
#1
|
|||
|
|||
Leap Year Validation -code formula requestArial
10 navy Hi, I a have 3 days experience with C and need help please in programming a leap year validation formula. I created a flowchart and attempted psuedo code but got stuck on the c formula. Any suggestions are appreciated. Flowchart is attached. Pseudocode attempt: intYear Get year = assign to variable Does the year contain "00" at the end? If intYear instr ('"00") a=intYear\400=yes Else a=intYear\4 End If If a=yesThen Output ="Yes this is a leap year" Else Ouput="No this is not a leap year" End if |
|
#2
|
|||
|
|||
|
Quote:
Welcome to the wonderful world of C. I like your flow chart better than your pseudo-code. I guess the idea of pseudo-code is to transform the 2-dimensional representation of the problem (flow chart) to a 1-dimensional procedural language representation. First of all, I will summarize what I think I would use to solve the problem in C, using the "modulus" operator, %: CPP / C++ / C Code:
A side note: why did I put the /* */ around the above description? Well, they are C language comments, and can be included directly in the program source. Three very important characteristics of good code are: documentation, documentation, and documentation. (Of course having code that actually works is pretty important, too.) Your flow chart is great documentation, but it can get separated from the program, so it's nice to have something in the source itself that reminds you what the heck you were trying to do. (And serves to tell others what you had in mind, in case someone else looks at it.) The key, to me at least, is that the diamond-shaped boxes of the flow chart can all be replaced by conditional procedural statements. One possibility is using C language if statements. Here's the pseudo code that I might have deduced from your flow chart: CPP / C++ / C Code:
Now, this is pseudo-code, not C language, but it's pretty close Just think of "then" as an opening brace: {. And "end if" is like a closing brace: }. You could define a function, say, "is_leap_year". And it could be organized something like the following: CPP / C++ / C Code:
Now, you can make this code part of your main program, where you give it a value for year and let it print the result. Or you can make a function that is called from your main program and returns a value to the main program. The main program then prints out a message, depending on the value from the function. Regards, Dave |
|
#3
|
|||
|
|||
|
Hi Dave,
Thanks for the guidance and encouragement, I couldn't figure out how the computer would read the last 2 digits and now I understand to /by 100. Yes I knew the pseudocode was messed up, thanks on that assist. I have attempted to program in C and here are my new challenges. 1) How to program in that the user must enter the year and then get program to read it 2) Get program to stay open when the user keys the year. We have not got that far in class yet. My instructor told us to use getch() to keep it open yet when I press any key it closes. Oh yes and I read a post on this site that said getchar() was better -not sure why but that won't hold it open with input either 3) I am getting the following error message with my code which I also pasted below Miracle C Compiler (r3.2), written by bts. Compiling C:\Program Files\Miracle C\LeapYearV3.c main C:\Program Files\Miracle C\LeapYearV3.c: line 14: Parse Error, expecting `SEP' 'int is_leap_year(int year)' aborting compile My attempt at code: CPP / C++ / C Code:
Last edited by LuciWiz : 25-Jul-2005 at 00:27.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#4
|
|||
|
|||
|
Quote:
As far as Miracle C: the compiler aborted because something is structurally wrong with your program. Before writing this program, practice with simpler things (like "Hello world"). Read examples in your book or from class. Reproduce them exactly and compile and execute them. Make sure you know how to put things together. Then you can try new stuff. First things first: Have you ever compiled any kind of program? Don't they always show an example that prints out "Hello world" or something? If not, then: find the simplest example from class or from the book, compile and execute it. Then write a program to get user input, and do nothing else (except, maybe, print out the value) Then add the code to do the calculations on user input. If you have had functions, then the calculations are appropriately put into a function. You don't have to do this, and you don't always to this even if you know how. It's really a matter of style. If your instructor tells you to use scanf, then you must know how to use scanf to get user input. Aren't there examples from class or your book? Also, if the instructor tells you to use getch, then you must use getch. This is not a standard C library function, but for compilers that do support it, its prototype is (usually) found in <conio.h> As far as Miracle C, I can't help you, but here is a program that you can try for starters: CPP / C++ / C Code:
Note that for scanf, to get the user's input into a variable, you must give scanf the address of the variable. That's why the second argument is &year, not year. It is always good practice to show what the program is going to use for its data, so I put the printf statement after the scanf to show what it really read. There are more sophisticated and more robust ways to handle user input, but these few lines will get you started. As far as the caculations: If the value of the integer year is 1234, then the value of year/100 is 1200. Integer division throws away any remainder. In order to test whether the year is divisible by 100, one way is to use the C operator '%', as I suggested in the previous post. The value of year%100 is 34 (That's the remainder from integer division.) For positive integers, the modulus operator, '%', gives the remainder of integer division. So to test whether the year is divisible by 100, you could do something like this: CPP / C++ / C Code:
As far as program organization: Unless the instructor specifically told you to use a function for the calculation, then you could just do something like the following: CPP / C++ / C Code:
If he told you to use a function, then it could be something like this: CPP / C++ / C Code:
See the theme here: 1. Do simple stuff, using examples from class or your book, before you jump in and start writing code. 2. Create your program a step at a time: First make sure that you can handle user input. Get that part of the program executing exactly the way that you want it to. Then, and only then, start in on some new stuff. For example: CPP / C++ / C Code:
Regards, Dave |
|
#5
|
|||
|
|||
Thank youHi Dave,
Wow thanks for the detailed explanation. Yes I did the standard Hello World and a few other basic practices. Your information is the next step that I needed and I appreciate the time you took to give me the information. I will be reviewing it and following your suggestions this upcoming week. Thanks again. |
|
#6
|
||||
|
||||
|
You might want to suggest to your instructor that Borland 5.5 C/C++ compiler is
1) a 32 bit compiler 2) Newer and handles the C/C++ standard better 3) it's easy to set up 4) free It can be downloaded at http://www.borland.com/products/down..._cbuilder.html. Using Miracle C to learn coding is like learning to be a car mechanic using a 1950 DeSoto. __________________
Age is unimportant -- except in cheese |
|
#7
|
||||
|
||||
|
And please, please use code tags!!! I've edited your posts many times. It's about time you got the point!
Thanks. __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#8
|
|||
|
|||
[c] Leap Year FormulaHi Everyone,
With lots of help from Dave and my instructor the formula was completed and posted below. This was a just for fun assignment in the class. I had much stress and no fun. Yet this to shall pass. I do appreciate the suggestion about a different and more up to date compiler. I will pass the suggestion along to the school as they not the instructor has the decision making power on this topic. Thanks again for all of the support. Next topic will be a currency conversion formula. Oh joy. 8-) CPP / C++ / C Code:
|
Recent GIDBlog
Observations of Iraq by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| help with displaying year, month, and date. | dazzer143 | C++ Forum | 3 | 18-May-2005 15:46 |
| Trying to create a calendar | eleet | C Programming Language | 4 | 17-Mar-2005 22:34 |
| Problem with int mixed with char,... | leitz | C++ Forum | 17 | 07-Dec-2004 20:56 |
| need some help formating a display | dilmv | C++ Forum | 6 | 10-Nov-2004 12:09 |
| leap year | lena | C++ Forum | 1 | 06-Jan-2004 23:27 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The