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 11-Apr-2004, 17:22
FearlessFife35 FearlessFife35 is offline
New Member
 
Join Date: Apr 2004
Posts: 3
FearlessFife35 is on a distinguished road
Arrow

Help with array and function input


I am having to write a program that determines the day number in a year for a date that is provided as input data. Like, if the user inputs January 1st, 2004...the program will output 1. I have some of it written and just can't seem to figure out the formula that computes the data...here is what I have written so far:

CPP / C++ / C Code:
#include <stdio.h>
void instructions(void);
int getdays(int, int, int);

int main(void)
{
	int month;
	int day;
	int year;

	instructions();
	printf("Please enter the month like this: for January..enter 1: ");
	scanf("%d", &month);
	printf("Please enter the day like this: for the fifth day..enter 5: ");
	scanf("%d", &day);
	printf("Please enter the year like this:2004....: ");
	scanf("%d", &year);

	printf("Here is the data you entered: %d/ %d/ %d\n", month, day, year);


	
	return 0;

}


void instructions(void)
{

	printf("This program will allow you to input any date such as, January 1, 2004.\n");
   printf("\n");
	printf("Once the date has been entered, the program will then tell you how\n");
	printf("many days have passed from the beginning of the calender year to\n");
	printf("the date you have entered.\n");
	printf("\n");
	printf("You will now be required to enter the month, day, and year using\
	        integers.\n");
	printf("\n\n");
}

int getdays(int m, d, y)
{
	int days[MONTHS] = {31,28,31,30,31,30,31,31,30,31,30,31};
	int x;
	int temp;
	int days = 0;

	for(x = 0; x < m; x++)
	{
	temp += MONTHS[x];
   	/*Here is where I'm having a problem figuring out how to do the formula*/
Last edited by dsmith : 12-Apr-2004 at 10:18. Reason: Please use [c] & [/c] to properly highlight code
  #2  
Old 11-Apr-2004, 18:18
BlueTeeth's Avatar
BlueTeeth BlueTeeth is offline
New Member
 
Join Date: Mar 2004
Posts: 9
BlueTeeth is on a distinguished road
CPP / C++ / C Code:
for(x = 0; x < m-1; x++)   //condition must be m-1 because days[0] stands for m=1
{
temp+=days[x];
}
temp+=d;
return temp;
//no need for variable days,and no need to pass year
  #3  
Old 11-Apr-2004, 22:48
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Although the colors are pretty, we would prefer your code be within code tags ([c] & [/c]) to preserve your formatting.

CPP / C++ / C Code:
int getdays(int m, d, y)    // improper syntax
{
    int days[MONTHS] = {31,28,31,30,31,30,31,31,30,31,30,31};
    int x;
    int temp;
    int days = 0;  // This is already defined above as an array
From this point, one way to do it is:

start temp at 0
set m as an offset from 0 instead of 1 because the index for Jan = 0
loop from 0 to m-1 and accumulate the days of each month in temp (you don't want the last month added yet)
When the loop is finished, add d
and voila you have the julian date for the year. I'll let you figure out how to do the leap year.
__________________

Age is unimportant -- except in cheese
  #4  
Old 15-Apr-2004, 14:45
FearlessFife35 FearlessFife35 is offline
New Member
 
Join Date: Apr 2004
Posts: 3
FearlessFife35 is on a distinguished road

Thanks...But a little more help?


Quote:
Originally Posted by WaltP
Although the colors are pretty, we would prefer your code be within code tags ([c] & [/c]) to preserve your formatting.

CPP / C++ / C Code:
int getdays(int m, d, y)    // improper syntax
{
    int days[MONTHS] = {31,28,31,30,31,30,31,31,30,31,30,31};
    int x;
    int temp;
    int days = 0;  // This is already defined above as an array
From this point, one way to do it is:

start temp at 0
set m as an offset from 0 instead of 1 because the index for Jan = 0
loop from 0 to m-1 and accumulate the days of each month in temp (you don't want the last month added yet)
When the loop is finished, add d
and voila you have the julian date for the year. I'll let you figure out how to do the leap year.


I have been doing some more work on this program, with your help....still not getting it to work. Could you take a look at it and give me some more advice? Thanks for any help!
CPP / C++ / C Code:
#include <stdio.h>
#define MONTHS 12
void instructions(void);
int getdays(int, int, int);
int LeapYear(int year);

int main(void)
{
	int month;
	int day;
	int year;
	int numdays;

  


	instructions();
	printf("Please enter the month like this: for January..enter 1: ");
	scanf("%d", &month);
	printf("Please enter the day like this: for the fifth day..enter 5: ");
	scanf("%d", &day);
	printf("Please enter the year like this:2004....: ");
	scanf("%d", &year);
	getdays(month,day,year);

	numdays = getdays(month, day, year);

  


	printf("The number of days passed since the date you've entered is: \n", numdays);


	
	return 0;

}


void instructions(void)
{

	printf("This program will allow you to input any date such as, January 1, 2004.\n");
	printf("\n");
	printf("Once the date has been entered, the program will then tell you how\n");
	printf("many days have passed from the beginning of the calender year to\n");
	printf("the date you have entered.\n");
	printf("\n");
	printf("You will now be required to enter the month, day, and year using\
	        integers.\n");
	printf("\n\n");
}

int getdays(int m,int d,int y)
{
	int days[MONTHS] = {31,28,31,30,31,30,31,31,30,31,30,31};
	int x;

	int dayCount = 0;

	for(x = 0; x < m; x++)
	{

		if(x==1)
			dayCount += days[x];
		{
		if(LeapYear(y) == 0)
			dayCount += days[x];
		else
			dayCount += 29;
    	else
			dayCount += d;
		}
	}
	return dayCount;
}

int LeapYear(int year)
{

	if((year % 400) == 0) || (((year % 4)  == 0) && ((year % 100) != 0));
	{
		return 0;
	else
		return 1;
	}
}
#include <stdio.h>
#define MONTHS 12
void instructions(void);
int getdays(int, int, int);
int LeapYear(int year);

int main(void)
{
	int month;
	int day;
	int year;
	int numdays;

  


	instructions();
	printf("Please enter the month like this: for January..enter 1: ");
	scanf("%d", &month);
	printf("Please enter the day like this: for the fifth day..enter 5: ");
	scanf("%d", &day);
	printf("Please enter the year like this:2004....: ");
	scanf("%d", &year);
	getdays(month,day,year);

	numdays = getdays(month, day, year);

  


	printf("The number of days passed since the date you've entered is: \n", numdays);


	
	return 0;

}


void instructions(void)
{

	printf("This program will allow you to input any date such as, January 1, 2004.\n");
	printf("\n");
	printf("Once the date has been entered, the program will then tell you how\n");
	printf("many days have passed from the beginning of the calender year to\n");
	printf("the date you have entered.\n");
	printf("\n");
	printf("You will now be required to enter the month, day, and year using\
	        integers.\n");
	printf("\n\n");
}

int getdays(int m,int d,int y)
{
	int days[MONTHS] = {31,28,31,30,31,30,31,31,30,31,30,31};
	int x;

	int dayCount = 0;

	for(x = 0; x < m; x++)
	{

		if(x==1)
			dayCount += days[x];
		{
		if(LeapYear(y) == 0)
			dayCount += days[x];
		else
			dayCount += 29;
    	else
			dayCount += d;
		}
	}
	return dayCount;
}

int LeapYear(int year)
{

	if((year % 400) == 0) || (((year % 4)  == 0) && ((year % 100) != 0));
	{
		return 0;
	else
		return 1;
	}
}
)
  #5  
Old 15-Apr-2004, 19:33
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
What is it you want to hear? Please ask a specific question, because I'd rather not look through all that code without knowing what the problem is first!
__________________
-Aaron
  #6  
Old 15-Apr-2004, 20:09
BlueTeeth's Avatar
BlueTeeth BlueTeeth is offline
New Member
 
Join Date: Mar 2004
Posts: 9
BlueTeeth is on a distinguished road
In the leap year the only difference is that Feb is 29 days so why don`t u check for leap year out of the loop and change the entry of feb. to 29 if it`s satisfied
and plz ask a specific question as said
  #7  
Old 15-Apr-2004, 20:17
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
You code looks soo long that i refused to look, but on second look, it actually is a same code posted twice. Anyway, this is what I saw in your code.

CPP / C++ / C Code:
int getdays(int m,int d,int y)
{
  int days[MONTHS] = {31,28,31,30,31,30,31,31,30,31,30,31};
  int x;

  int dayCount = 0;

  for(x = 0; x < m-1; x++)
  {
    if(x==1) //for second month only (FEB)
    {
        if(y%4 != 0) //not leap year
           dayCount += days[x];
        else
           dayCount += 29; // yep. its leap year
    }
    else
      dayCount += days[x]; //other months, don't care, just add
  }
  dayCount+=d; //add remainning day.
  return dayCount;
}

Why do you make your leap year code like that :
CPP / C++ / C Code:
if((year % 400) == 0) || (((year % 4)  == 0) && ((year % 100) != 0));
  {
    return 0;
  else
    return 1;
  }
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #8  
Old 15-Apr-2004, 23:18
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by Max Payne
Why do you make your leap year code like that :
CPP / C++ / C Code:
if((year % 400) == 0) || (((year % 4)  == 0) && ((year % 100) != 0));
  {
    return 0;
  else
    return 1;
  }
Because that's how you decide if Feb has an extra day or not. But you have to format the if correctly:
CPP / C++ / C Code:
if(((year % 400) == 0) || (((year % 4)  == 0) && ((year % 100) != 0)))
    return 1;
else
    return 0;
__________________

Age is unimportant -- except in cheese
  #9  
Old 16-Apr-2004, 00:15
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
Quote:
Originally Posted by WaltP
Because that's how you decide if Feb has an extra day or not. But you have to format the if correctly:

Yes I know that. Buts isn't enough just like this:
CPP / C++ / C Code:
if(year%4==0)
    return 1;
else
    return 0;
Just curious.
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #10  
Old 16-Apr-2004, 04:24
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
Quote:
Originally Posted by Max Payne
Yes I know that. Buts isn't enough just like this:
CPP / C++ / C Code:
if(year%4==0)
    return 1;
else
    return 0;
Just curious.

No, considering the 'hundred years', there's a leap year only every 400 years. Like there was leap year in 2000, but not in 1900.
GF
 
 

Recent GIDBlogHalfway done! 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 04:59
need help with passing 3 arrays into a function tommy69 C Programming Language 14 07-Apr-2004 00:22
Speed up C++ code about 3d array! Truong Son C++ Forum 0 16-Mar-2004 21:52
throwing an struct(with an array) through a function knakworstje C Programming Language 5 15-Feb-2004 16:20
deleting from an array ionyka C++ Forum 1 02-Sep-2003 03:09

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

All times are GMT -6. The time now is 05:33.


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