![]() |
|
#1
|
|||
|
|||
HELP!! New to C!I know nothing about C and I have to write a program that determines the day number (1 to 366) in a year for a date that is provided as data input in the form yyyy-mm-dd including leap years. A year is a leap year if it is divisible by four, except that any year divisible by 100 is a leap year only if it is divisible by 400. The program has to include a function leap that returns 1 if its a leap year and 0 if not. How do I do it?!?! Need as much info as possible! Dont even know where to start with writing a code. It has
|
|
#2
|
|||
|
|||
Re: HELP!! New to C!Quote:
My sympathies. Here's a suggested plan of attack: 1. Take an introductory course with no programming prerequisites. 2. Obtain whatever book or other course materials are required. 3. Obtain and install whatever software development environment is suggested for the course. If any sample or example programs are supplied, make sure that you can compile and execute known good programs. (See footnote.) 4. Go to class (or attend on-line sessions, if appropriate). Use any examples supplied by the instructor as part of the course materials to make sure you can compile and execute on your environment. 5. Undertake course assignments in the sequence in which they are assigned. If you have problems, then post whatever you have tried, and ask specific questions about what you don't understand. (If you have an instructor with office hours or on-line availability, you might try that resource first.) Regards, Dave Footnote: If there are no sample/example programs supplied with your development system, try the following C program as a test. Create a file (with a text editor named "hello.c" and paste the following into it. Follow whatever instructions that are appropriate for compiling with your development environment (some systems involve creating a "project"). Compile and execute. CPP / C++ / C Code:
Last edited by davekw7x : 21-Mar-2006 at 13:02.
|
|
#3
|
|||
|
|||
Re: HELP!! New to C!Quote:
You may want to take a look at: http://www.gidforums.com/t-8961.html ...it isn't "right," but it may get you started and pointed in the general direction that you seek. :davis: |
|
#4
|
|||
|
|||
Re: HELP!! New to C!This is what I have, it's not working. Can anyone tell me what's wrong with it. The errors that come up are 'x' undeclared identifier and redefinition of formal parameter x. Sorry, i dotn know how to put them on as an attachment or whatever. HELP! Thanks.
/* Compute day number in a year for a date that is input in the form yyyy-mm-dd */ #include <stdio.h> /*Function prototypes*/ void instructions (void); leap (int year, int num); mon_cnt (int month, int count); int main(void) { /*Display user instructions*/ void instructions (void); /*Determine if year is a leap year*/ int leap (int year, int num); /*Count the day number of the year*/ int mon_cnt (int month, int count); /*Return day number*/ printf("The day number is %d" , &x); return 0; } /* Display user instructions */ void instructions (void) { int day; int month; int year; printf("Please enter the date in the form yyyy-mm-dd>"); scanf("%d-%d-%d", &year, &month, &day); } /* Determines if a year is a leap year and returns a value of 1 or 0 */ int leap (int year, int num) { if((year % 4) == 0) num = 1; else num = 0; if((year % 100 == 0) && ((year % 400)) != 0) num = 1; return (num); } /* Determine day count */ int mon_cnt (int month, int count) { if (month == 1) count= 0; else if (month == 2) count = 31; else if (month == 3) count = 59; else if (month == 4) count = 90; else if (month == 5) count = 120; else if (month == 6) count = 151; else if (month == 7) count =181; else if (month == count = 212; else if (month ==9) count = 243; else if (month == 10) count = 273; else if (month == 11) count = 304; else if (month == 12) count = 334; return (count); } /*Determine day count of the year*/ int day_count (int count, int num, int day, int x) { int x; x = count + num + day; return (x); } |
|
#5
|
|||
|
|||
Re: HELP!! New to C!Quote:
1. The variable is declared outside of any function and the declaration occurs before the function in the file. This is called a "global variable". I mention this because global variables are part of the language, not because I think you should use them here. It is generally considered that the use of global variables should be reserved for cases where you really need a global variable. There are many reasons to avoid the use of global variables in general usage. 2. The variable is used in a block of code inside the function and is declared in that same block. In simplest terms, a block of code is one or more instructions enclosed in braces {}. This kind of variable is usually called a "local" variable, and its identity is known only within that block. 3. The variable name is a parameter of the function. So in your main() function: CPP / C++ / C Code:
Quote:
Now, in your day_count() function: CPP / C++ / C Code:
The variable x is a parameter for the function. This means that the calling program gives an argument list that consists four integers. The function creates four local variables, named count, num, day, and x. The values of arguments from the calling program are assigned to these variables. The function can do anything it wants to (anything legal, that is) with these variables, but nothing that happens to these local copies is reflected back into the calling program. So, here's the problem that the compiler is flagging: Since x is in the parameter list, the function will be using its local copy of the variable from the calling function's argument list. You cannot declare a variable with the same name as a function parameter within the main block of the function. Hint: drop x from the function definition. Note that the compiler needs to know something about the function before you use it in the program, so you should always declare or define the function before its use: CPP / C++ / C Code:
Now the program should compile and you can start the fun task of debugging. Your program never calls the function day_count(), so you will have to put that somewhere. Anoter point: Your function instructions() reads the values of three things into its local variables, but the main() program and the other functions won't know what their values are, so you will have to work that out also. Anyhow, once you get the program to compile, you can put print instructions at strategic places to see exactly what the program is working on and where it is going. Regards, Dave |
Recent GIDBlog
Meeting the local Iraqis by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The