Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 03-27-2005, 06:59 PM   #1 (permalink)
cleverest
Registered User
 
Join Date: Mar 2005
Posts: 22
cleverest is on a distinguished road
Exclamation Urgently need help, Functions and leap year problem

IF ANYONE CAN HELP, I'D APPRECIATE IT!

How do I create a program that allows a user to put in two INT values such as:

Code:
cin >> int1 >> int2;
and then outputs ONLY the values between the two numbers they input that meet a certain restriction? For instance I'm trying to make a program where a person puts in two date ranges and it will diplay the LEAP YEARS within that range

Code:
example:   cout << "the leap years were:" << int1 << through int2;
I cannot use pointers/arrays...I'm pretty much limited to loops, IF statements, switches, functions etc....

So far I've managed to create a program that shows if the values placed are LEAP YEARS (but it may not be perfect....), but the main issue is that it does NOT scan a RANGE of years at all.....I have to use at least 1 function and I'm using 2......

IF ANYONE CAN HELP, I'D APPRECIATE IT!

Code:
#include <iostream>

using namespace std;

int leapyears(int year1);
int leapyears2(int year2);


int main()
{

	int year1;
	int year2;

	cout << "Enter a range of years to consider:";
	cin >> year1 >> year2; 
	if ((year1 & year2) % 400 || 100 == 0)  
		leapyears(year1); leapyears2(year2);



	
}


int leapyears(int year1)  //function ONE
{
    switch (year1 % 4)
    {
    case 0:
        if (year1 % 100 == 0)
        {
            cout;
            if (year1 % 400 == 0)
            {
				cout << "The Leap years were: " << year1;
            }
            else
                cout;
        }
        else
            cout << "The Leap years were: " << year1; break;

    //case 3:
       // cout << "Next year is a leap year.  "; // COMMENTED OUT FOR NOW
    default:
        return 0; break;
		
    }
	return year1;

	
} 


int leapyears2(int year2)  //function TWO
{
 
    switch (year2 % 4)
	   {
    case 0:
        if (year2 % 100 == 0)
        {
            cout;
			if (year2 % 400 == 0)
            {
                cout << " " << year2 <<"\n";
            }
            else
                cout;
        }
        else
            cout << " " << year2 <<"\n"; break;

    //case 3:
        //cout << "Next year is a leap year.  "; // COMMENTED OUT FOR NOW
    default:
        return 0; break;
		
    }
	return year2;

}

Last edited by cleverest; 03-27-2005 at 07:04 PM. Reason: spelling and such
cleverest is offline   Reply With Quote
Old 03-28-2005, 02:35 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Code:
#include <iostream>

using namespace std;

//--
const unsigned REFORM_YEAR = 1582;
bool is_leap(const int);
void print_leap_excl_range(int, int);
//--

int main()
{
  int start, end;
  //Boht are leap years, but wont be printed.
  start = 800;
  end = 2008; 
  
  //Printing *closed* range. So (excl)usive start/end.
  print_leap_excl_range(start, end);
  
  system("pause");
  return 0;
}

//------------------------------------------------

bool is_leap(const int yr)
{
  if( yr % 4 ) return false;
  if( yr < REFORM_YEAR ) return true;
  if( yr % 100 ) return true;
  if( yr % 400 ) return false;
  return true;
}

//------------------------------------------------

void print_leap_excl_range(int start, int end)
{
  //Test for "between". So do *not* include passed end year.
  --end;
  do
  {
    //Test for "between". So do *not* include passed start year.
    ++start;    
    if( is_leap(start) )
    {
      cout<<"Leap Year Found: "<<start<<endl;
    }  
  } while(start < end); 
}
You could do it this way too.
QUESTIONS:
1 ) Is it faster or slower or doesn't it matter?
2) Is it better for some reason?
3) Why?
hint: read my signature below.

Code:
void print_leap_excl_range(int start, int end)
{
  //Test for "between". So do *not* include passed end year.
  --end;
  while(++start < end)
  {
    if( is_leap(start) )
    {
      cout<<"Leap Year Found: "<<start<<endl;
    }
  }  
}
__________________

Last edited by Valmont; 03-28-2005 at 03:09 AM.
Valmont is offline   Reply With Quote
Old 03-28-2005, 02:48 AM   #3 (permalink)
cleverest
Registered User
 
Join Date: Mar 2005
Posts: 22
cleverest is on a distinguished road
Thanks for the help, I think I can update my code accordingly. Appreciate it!
cleverest is offline   Reply With Quote
Old 03-28-2005, 03:09 AM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
I'd check my code again. I corrected a lil error.
__________________
Valmont is offline   Reply With Quote
Old 03-28-2005, 03:16 AM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
And better change to:
Code:
if( is_leap(start) )
    {
      cout<<start<<" ";
    }
So more leap years will be visible in console.
__________________
Valmont is offline   Reply With Quote
Old 03-28-2005, 09:55 AM   #6 (permalink)
cleverest
Registered User
 
Join Date: Mar 2005
Posts: 22
cleverest is on a distinguished road
That's funny....I actually noticed the error when I compiled it and tested....It took me awhile to figure out what to do.... but I did those changes before you recommended them! lol

Thanks for letting me know regardless!
cleverest is offline   Reply With Quote
Old 03-28-2005, 11:54 AM   #7 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
I gave you two functions: a "while" version and a "do" version. Try to answer the questions I gave you. Better start early with this.
__________________
Valmont is offline   Reply With Quote
Old 03-28-2005, 04:53 PM   #8 (permalink)
cleverest
Registered User
 
Join Date: Mar 2005
Posts: 22
cleverest is on a distinguished road
Hmmm I'm at work so I can't play around with it yet, but I'll check it out tonight and answer your questions...I appreciate the further lessons....

:-D
cleverest is offline   Reply With Quote
Old 03-28-2005, 10:43 PM   #9 (permalink)
cleverest
Registered User
 
Join Date: Mar 2005
Posts: 22
cleverest is on a distinguished road
Wow I can't see any difference between the two loops when I run the program or try different values....

QUESTIONS:
1 ) Is it faster or slower or doesn't it matter? (don't see a difference with this code)
2) Is it better for some reason? (It appears to be the same....)
3) Why? (please tell me....I'm intrigued)

Please tell me what I'm obviously missing with your questions, sorry I'm a C++ Dumb-ass....thanks again!
cleverest is offline   Reply With Quote
Old 03-28-2005, 10:50 PM   #10 (permalink)
cleverest
Registered User
 
Join Date: Mar 2005
Posts: 22
cleverest is on a distinguished road
Also how can I get the program to also display the years I type in IF they are leap years too?? Such as 1996 2000

(both of these should display for example....)

Sorry....This will sink in eventually...
cleverest is offline   Reply With Quote
Old 03-29-2005, 03:53 AM   #11 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Quote:
Originally Posted by cleverest
Also how can I get the program to also display the years I type in IF they are leap years too?? Such as 1996 2000

(both of these should display for example....)
Do you mean 1996-2000 is a range and you want to INCLUDE the outer ranges, instead of excluding them?

For the questions.
Performance wise it doesn't matter at all.
The only difference between them is intent:
The "do" loop will execute the body at least once.
But the "while" loop does not necessary execute the body. It might skip it the first time.
So the precondition for these two loops are slightly different.

Right now, it doesn't matter much.
__________________
Valmont is offline   Reply With Quote
Old 03-29-2005, 10:52 AM   #12 (permalink)
cleverest
Registered User
 
Join Date: Mar 2005
Posts: 22
cleverest is on a distinguished road
Yes I would like to include the inputed values as well (not exlude them) is this an easy fix to the existing code? Thanks again.
cleverest is offline   Reply With Quote
Old 03-29-2005, 12:12 PM   #13 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
it is an easy fix. Try it.
__________________
Valmont is offline   Reply With Quote
Old 03-29-2005, 02:06 PM   #14 (permalink)
cleverest
Registered User
 
Join Date: Mar 2005
Posts: 22
cleverest is on a distinguished road
hmmm I was able to get the final inputted date to display by removing the

--end;

in the while (or do while) loop....

however I can't see to get the first inputed date to display....I'm at work right now so I can't do much, I'll play around with it more tonight....but I gotta start working on another assignment soon....this class is killing me....I should quit my job, lol
cleverest is offline   Reply With Quote
Old 03-29-2005, 02:11 PM   #15 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Hint:
In this code:
Code:
//Test for "between". So do *not* include passed start year.
++start;
1) Remove the comment.
2) Do something with "++start".
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 06:49 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting