0

Array Manipulation

Array Manipulation This was the given question my code is attached Write a function that for given two arrays (input arrays) returns a new array (output array) that contains elements of the input arrays that are divisible by 5. Hints: Remember to pass sizes of input arrays into this function. Also, recall how arrays are returned from the function. More specifically, what is default mechanism for passing arrays into a funtion. Example: Input array #1: 1 15 23 10 20 79 80 55 Input array #2: 31 5 38 190 21 95 83 85 22 Output array: 15 10 20 80 55 5 190 95 85 Note: You do not need to return the size of the output array. You may assume that the size of the output array is always valid. //i am not sure how to get the output array https://code.sololearn.com/cOj4G6s6mr3Y/?ref=app

18th Dec 2018, 2:49 PM
Evan Vuong
Evan Vuong - avatar
2 Réponses
+ 9
#include <iostream> using namespace std; int *generateDivisibles(int *arr1, int *arr2, int arr1Size, int arr2Size) { static int divisblesArr[20] = {0}; //Supposing fixed size int index = 0; for(int i = 0; i < arr1Size; i++) { if(arr1[i] % 5 == 0) divisblesArr[index++] = arr1[i]; } for(int i = 0; i < arr2Size; i++) { if(arr2[i] % 5 == 0) divisblesArr[index++] = arr2[i]; } return divisblesArr; } int main() { int arr1Size = 8, arr2Size = 9; int arr1[arr1Size] = {1,15,23,10,20,79,80,55 }; int arr2[arr2Size] = {31,5,38,190,21,95,83,85,22 }; int *divisblesArr = generateDivisibles(arr1, arr2, arr1Size, arr2Size); for(int i = 0; i < 20; i++) cout << divisblesArr[i] << " "; return 0; }
18th Dec 2018, 7:16 PM
blACk sh4d0w
blACk sh4d0w - avatar
0
Updated to finish. Note I used globals as you called function twice instead of once with both inputs. https://code.sololearn.com/ct3LtyPMmYN1
18th Dec 2018, 7:05 PM
John Wells
John Wells - avatar