CS1372 Homework 2 solution

$23.99 $16.79



This assignment is to make sure you have an understand of conditionals, loops, bitpacking, and functions
in C.
Write the following function in a file named “hw2Lib.cpp” with header file names “hw2Lib.h”. You
should also have a main function in another file named “main.cpp”. Your main function should thoroughly
test all of your functions. That said, you should try to think of possibilities when your functions might fail
(for example, an unexpected input).
This assignment does not use the Gameboy. Instead of creating a “GBA ROM Wizard” project, create an
“Empty Project” in Visual Studio. This project will run in a console window. You can print to the screen
using the printf function (see Kernighan and Ritchie).
1. Write a function called countOnes that will take in an unsigned int and count the number of 1’s
contained therein if the number was expressed as a binary number. In other words count the number
of bits that are on (1). You must use a loop. Do not hardcode!
2. Write a macro called isEven that will take in an int type and return 1 if even.
3. Write a macro called isOdd that will take in an int type and return 1 if odd.
4. When is a year a leap year? If the year is evenly visible by 4 it is a leap year unless the year is evenly
divisible by 100 in which case it is not a leap year unless it is evenly divisible by 400 in which case it
is a leap year. Write a function called isLeap that will take in an int containing a year and will return
1 if it is a leap year and 0 if it is not.
5. Write a function called daysInMonth which will take in an int representing a month (1 for January, 2
for February, etc.) and an int for the year. It will return an int representing the number of days in
that month for that year. Your function must use a switch/case statement. If the month number is
less than 1 or grater than 12 your function should return 0. You may assume that the year is valid.
Note: You should use the function from Problem 4 on cases where the month is February.
6. Write a function that will pack two unsigned characters into an unsigned short. That is if you were
passed 3 and 5 your function would return 0x0305. Call your function packChars:
unsigned short packChars(unsigned char n0, unsigned char n1)
As before, include the following in the top of your hw2Lib.cpp file:
// Name: //
// Prism Account: //
// Collaboration: //
// “I worked on the homework assignment alone, using only //
// course materials.” //
// or //
// “I received outside aid on this assigned from the following //
// person(s): //
NOTE: None of your functions should print anything to the console. You may wish you wish to print
from your function while debugging, but your final submission should only print from main.