Divide every element of one array by other array elements in C++ Program


In this tutorial, we are going to write a program that divides one array of elements by another array of elements.

Here, we are following a simple method to complete the problem. Let's see the steps to solve the problem.

  • Initialize the two arrays.

  • Iterate through the second array and find the product of the elements.

  • Iterate through the first array and divide each element with a product of the second array elements.

Example

Let's see the code.

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void divideArrOneWithTwo(int arr_one[], int arr_two[], int n, int m) {
   int arr_two_elements_product = 1;
   for (int i = 0; i < m; i++) {
      if (arr_two[i] != 0) {
         arr_two_elements_product = arr_two_elements_product * arr_two[i];
      }
   }
   for (int i = 0; i < n; i++) {
      cout << floor(arr_one[i] / arr_two_elements_product) << " ";
   }
   cout << endl;
}
int main() {
   int arr_one[] = {32, 22, 4, 55, 6}, arr_two[] = {1, 2, 3};
   divideArrOneWithTwo(arr_one, arr_two, 5, 3);
   return 0;
}

Output

If you run the above code, then you will get the following result.

5 3 0 9 1

Conclusion

If you have any queries in the tutorial, mention them in the comment section.

Updated on: 27-Jan-2021

712 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements