$30
Assignment 1 & 2 – (200 points)
Instructions
• Answer the questions individually. Group effort is not allowed.
• Ensure that your code runs on remote.cs.binghamton.edu.
• Useful resources:
1. Common linux commands: http://www.informit.com/blogs/blog.aspx?uk=The10-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 n
th 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. If the numbers 1 to 5 are written out in words: OnE, twO, thrEE, fOUr, fIvE, then
there are 2 + 1 + 2 + 2 + 2 = 9 vowels used in total. Write a function unsigned
int count vowels(unsigned long num); that accepts a number as
input and returns the sum of all vowels when the number is written out in words.
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. Write a function unsigned int count ones(long n) that accepts a 64bit
integer n and returns the number of 1’s in the binary representation of the number
(2’s complement representation for negative numbers). (40 points)
4. Write a function unsigned long swap bytes(unsigned long n) that
accepts an 8-byte integer, swaps bytes 1 and 2, 3 and 4, 5 and 6, and 7 and 8 and
returns the result. (40 points)
Example: if n = 0x12345678deadbeef, swap bytes returns 0x34127856addeefbe.
5. Write a function long a4 minus b4(int a, int b) that returns a
4−b
4
.
Do not use math library. Assume −100 ≤ a, b ≤ 100 (20 points)