conordb / Testing

0 stars 0 forks source link

Leap Year Code #1

Open conordb opened 7 years ago

conordb commented 7 years ago

// Program to test if year is a leap year // By Conor Briggs // Created 12 / 03 / 2017

include

include

include

define CAL_START 1582

define TRUE 1

define FALSE 0

int main (int argc, char* argv[]) {

int year;

int isLeapYear (int year);

printf("please enter a year after 1582:\n");
    scanf("%d",&year);
    assert (year >= CAL_START);
        if (year%4 == 0){
        if (year%100 == 0){
        if (year%400 == 0)
            printf("%d\n", TRUE);
                else
                printf("%d\n", FALSE);}
                    else
                    printf("%d\n",TRUE);}
                        else
                        printf("%d\n",FALSE);

return 0;

}

olgapopovic commented 7 years ago

My suggestions:

  1. return EXIT_SUCCESS; just for clarity and style guide (and because Richard says so haha)
  2. I'm not sure what's going on with your indentation, please stick to style guide if this was not intentional as it makes it easier to read
  3. Do your homework on time smh
  4. Take out the lineint isLeapYear (int year); and put it between your #defines and the line int main (int argc, char* argv[]) {
  5. Next, everything inside the main function between if (year%4 == 0){ and printf("%d\n",FALSE); needs to go in a separate function. So basically:

`int main (int argc, char *argv[]) { // some code here, including return EXIT_SUCCESS; }

int isLeapYear (int year) { //your leap year algorithm here }`

  1. Now, you're going to need to fix up your algorithm. In order to tell the main function (which will only be using your isLeapYear function) whether int year is a leap year or not, you need to return a value. That means you're going to need another variable inside int isLeapYear, for example you could make int leapYear and then, instead of printing true or false in your function, you do something like this:
if (year % 4 == 0) {
   if (year % 100 == 0) {
      if(year % 400 == 0) {
         leapYear = TRUE;
      } else {
         leapYear = FALSE;
      }
   } else {
      leapYear = TRUE;
   }
} else {
   leapYear = FALSE;
}

and then at the end of your function, you return leapYear; and so, when the main function finishes running isLeapYear, it has a value for true or false that you can use. How to do this inside your int main:

int isItALeapYear; //or whatever you choose to call it
isItALeapYear = isLeapYear (year); //here we put whatever value isLeapYear has returned using the variable year and copying it into isItALeapYear. This is called **calling** the function.
  1. Now the easy part. In your main function after that, you make some if statements which will make your program print "[year] is a leap year" if isItALeapYear is TRUE. (if false I'm sure you can figure out what to print)

  2. Try and compile then submit to OL once it works. If you have any more questions ask me and if I don't have time I'll go over it with you on Wednesday.

Good luck!