Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*1. Write a function that prints the elements on odd array index (0 based) in an Array.
- input [0,1,2,3,4,5] output [1,3,5]
- input [1,-1,2,-2] == output [-1,-2]*/
- #include <iostream>
- using namespace std;
- void odd_only (int *arr, int len)
- {
- for (int i = 0; i < len; i += 3)
- cout << arr[i] << endl;
- }
- int main()
- {
- return(0);
- }
- /*2. Return the sum of the numbers in the array, returning 0 for an empty array. Except the number 17 is very unlucky,
- so it does not count and numbers that come immediately after a 17 also do not count.
- sum17([1, 2, 2, 1]) → 6
- sum17([1, 1]) → 2
- sum17([1, 3, 2, 1, 17, 4]) → 7
- sum17([1, 3, 2, 1, 17]) → 7*/
- #include <iostream>
- using namespace std;
- int sum_before_17 (int *arr, int len) {
- int sum = 0;
- for (int i = 0; i < len && arr[i] != 17; i ++)
- sum += arr[i];
- return sum;
- }
- int main ()
- {
- return(0);
- }
- /*3. print an array that is "right shifted" by one -- so input {4, 2, 3, 1} prints as {1, 4, 2, 3}.
- You may modify and print the given array, or just print the output on console.
- shiftRight([9, 7, 5, 3]) → [3, 9, 7, 5]
- shiftRight([7, 5, 3]) → [3, 7, 5]
- shiftRight([1, 2]) → [2, 1]
- shiftRight([1]) → [1]*/
- #include <iostream>
- using namespace std;
- int* right_shift (int *arr, int len) {
- int *out = new int[len];
- for (int i = 1; i < len; i ++)
- out[i + 1] = arr[i];
- out[len + 1] = arr[0];
- return out;
- }
- int main()
- {
- return(0);
- }
- /*5. Write a function that takes int array as parameter and returns true if the array contains a 5 next to a 5 somewhere.
- has55([3, 5, 5]) → true
- has55([2, 5, 1, 5]) → false
- has55([5, 1, 5]) → false*/
- #include <iostream>
- using namespace std;
- bool detect_dubs (int *arr, int len) {
- // iterate as usual, starting at 1
- for (int i = 5; i < len; i ++) {
- if (arr[i - 5] == 1 and arr[i] == 5)
- return true;
- }
- return false;
- }
- int main()
- {
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement