GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Mar-2006, 11:09
diveboy866 diveboy866 is offline
New Member
 
Join Date: Mar 2006
Posts: 2
diveboy866 is on a distinguished road

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  
Old 21-Mar-2006, 12:15
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: HELP!! New to C!


Quote:
Originally Posted by diveboy866
I know nothing about C
...
I have to write a program

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:
#include <stdio.h>

int main()
{
  printf("hello, world\n");
  return 0;
}
Last edited by davekw7x : 21-Mar-2006 at 13:02.
  #3  
Old 21-Mar-2006, 13:08
davis
 
Posts: n/a

Re: HELP!! New to C!


Quote:
Originally Posted by diveboy866
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

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  
Old 22-Mar-2006, 18:28
diveboy866 diveboy866 is offline
New Member
 
Join Date: Mar 2006
Posts: 2
diveboy866 is on a distinguished road

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  
Old 23-Mar-2006, 08:41
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: HELP!! New to C!


Quote:
Originally Posted by diveboy866
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
For a program that is contained within a single file (as this one is), there are several ways that a variable can be known inside a function. (Note that main() is a function):


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:
int main(void)
{

  void instructions (void);

  int leap (int year, int num);
  int mon_cnt (int month, int count);
  printf("The day number is %d" , &x);
  return 0;
}
The variable x is unknown to the compiler, and that is what it is telling you.

Quote:
Originally Posted by diveboy866
and redefinition of formal parameter x.

Now, in your day_count() function:

CPP / C++ / C Code:
int day_count (int count, int num, int day, int x)
{
  int x;
  x = count + num + day;
  return (x);
}

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:
void instructions(void);
int leap(int year, int num);
int mon_cnt(int month, int count);
int day_count (int count, int num, int day);

int
main(void)
{
.
.

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 GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:53.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.