Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count the number of rectangles such that ratio of sides lies in the range [a,b] in C++.
Given sides of rectangles in and range variables first and last. The goal is to find the count of rectangles that have a ratio of their side’s length/breadth lying in the range [ first, last].
For Example
Input
rec[] = { { 200, 210 }, { 100, 50 }, { 300, 190}, {180, 200}, {300, 200}} and first = 1.0, last = 1.6
Output
Count of number of rectangles such that ratio of sides lies in the range [a,b] are: 4
Explanation
The sides that have ratio in the range [ 1.0,1.6 ] are :
{200,210}, {300,190}, {180,200}, {300,200}
Input
rec[] = { { 10,20 }, { 30, 10 }, { 100, 500}, {900, 300}, {450, 90}} and
first = 3.0, last = 4.0
Output
Count of number of rectangles such that ratio of sides lies in the range [a,b] are: 2
Explanation
The sides that have ratio in the range [ 3.0,4.0 ] are :
{30,10}, {900,300}
Approach used in the below program is as follows −
In this approach we will take sides in the form of an array of pair<int,int>. For each pair check if the larger value/smaller value has a result which lies in the range [ first,last ]. If true then increment count of such pairs.
Take an array rec[] of type pair<int,int>.
Take two variables first and last for defining range.
Function ratio_sides(pair<int, int> rec[], int total, double first, double last) takes the sides of rectangles and returns the count of number of rectangles such that ratio of sides lies in the range [a,b].
Take the initial count as 0.
Using a for loop traverse from i=0 to i<total.
Extract larger value in pair rec[i] as maxi = max(rec[i].first, rec[i].second).
Extract smaller value in pair rec[i] as mini = min(rec[i].first, rec[i].second).
Calculate average=maxi/mini.
If average has value in range [ first,last ], then increment count.
At the end of the for loop return count as result..
Example
#include <bits/stdc++.h>
using namespace std;
int ratio_sides(pair<int, int> rec[], int total, double first, double last){
int count = 0;
for (int i = 0; i < total; i++){
double maxi = max(rec[i].first, rec[i].second);
double mini = min(rec[i].first, rec[i].second);
double average = maxi/mini;
if (average >= first){
if(average <= last){
count++;
}
}
}
return count;
}
int main(){
pair<int, int> rec[] = { { 200, 210 }, { 100, 50 }, { 300, 190}, {180, 200}, {300, 200}};
int total = 5;
double first = 1.0, last = 1.6;
cout<<"Count of number of rectangles such that ratio of sides lies in the range [a,b] are: "<<ratio_sides(rec, total, first, last);
return 0;
}
Output
If we run the above code it will generate the following output −
Count the number of rectangles such that ratio of sides lies in the range [a,b] are: 4