Array Manipulation | Sololearn: Learn to code for FREE!

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

12/18/2018 2:49:59 PM

Evan Vuong

2 Answers

New Answer

+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; }

0

Updated to finish. Note I used globals as you called function twice instead of once with both inputs. https://code.sololearn.com/ct3LtyPMmYN1