Advertisement
Abreu-Hoff

Untitled

Apr 5th, 2021
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. /*1. Write a function that prints the elements on odd array index (0 based) in an Array.
  2.  
  3. input [0,1,2,3,4,5] output [1,3,5]
  4. input [1,-1,2,-2] == output [-1,-2]*/
  5.  
  6. #include <iostream>
  7.  using namespace std;
  8.  
  9.  
  10. void odd_only (int *arr, int len)
  11.     {
  12.     for (int i = 0; i < len; i += 3)
  13.         cout << arr[i] << endl;
  14.     }
  15. int main()
  16. {
  17. return(0);
  18. }
  19.  
  20. /*2. Return the sum of the numbers in the array, returning 0 for an empty array. Except the number 17 is very unlucky,
  21.  so it does not count and numbers that come immediately after a 17 also do not count.
  22.  
  23.  sum17([1, 2, 2, 1]) → 6
  24.  sum17([1, 1]) → 2
  25.  sum17([1, 3, 2, 1, 17, 4]) → 7
  26.  sum17([1, 3, 2, 1, 17]) → 7*/
  27. #include <iostream>
  28. using namespace std;
  29.  
  30. int sum_before_17 (int *arr, int len) {
  31.   int sum = 0;
  32.   for (int i = 0; i < len && arr[i] != 17; i ++)
  33.     sum += arr[i];
  34.   return sum;
  35. }
  36. int main ()
  37. {
  38.     return(0);
  39. }
  40.  
  41. /*3. print an array that is "right shifted" by one -- so input {4, 2, 3, 1} prints as  {1, 4, 2, 3}.
  42.  You may modify and print the given array, or just print the output on console.
  43.  
  44.  shiftRight([9, 7, 5, 3]) → [3, 9, 7, 5]
  45.  shiftRight([7, 5, 3]) → [3, 7, 5]
  46.  shiftRight([1, 2]) → [2, 1]
  47.  shiftRight([1]) → [1]*/
  48. #include <iostream>
  49.  using namespace std;
  50.  
  51. int* right_shift (int *arr, int len) {
  52.   int *out = new int[len];
  53.     for (int i = 1; i < len; i ++)
  54.     out[i + 1] = arr[i];
  55.     out[len + 1] = arr[0];
  56.     return out;
  57.   }
  58. int main()
  59. {
  60. return(0);
  61. }
  62.  
  63. /*5. Write a function that takes int array as parameter and returns true if the array contains a 5 next to a 5 somewhere.
  64.  
  65. has55([3, 5, 5]) → true
  66. has55([2, 5, 1, 5]) → false
  67. has55([5, 1, 5]) → false*/
  68.  
  69. #include <iostream>
  70.  using namespace std;
  71. bool detect_dubs (int *arr, int len) {
  72.     // iterate as usual, starting at 1
  73.     for (int i = 5; i < len; i ++) {
  74.    
  75.         if (arr[i - 5] == 1 and arr[i] == 5)
  76.             return true;
  77.     }
  78.  
  79.     return false;
  80. }
  81. int main()
  82. {
  83. return(0);
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement