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.
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...
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.
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...
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 -...
Write a function that takes a C string as an input parameter and reverses the string.
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 called linearSearch that takes an array as a parameter and search for...
C++ Write a function called linearSearch that takes an array as a parameter and search for a specific value inside this parameter. The function returns the frequency of a specific value in the array. In the main function: 1. Define an array called salaries of length 5. 2. Initialize the array by asking the user to input the values of its elements. 3. Define a variable called key and ask the user to enter a value for this variable. 4....
in c++, please provide only a function. the function should be named "tally." it takes no...
in c++, please provide only a function. the function should be named "tally." it takes no parameters but returns an int. the first int should be 0, next being 1, then 2, etc.. will rate if correct. again should only be 1 function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT