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 Rating: Thread Rating: 3 votes, 5.00 average.
  #1  
Old 23-Jul-2005, 13:19
redhead049 redhead049 is offline
New Member
 
Join Date: Jul 2005
Posts: 4
redhead049 is on a distinguished road

Leap Year Validation -code formula request


Arial
10
navy

Hi,
I a have 3 days experience with C and need help please in programming a leap year validation formula. I created a flowchart and attempted psuedo code but got stuck on the c formula. Any suggestions are appreciated.

Flowchart is attached.

Pseudocode attempt:
intYear
Get year = assign to variable
Does the year contain "00" at the end?
If intYear instr ('"00")
a=intYear\400=yes
Else
a=intYear\4
End If
If a=yesThen
Output ="Yes this is a leap year"
Else
Ouput="No this is not a leap year"
End if
Attached Files
File Type: zip leapyearflowchart.zip (42.7 KB, 293 views)
  #2  
Old 23-Jul-2005, 14:34
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,702
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
Quote:
Originally Posted by redhead049
Hi,
I a have 3 days experience with C

Welcome to the wonderful world of C.

I like your flow chart better than your pseudo-code. I guess the idea of pseudo-code is to transform the 2-dimensional representation of the problem (flow chart) to a 1-dimensional procedural language representation.

First of all, I will summarize what I think I would use to solve the problem in C, using the "modulus" operator, %:

CPP / C++ / C Code:
/* 
 * To determine mathemetically whether a year is a leap year, we need
 * to perform the following numerical calculations
 *
 * Is the year divisible by 100?
 *   The year is divisible by 100 if and only if (year % 100) == 0
 *
 * Is the year divisible by 400?
 *   The year is divisible by 400 if and only if (year % 400) == 0
 *
 * Is the year divisible by 4?
 *   The year is divisible by 4 if and only if (year % 4) == 0
 *
 */

A side note: why did I put the /* */ around the above description? Well, they are C language comments, and can be included directly in the program source. Three very important characteristics of good code are: documentation, documentation, and documentation. (Of course having code that actually works is pretty important, too.) Your flow chart is great documentation, but it can get separated from the program, so it's nice to have something in the source itself that reminds you what the heck you were trying to do. (And serves to tell others what you had in mind, in case someone else looks at it.)

The key, to me at least, is that the diamond-shaped boxes of the flow chart can all be replaced by conditional procedural statements. One possibility is using C language if statements. Here's the pseudo code that I might have deduced from your flow chart:

CPP / C++ / C Code:
/*
 *  if the year is divisible by 100, then
 *    if the year is divisible by 400 then
 *      it is a leap year
 *    else
 *      it is not a leap year
 *    end if
 *  else
 *    if the year is divisible by 4 then
 *      it is a leap year
 *    else
 *      it is not a leap year
 *    end if
 *  end if
 */

Now, this is pseudo-code, not C language, but it's pretty close Just think of "then" as an opening brace: {. And "end if" is like a closing brace: }.


You could define a function, say, "is_leap_year". And it could be organized something like the following:



CPP / C++ / C Code:
/* 
 * perform calculations on the integer "year" and
 * return a value of 1 if it is a leap year.
 * return a value of 0 if if it not a leap year:
 *
 */
int is_leap_year(int year)
{
  int return_value;

   if (/*   */) {
     if (/* */) {
       return_value = ?;
     }
     else {
       return_value = ?;
     }
   }
   else if (/* */){
     return_value = ?;
   }
   else {
     return_value = ?;
   }

   return return_value;
}

Now, you can make this code part of your main program, where you give it a value for year and let it print the result. Or you can make a function that is called from your main program and returns a value to the main program. The main program then prints out a message, depending on the value from the function.


Regards,

Dave
  #3  
Old 23-Jul-2005, 19:02
redhead049 redhead049 is offline
New Member
 
Join Date: Jul 2005
Posts: 4
redhead049 is on a distinguished road
Hi Dave,

Thanks for the guidance and encouragement, I couldn't figure out how the computer would read the last 2 digits and now I understand to /by 100. Yes I knew the pseudocode was messed up, thanks on that assist.

I have attempted to program in C and here are my new challenges.

1) How to program in that the user must enter the year and then get program to read it

2) Get program to stay open when the user keys the year. We have not got that far in class yet. My instructor told us to use getch() to keep it open yet when I press any key it closes. Oh yes and I read a post on this site that said getchar() was better -not sure why but that won't hold it open with input either

3) I am getting the following error message with my code which I also pasted below
Miracle C Compiler (r3.2), written by bts.
Compiling C:\Program Files\Miracle C\LeapYearV3.c
main

C:\Program Files\Miracle C\LeapYearV3.c: line 14: Parse Error, expecting `SEP'
'int is_leap_year(int year)'
aborting compile


My attempt at code:

CPP / C++ / C Code:
//Leap Year Determinator

#include <stdio.h>
#include <system.h>
main ()

/* 
 * perform calculations on the integer "year" and
 * return a value of 1 if it is a leap year.
 * return a value of 0 if if it not a leap year:
 *
 */
{
int is_leap_year(int year);

  int return_value;
  }

/*Prompt for year */
  printf("Enter Calandar Year");  

  /* Read in year */
  scanf("%i",year);

   if (int/100==0) }
     if (int/400==0) {
       return_value = 1;
     }
     else {
       return_value = 0;
     }
   }
   else if (int/4==0){
     return_value = 1;
   }
   else {
     return_value = 2;
   }

   return return_value;
}
Last edited by LuciWiz : 25-Jul-2005 at 00:27. Reason: Please insert your C code between [c] & [/c] tags
  #4  
Old 24-Jul-2005, 08:30
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,702
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
Quote:
Originally Posted by redhead049
Hi Dave,


I have attempted to program in C and here are my new challenges.

1) How to program in that the user must enter the year and then get program to read it

2) Get program to stay open when the user keys the year. We have not got that far in class yet. My instructor told us to use getch() to keep it open yet when I press any key it closes. Oh yes and I read a post on this site that said getchar() was better -not sure why but that won't hold it open with input either

3) I am getting the following error message with my code which I also pasted below
Miracle C Compiler (r3.2), written by bts.
Compiling C:\Program Files\Miracle C\LeapYearV3.c
main

C:\Program Files\Miracle C\LeapYearV3.c: line 14: Parse Error, expecting `SEP'
'int is_leap_year(int year)'
aborting compile

As far as Miracle C: the compiler aborted because something is structurally wrong with your program. Before writing this program, practice with simpler things (like "Hello world"). Read examples in your book or from class. Reproduce them exactly and compile and execute them. Make sure you know how to put things together. Then you can try new stuff.

First things first: Have you ever compiled any kind of program? Don't they always show an example that prints out "Hello world" or something? If not, then: find the simplest example from class or from the book, compile and execute it.


Then write a program to get user input, and do nothing else (except, maybe, print out the value)

Then add the code to do the calculations on user input.


If you have had functions, then the calculations are appropriately put into a function. You don't have to do this, and you don't always to this even if you know how. It's really a matter of style.

If your instructor tells you to use scanf, then you must know how to use scanf to get user input. Aren't there examples from class or your book?

Also, if the instructor tells you to use getch, then you must use getch. This is not a standard C library function, but for compilers that do support it, its prototype is (usually) found in <conio.h> As far as Miracle C, I can't help you, but here is a program that you can try for starters:

CPP / C++ / C Code:
#include <stdio.h>
#include <conio.h>

int main()
{
  int year;

  printf("Enter the year: ");
  scanf("%d", &year);
  printf("You entered %d\n", year);

  printf("Press any key: ");
  getch();
  return 0;
}

Note that for scanf, to get the user's input into a variable, you must give scanf the address of the variable. That's why the second argument is &year, not year.

It is always good practice to show what the program is going to use for its data, so I put the printf statement after the scanf to show what it really read. There are more sophisticated and more robust ways to handle user input, but these few lines will get you started.

As far as the caculations:

If the value of the integer year is 1234, then the value of year/100 is 1200. Integer division throws away any remainder. In order to test whether the year is divisible by 100, one way is to use the C operator '%', as I suggested in the previous post. The value of year%100 is 34 (That's the remainder from integer division.) For positive integers, the modulus operator, '%', gives the remainder of integer division.

So to test whether the year is divisible by 100, you could do something like this:
CPP / C++ / C Code:
..
  if ((year % 100) == 0) {
    /* statements here to handle the case of year is divisible by 100 */
  }
  else {
    /* statements here to handle the case of year not divisible by 100 */
  }

As far as program organization: Unless the instructor specifically told you to use a function for the calculation, then you could just do something like the following:

CPP / C++ / C Code:
/* put the headers here: <stdio.h>, <conio.h> */

int main()
{
  /* put the stuff here to get user input */

  /* put the stuff here to implement your flow chart/pseudo code */

  return 0;
}

If he told you to use a function, then it could be something like this:

CPP / C++ / C Code:
/* put the headers here: <stdio.h>, <conio.h> */

int main()
{
  int is_leap_year(int y); /* this declares the function for the compiler */
  /* put the stuff here to get user input */

  /* put the stuff here to call the function and set a variable's value
   * to the return value of the function
   */
  
  /* put stuff here to report the result of the function */

  return 0;
}

/* the following is the function definition (implements the caculations) */

int is_leap_year(int year)
{
  /* put the stuff here to implement the flow chart/pseudo-code*/
}
There are other ways, and if the instructor told you how to do it some other way, then you must do it his way. Use examples from class or your book to see how to put things together.

See the theme here:
1. Do simple stuff, using examples from class or your book, before you jump in and start writing code.

2. Create your program a step at a time: First make sure that you can handle user input. Get that part of the program executing exactly the way that you want it to. Then, and only then, start in on some new stuff. For example:

CPP / C++ / C Code:
#include <stdio.h>
#include <conio.h>

int main()
{
  int year;

  printf("Enter the year: ");
  scanf("%d", &year);
  printf("You entered %d\n", year);
  if ((year % 100) == 0) {
    printf("The year is divisible by 100.\n");
  }
  else {
    printf("The year is not divisible by 100.\n");
  }
  printf("Press any key: ");
  getch();
  return 0;
}
After you make sure that this part works, then instead of just having printf statements inside the if{}else{} braces, put in some code (a little at a time) to perform the other logic.


Regards,

Dave
  #5  
Old 24-Jul-2005, 12:46
redhead049 redhead049 is offline
New Member
 
Join Date: Jul 2005
Posts: 4
redhead049 is on a distinguished road
Talking

Thank you


Hi Dave,
Wow thanks for the detailed explanation. Yes I did the standard Hello World and a few other basic practices. Your information is the next step that I needed and I appreciate the time you took to give me the information. I will be reviewing it and following your suggestions this upcoming week.

Thanks again.
  #6  
Old 24-Jul-2005, 20:37
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
You might want to suggest to your instructor that Borland 5.5 C/C++ compiler is
1) a 32 bit compiler
2) Newer and handles the C/C++ standard better
3) it's easy to set up
4) free

It can be downloaded at http://www.borland.com/products/down..._cbuilder.html.

Using Miracle C to learn coding is like learning to be a car mechanic using a 1950 DeSoto.
__________________

Age is unimportant -- except in cheese
  #7  
Old 25-Jul-2005, 00:26
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 893
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
And please, please use code tags!!! I've edited your posts many times. It's about time you got the point!
Thanks.
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #8  
Old 28-Jul-2005, 18:13
redhead049 redhead049 is offline
New Member
 
Join Date: Jul 2005
Posts: 4
redhead049 is on a distinguished road

[c] Leap Year Formula


Hi Everyone,
With lots of help from Dave and my instructor the formula was completed and posted below. This was a just for fun assignment in the class. I had much stress and no fun. Yet this to shall pass. I do appreciate the suggestion about a different and more up to date compiler. I will pass the suggestion along to the school as they not the instructor has the decision making power on this topic. Thanks again for all of the support. Next topic will be a currency conversion formula. Oh joy. 8-)

CPP / C++ / C Code:

//Year Validation Version 6	July 25 2005

#include <stdio.h>
#include <system.h>
#include <stdlib.h>

#define TRUE	1
#define FALSE	0

int main()
{
  int year;

  	printf("\nHANDY DANDY LEAP YEAR CALCULATOR\n ");
  	printf("Valid for any year after 1581:\n\n ");
  	printf("\nType the four digit year and press the enter key: ");
  	scanf("%d", &year);
  	printf("You entered %d\n\n", year);
  	if ((year % 100 == 0) && (year % 400 == 0))
    		printf("\t\tThe year IS A Century and a LEAP YEAR.\n\n");
   	else if ((year % 100 != 0) && (year % 4 == 0)) 
    		printf("\t\tThe year IS A LEAP YEAR but nbot a century.\n\n");
  	else
    		printf("\tThe year is NOT A LEAP YEAR\n\n");
 
  printf("\n\nPress any key to close the window");
  getch();//Press any keyboard key
  return 0;
}

 
 

Recent GIDBlogObservations of Iraq 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
help with displaying year, month, and date. dazzer143 C++ Forum 3 18-May-2005 15:46
Trying to create a calendar eleet C Programming Language 4 17-Mar-2005 22:34
Problem with int mixed with char,... leitz C++ Forum 17 07-Dec-2004 20:56
need some help formating a display dilmv C++ Forum 6 10-Nov-2004 12:09
leap year lena C++ Forum 1 06-Jan-2004 23:27

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

All times are GMT -6. The time now is 17:11.


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