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 C function, for example void sumOfPrime(int n), that takes a positive int n as...
Write a C function, for example void sumOfPrime(int n), that takes a positive int n as a parameter, put all prime numbers less than n into an array, and print out the array and the sum of the numbers in that array. You can use the isPrime function above to save time.
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 function named "replacement" that takes a string as a parameter and returns an identical...
Write a function named "replacement" that takes a string as a parameter and returns an identical string except with every instance of the character "w" replaced with the character "v" My code: function replacement(word){ var str=word; var n=str.replace("w","v"); return n; } Syntax Error: function replacement incorrect on input Not sure how to fix? Can't use a loop for answer
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.
Complete function PrintPopcornTime(), with int parameter bagOunces, and void return type. If bagOunces is less than...
Complete function PrintPopcornTime(), with int parameter bagOunces, and void return type. If bagOunces is less than 3, print "Too small". If greater than 10, print "Too large". Otherwise, compute and print 6 * bagOunces followed by " seconds". End with a newline. Example output for ounces = 7: 42 seconds #include <stdio.h> void PrintPopcornTime(int bagOunces) { } int main(void) { int userOunces; scanf("%d", &userOunces); PrintPopcornTime(userOunces); return 0; } 2. Write a function PrintShampooInstructions(), with int parameter numCycles, and void return...
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...
Create a void function named swap that accepts two int parameters and swaps their values. Provide...
Create a void function named swap that accepts two int parameters and swaps their values. Provide a main function demonstrating a call to the swap function. Document the dataflow of the function.
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT