0% found this document useful (0 votes)
3 views6 pages

C++ Programming Assignment

The document outlines a series of C++ programming exercises with specific function signatures and requirements. Exercises include tasks such as determining Fibonacci numbers, checking if all array values are the same, counting prime numbers, and identifying anagrams. Each exercise specifies input parameters and expected output, emphasizing the need for non-recursive solutions in certain cases.

Uploaded by

aragawasres8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

C++ Programming Assignment

The document outlines a series of C++ programming exercises with specific function signatures and requirements. Exercises include tasks such as determining Fibonacci numbers, checking if all array values are the same, counting prime numbers, and identifying anagrams. Each exercise specifies input parameters and expected output, emphasizing the need for non-recursive solutions in certain cases.

Uploaded by

aragawasres8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

C++ Programming Exercises Due Date: 12/11/22

1. The Fibonacci sequence of numbers is 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The first and second
numbers are 1 and after that ni = ni-2 + ni-1, e.g., 34 = 13 + 21. Write a method with signature
int isFibonacci(int n) which returns 1 if its argument is a number in the Fibonacci sequence,
otherwise it returns 0. For example, isFibonacci(13) returns a 1 and isFibonacci(27) returns
a 0. Your solution must not use recursion because unless you cache the Fibonacci numbers
as you find them, the recursive solution recomputes the same Fibonacci number many times.

2. Write a function named allValuesTheSame that returns 1 if all elements of its argument
array have the same value. Otherwise, it returns 0.
The function signature is:
int allValuesTheSame(int a[ ], int len) where len is the number of elements in a
Examples:

3. Write a function named primeCount with signature int primeCount(int start, int end);
The function returns the number of primes between start and end inclusive.
Recall that a prime is a positive integer greater than 1 whose only integer factors are 1 and
itself.

1
Examples:

4. The sum factor of an array is defined to be the number of times that the sum of the array
appears as an element of the array. So the sum factor of {1, -1, 1, -1, 1, -1, 1} is 4 because
the sum of the elements of the array is 1 and 1 appears four times in the array. And the sum
factor of {1, 2, 3, 4} is 0 because the sum of the elements of the array is 10 and 10 does not
occur as an element of the array. The sum factor of the empty array { } is defined to be 0.
Write a function named sumFactor that returns the sum factor of its array argument.

The function signature is:


int sumFactor(int a[ ], int len) where len is the number of elements in the array.
Examples:

5. An array can hold the digits of a number. For example the digits of the number 32053 are
stored in the array {3, 2, 0, 5, 3}. Write a function called repsEqual that takes an array and
an integer and returns 1 if the array contains only the digits of the number in the same order
that they appear in the number. Otherwise it returns 0.
The function signature is:
int repsEqual(int a[ ], int len, int n) where len is the number of elements in the array.

2
Examples (note: your program must work for all values of a and n, not just those given here!)

6. One word is an anagram of another word if it is a rearrangement of all the letters of the
second word. For example, the character arrays {'s', 'i', 't'} and {'i', 't', 's'} represent words
that are anagrams of one another because "its" is a rearrangement of all the letters of "sit"
and vice versa. Write a function that accepts two character arrays and returns 1 if they are
anagrams of one another, otherwise it returns 0. For simplicity, if the two input character
arrays are equal, you may consider them to be anagrams.

The function signature is


int areAnagrams(a1 char[ ], a2 char[ ], int len) where len is the length of a1 and a2.

Hint: Please note that "pool" is not an anagram of "poll" even though they use the same
letters. Please be sure that your function returns 0 if given these two words! You can use
another array to keep track of each letter that is found so that you don't count the same letter
twice (e.g., the attempt to find the second "o" of "pool" in "poll" should fail.)

Hint: do not modify either a1 or a2, i.e., your function should have no side effects! If your
algorithm requires modification of either of these arrays, you must work with a copy of the
array and modify the copy!

3
Examples

7. The Fibonacci sequence of numbers is 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The first and second
numbers are 1 and after that ni = ni-2 + ni-1, e.g., 34 = 13 + 21. A number in the sequence is
called a Fibonacci number.

Write a function with signature int closestFibonacci(int n) which returns the largest
Fibonacci number that is less than or equal to its argument. For example,
closestFibonacci(13) returns 8 because 8 is the largest Fibonacci number less than 13 and
closestFibonacci(33) returns 21 because 21 is the largest Fibonacci number that is <= 33.
closestFibonacci(34) should return 34.

If the argument is less than 1 return 0. Your solution must not use recursion because unless
you cache the Fibonacci numbers as you find them, the recursive solution recomputes the
same Fibonacci number many times.

8. An array is called layered if its elements are in ascending order and each element appears
two or more times. For example, {1, 1, 2, 2, 2, 3, 3} is layered but {1, 2, 2, 2, 3, 3} and {3, 3,
1, 1, 1, 2, 2} are not.
Write a function named isLayered that accepts an integer array and returns 1 if the array is
layered, otherwise it returns 0.

The function signature is:


int isLayered(int a[ ], int len) where len is the number of elements in the array

4
Examples:

9. Write a function named largestAdjacentSum that iterates through an array computing the
sum of adjacent elements and returning the largest such sum. You may assume that the array
has at least 2 elements.

The function signature is:


int largestAdjacentSum(int a[ ], int len) where len is the number of elements in a

Examples:

10. Write a function named countOnes that returns the number of ones in the binary representati
on of its argument. For example, countOnes(9) returns 2 because the binary representation
of 9 is 1001.

Some other examples:


countOnes(5) returns 2 because binary 101 equals 5
countOnes(15) returns 4 because binary 1111 equals 15.

5
You may assume that the argument is greater than 0.
The function prototype is
int countOnes(int n);

Hint use modulo and integer arithmetic to count the number of ones.

11. Write a function named countDigit that returns the number of times that a given digit appea
rs in a positive number. For example countDigit(32121, 1) would return 2 because there are
two 1s in 32121.
Other examples:
countDigit(33331, 3) returns 4
countDigit(33331, 6) returns 0
countDigit(3, 3) returns 1
The function should return -1 if either argument is negative, so
countDigit(­543, 3) returns -1.

The function signature is:


int countDigit(int n, int digit)
Hint: Use modulo base 10 and integer arithmetic to isolate the digits of the number.

12. An integer is defined to be “continuous factored” if it can be expressed as the product of


two or more continuous integers greater than 1.
Examples of “continuous factored” integers are:
6 = 2 * 3.
60 = 3 * 4 * 5
120 = 4 * 5 * 6
90 = 9*10
Examples of integers that are NOT “continuous factored” are:
99 = 9*11, 121=11*11, 2=2, 13= 13

Write a function named isContinuousFactored(int n) that returns 1 if n is continuous factor


ed and 0 otherwise.

You might also like