-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoordinates.cpp
More file actions
89 lines (75 loc) · 2.36 KB
/
coordinates.cpp
File metadata and controls
89 lines (75 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include<iostream>
#include<vector>
#include<tuple>
#include<iterator>
#include<string>
/*
Create a function that creates a vector of coordinates as tuples
which satisfies the constraint that ax+by<=c and x>=0 and y>=0
*/
std::vector<std::tuple<int, int>> in_range(float a, float b, float c)
{
/*
---------------------------------------------------------------
THe in_range(float, float, float) is a function that takes as
argument the values of a,b and c and returns a vector of
co-ordinates(x,y) in the form of tuples, which are bounded by
x>=0, y>=0 and ax+by<=c straight line
^
*
*
(y=0) * *
* . * <- ax+by=c
* . . .*
* . . . . *
* * * * * * * * * * >
0 (y=0)
---------------------------------------------------------------
*/
// Maximum value that x can take is when y=0
float max_x = c/a;
// Maximum value that x can take is when y=0
float max_y = c/b;
// initializing the vector of tuples
std::vector<std::tuple<int, int>> coordinates;
// looping over the possible values of x and y
for(int x=0; x<= max_x; x++)
{
for(int y=0; y<= max_y; y++)
{
if(a*x+b*y<=c) // check if the inequality is satisfied
{ /*
if inequality is satisfied add the coordinate
tuple into the vector.
std::make_tuple(a,b,...,d)makes a tuple
<vector>.push_back(elem) enters elem to the tail
of the vector
*/
coordinates.push_back(std::make_tuple(x,y));
}
}
}
// return the vector of tuples
return coordinates;
}
int main(){
// Take in inputs
float a,b,c;
std::cout << "Enter the value of a: ";
std::cin >> a;
std::cout << "Enter the value of b: ";
std::cin >>b;
std::cout << "Enter the value of c: ";
std::cin >>c;
// containter to store the vector if tuples
std::vector<std::tuple<int, int>> coords = in_range(a,b,c);
int x,y;
for(auto &i: coords)
{
// capture all the elements of the tuples
// std:tie(x,y) allows for list expansion
std::tie(x,y) = i;
std::cout<<"(" << x << "," << y << ")" << std::endl;
}
return 0;
}