Question

In: Computer Science

C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is...

C++

A void function named NextLeapYear() that takes an int reference parameter. If the parameter is positive, the function will assign it the next leap year after it; otherwise, the function will assign 4 to it.

Solutions

Expert Solution

The C++ code sniffet of main.cpp is:

#include <iostream>

void NextLeapYear(int *year)
{
    int temp = *year; //to prevent any mid changes in variable.
    if(temp < 0) //negative entry check
    {
        *year = 4;
        return;
    }
    temp = ((temp/4)+1)*4; // this is most probable leap yr next to year provided.
    //Nested if statements check if temp contains a year which is multiple of 100 and not a multiple of 400 like 1900 which is not a leap year.
    if(temp%100 == 0)
    {
        if(temp%400 != 0)
        {
            temp += 4;
        }
    }
    *year = temp;
    return;
}

int main() {
    int yr;
    std::cout << "Hello world!\n";
    std::cout << "Enter a year: ";
    std::cin >> yr;
    NextLeapYear(&yr);
    std::cout << "Next Leap Year is: " << yr;

    return 0;
}

The function NextLeapYear() is:

void NextLeapYear(int *year)
{
    int temp = *year; //to prevent any mid changes in variable.
    if(temp < 0) //negative entry check
    {
        *year = 4;
        return;
    }
    temp = ((temp/4)+1)*4; // this is most probable leap yr next to year provided.
    //Nested if statements check if temp contains a year which is multiple of 100 and not a multiple of 400 like 1900 which is not a leap year.
    if(temp%100 == 0)
    {
        if(temp%400 != 0)
        {
            temp += 4;
        }
    }
    *year = temp;
    return;
}

Sample Output:

Hope it helps.


Related Solutions

Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Write a recursive function in C++ named multiplyNumbers, which takes one int argument n as input...
Write a recursive function in C++ named multiplyNumbers, which takes one int argument n as input and returns the product of numbers from 1 to n.
Write a MIPS function using a MARS 4.5 complier named void makeRandomArray(int arr[], int size) that...
Write a MIPS function using a MARS 4.5 complier named void makeRandomArray(int arr[], int size) that generates and stores a specified number of random numbers each of whose values are between 0 and 1000 in an array
IN C Write a function in the form: void play( int key, int duration) // duration...
IN C Write a function in the form: void play( int key, int duration) // duration units are tenths of a second which generates and prints samples of sin(w*t) for t=0,1,2,...,n-1 which represent a tone corresponding to piano key number key, where: n = (duration/10.0)*8000 w = (2π440rkey-49)/8000 r = 21/12 In the main program call your function to play the first three notes of three blind mice.
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
Write a subroutine that takes an int (.long) parameter and squares it. The parameter should be...
Write a subroutine that takes an int (.long) parameter and squares it. The parameter should be passed in on the stack. The answer should be returned in eax. The subroutine should not disturb any registers except eax, ecx, and edx. (Save any registers on the stack and restore them before exiting the subroutine.) Upon entry to the subroutine, push ebp, etc., to access the parameter.
Write a function in C# that takes an array of double as the parameter, and return...
Write a function in C# that takes an array of double as the parameter, and return the average
Write a function which takes one parameter int num, and prints out a countdown timer with...
Write a function which takes one parameter int num, and prints out a countdown timer with minutes and seconds separated by a colon (:). It should print out one line for each second elapsed and then pause one second before printing out the next line. A few things to note: - You can assume that calling the function usleep(1000000) makes the program pause for one second - It should count down from num minutes:zero seconds to zero minutes:zero seconds -...
Make a function definition in C for the following: void insert (double *b, int c, double...
Make a function definition in C for the following: void insert (double *b, int c, double s, int pos); //Insert value s at position pos in array. //needs: // c > 0, pos >= 0, and pos <= c-1. Elements b[0]...b[c-1] exist. //this will do: //Elements from indexes pos up to c-2 have been moved up to indexes pos+1 up to c-1. The value s has been copied into b[pos]. //Note: the data that was in b[c-1] when the function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT