Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i.
For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6].
Follow-up: what if you can't use division?
This is what I came up with, any suggestions or improvements upon my code would be very helpful. Thank you!
void arrProd(const int arr[], int size){
int prod = 1;
int arr2[size];
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
if(j != i)
prod = prod * arr[j];
}
arr2[i] = prod;
prod = 1;
}
//Prints out final array
for(int i = 0; i < size; i++){
std::cout << arr2[i] << " ";
}
std::cout << std::endl;
}