Assignment 1 & 2 Solution

$30.00

Description

Instructions

• Answer the questions individually. Group effort is not allowed.

• Solutions must be committed to your respective repositories on github.

• Ensure that your code runs on remote.cs.binghamton.edu.

• Useful resources:

1. Common linux commands: http://www.informit.com/blogs/blog.aspx?uk=The-

10-Most-Important-Linux-Commands

2. http://c-faq.com/

Questions

1. A Fibonacci sequence is a series of numbers: 0, 1, 1, 2, 3, 5, 8, … where: The first number is 0, the second number is 1, and each successive number is found by adding the two preceding numbers. Write a function with name “isFib” that accepts an integer i as input and returns n such that i is the nth Fibonacci number. If i is not a Fibonacci number, the function returns -1. Assume 0 ≤ i ≤

1000000000. If i is 1, return 2. Use the prototype: int isFib(unsigned long i); (40 points)

2.Ifthenumbers1to5arewrittenoutinwords:OnE,twO,thrEE,fOUr,fIvE,then thereare2+1+2+2+2=9vowelsusedintotal.Writeafunctionunsigned intcountvowels(unsigned longnum);thatacceptsanumberas inputandreturnsthesumofallvowelswhenthenumberiswrittenoutinwords.

0 ≤ num ≤ 1000000000. Return 0 if input is invalid.

NOTE: Do not count ‘and’ in the expansion of the word. For example, 342 (thrEE hUndrEd and fOrty-twO) contains 6 vowels and 115 (OnE hUndrEd and fIftEEn) contains 7 vowels. 1500 is OnE thOUsAnd fIvE hUndrEd (not fifteen hundred) contains 9 vowels. (60 points)

1

3.Writeafunctionunsignedint countones(longn)thatacceptsa64bit integernandreturnsthenumberof1’sinthebinaryrepresentationofthenumber (2’scomplementrepresentationfornegativenumbers).(40points)

4.Writeafunctionunsigned long swapbytes(unsignedlongn)that acceptsan8-byteinteger,swapsbytes1and2,3and4,5and6,and7and8and returnstheresult.(40points)

Example:ifn=0x12345678deadbeef,swapbytesreturns0x34127856addeefbe.

5.Writeafunctionlong a4minusb4(inta, int b)thatreturnsa4 −b4 .

Do not use math library. Assume −100 ≤ a, b ≤ 100 (20 points)