GIDForums  

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

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-May-2004, 18:59
homz homz is offline
New Member
 
Join Date: May 2004
Location: ohio
Posts: 23
homz is on a distinguished road
Question

c++ help


Hello everyone, as you can tell i am new to this and need help with my C++ coding. If any one can help me it would be greatly appreciated. The follwoing is the question to the problem:

Write a program that prints the day number of the the year, given the date in the form month-day-year. For example if the input is 1-1-02, the day number is 1; if the input is 12-25-02, the day number is 359. The program should check for a leap year.

I have done the following but i am sure there is more efficent way of doing it. Thanks

CPP / C++ / C Code:
#include<iostream>

usingnamespace std;

void noOfDays(int, int, int);

int main()

{
int day, month, year;

cout<<"Please enter month, day and year: "<<endl<<endl;
cin>>month>>day>>year;

noOfDays(day, month, year);

system ("pause");

return 0;
}
void noOfDays(int d, int m, int y)
{
int numDays;

if ((y % 4 == 0) || ( y % 400 == 0))
{
cout<< "The year " << y <<" is a leap year. "<<endl<<endl;

switch (m)
{
case 1:numDays = d;
break;
case 2:numDays = 31 + d;
break;
case 3:numDays = 31 + 29 + d;
break; 
case 4:numDays = 31 + 29 +31 + d;
break;
case 5:numDays = 31 + 29 + 31 + 30 + d;
break;
case 6:numDays =31 + 29 + 31 + 30 + 31 + d;
break;
case 7:numDays = 31 + 29 + 31 + 30 + 31 + 30 + d;
break;
case 8:numDays = 31 + 29 + 31 + 30 + 31 + 30 + 31 + d;
break;
case 9:numDays = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + d;
break;
case 10:numDays = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + d;
break;
case 11:numDays = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + d;
break;
case 12:
numDays = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + d;
}

}

else

{
cout<< "The year " << y <<" is not a leap year. "<<endl<< endl;

switch (m)
{
case 1:numDays = d;
break;
case 2:numDays = 31 + d;
break;
case 3:numDays = 31 + 28 + d;
break;
case 4:numDays = 31 + 28 +31 + d;
break; 
case 5:numDays = 31 + 28 + 31 + 30 + d;
break;
case 6:numDays =31 + 28 + 31 + 30 + 31 + d;
break;
case 7:numDays = 31 + 28 + 31 + 30 + 31 + 30 + d;
break;
case 8:numDays = 31 + 28 + 31 + 30 + 31 + 30 + 31 + d;
break; 
case 9:numDays = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + d;
break;
case 10:numDays = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + d;
break; 
case 11:numDays = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + d;
break; 
case 12:
numDays = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + d;
}
}

cout<<"Number of days: " << numDays << endl;

}
Last edited by dsmith : 12-May-2004 at 21:36. Reason: Please use [c] & [/c] to highlight C code
  #2  
Old 12-May-2004, 19:36
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
Woah! Before you make any more posts, check out this page! By the way, welcome to the forum, and kudos for wanting to be a more efficient programmer. Since most of the months are different, I can't think of anything that would solve your problem any better right now except just doing all the addition in your head instead of with the computer processor. Add all the numbers to a sum... like, instead of saying...

CPP / C++ / C Code:
case 11:numDays = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + d;
You could say...

CPP / C++ / C Code:
case 11: numdays = 304 + d;
__________________
-Aaron
  #3  
Old 12-May-2004, 19:53
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
hi homz, welcome to c++!
first, i'd like to point out in your if statement you have two modular divisions one by 4 and another by 400. i don't think you need the one by 400. if any number is wholly divisible by 400, it will also be wholly divisible by 4.

second you might want to store the sums of the number of days in an array.
if you haven't learned how to use arrays yet, then i think your program is as efficient as it could be if somebody else or myself can think of a few ways to make it better we'll post soon!

good luck in your journey towards learning this great programming language!
  #4  
Old 12-May-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
I still remembers this Q was asked before, I think the solution is a better one.
Take alook at this thread:

Help with array and function input

Hope it would help you find a better solution
__________________
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
  #5  
Old 12-May-2004, 21:02
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
homz, i've found a way to just eliminate the second switch statement, i'm posting the code.

CPP / C++ / C Code:
#include<iostream>

using namespace std;

void noOfDays(int, int, int);

int main()

{
int day, month, year;

cout<<"Please enter month, day and year: "<<endl<<endl;
cin>>month>>day>>year;

noOfDays(day, month, year);

//system ("pause");

return 0;
}

void noOfDays(int d, int m, int y)
{
int numDays = 0;
bool leap;

if (y % 4 == 0)
{
	cout<< "The year " << y <<" is a leap year. "<<endl<<endl;
	numDays = 366;
	leap = true;
}
else
{
	cout<< "The year " << y <<" is not a leap year. "<<endl<< endl;
	numDays = 365;
	leap = false;
}


switch (m)
{
case 12:numDays -= (31-d);
	break;
case 11:numDays -= (61-d);
	break;
case 10:numDays -= (92-d);
	break;
case 9:numDays -= (122-d);
	break;
case 8:numDays -= (153-d);
	break;
case 7:numDays -= (184-d);
	break;
case 6:numDays -= (214-d);
	break;
case 5:numDays -= (245-d);
	break;
case 4:numDays -= (275-d);
	break;
case 3:numDays -= (306-d);
	break;
case 2:
	{
		if(leap)
			numDays -= (335-d);
		else
			numDays -= (334-d);
	}
	break;
case 1:
	{
		if(leap)
			numDays -= (366-d);
		else
			numDays -= (365-d);
	}
	break;
default: cout<<"no such month"<<endl;
}

cout<<numDays;
}


notice that i started from bottom up, so that month 2 ends up closer toward bottom and i only have to type those if else statments in 2 cases, otherwise i'da have to put them in 11 cases.
  #6  
Old 12-May-2004, 21:50
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
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
What about:
CPP / C++ / C Code:
#include<iostream>
using namespace std;

void noOfDays(int, int, int);

int main()
{
    int days, day, month, year;

    cout<<"Please enter month, day and year: "<<endl<<endl;
    cin>>month>>day>>year;

    days = noOfDays(day, month, year);
    cout << days;
    return 0;
}

void noOfDays(int d, int m, int y)
{
    monval[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int numDays = 0;
    int i;

    for (i = 0; i < m-1; i++)
    {
        numDays += monval[i];
    }

    if (y % 4 == 0)
    {
        numDays++;
    }
    return numDays;
}
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #7  
Old 12-May-2004, 22:51
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
yes ofcourse using an array would be the best way, but i don't think he's covered arrays yet. also i think you forgot to account for variable d in your code and thats why it's not working correctly. also the last if statement numdays++ wouldnt work for days ranging from 1/1 thru 2/28 since the extra day doesnt apply to dates before feb 29.
Also you are returning from a void function. also you haven't specified the array type.
did waltp actually post this code?


also i fixed the switch statement a little!

CPP / C++ / C Code:
switch (m)
{
case 12:numDays -= (31-d);
	break;
case 11:numDays -= (61-d);
	break;
case 10:numDays -= (92-d);
	break;
case 9:numDays -= (122-d);
	break;
case 8:numDays -= (153-d);
	break;
case 7:numDays -= (184-d);
	break;
case 6:numDays -= (214-d);
	break;
case 5:numDays -= (245-d);
	break;
case 4:numDays -= (275-d);
	break;
case 3:numDays -= (306-d);
	break;
case 2:
	{
		if(leap)
			numDays -= (335-d);
		else
			numDays -= (334-d);
	}
	break;
case 1:
			numDays -= (numDays-d);
	break;
default: cout<<"no such month"<<endl;
}

turns out that only case 2 needs that if else statment! also my mistake on previous post. when i said starting bottom up, i meant starting from 365 or 366 and working your way down to 1, not the case statements in switch. you could post the case statments starting from 1 down to 12, but it seemed more appealing to me to do them upside down.
__________________
spasms!!!
  #8  
Old 12-May-2004, 23:32
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
Looks like Walt posted it, but I'm guessing he was really tired!
__________________
-Aaron
  #9  
Old 12-May-2004, 23:35
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
Or maybe he is drunk!
__________________
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 12-May-2004, 23:55
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
I dunno... drunks have a tendency to be lazy and talkative... he wrote code and said nothing! :-)
__________________
-Aaron
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? 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 01:46.


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