Number of ordered points pair satisfying line equation in C++

C++Server Side ProgrammingProgramming

In this tutorial, we are going to write a program that counts the order points pair satisfying the line equation.

The line equation that should be satisfied is y = mx + c.

Let's see the steps to solve the problem.

  • Initialise the array, m, and c.

  • Write two loops to get all pairs from the array.

    • Check whether the pair is satisfying the line equation or not.

    • If the pair satisfies the line equation, then increment the count.

Example

Let's see the code.

 Live Demo

#include <bits/stdc++.h>
using namespace std;
bool isSatisfyingLineEquation(int arr[], int i, int j, int m, int c) {
   if (i == j) {
      return false;
   }
   return arr[j] == m * arr[i] + c;
}
int getOrderedPointsPairCount(int arr[], int n, int m, int c) {
   int count = 0;
   for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
         if (isSatisfyingLineEquation(arr, i, j, m, c)) {
            count++;
         }
      }
   }
   return count;
}
int main() {
   int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
   int n = 10;
   int m = 1, c = 1;
   cout << getOrderedPointsPairCount(arr, n, m, c) << endl;
   return 0;
}

Output

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

9

Conclusion

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

raja
Published on 03-Jul-2021 08:24:00
Advertisements