-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscrete_math_functions.cpp
More file actions
147 lines (122 loc) · 4.2 KB
/
Copy pathdiscrete_math_functions.cpp
File metadata and controls
147 lines (122 loc) · 4.2 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Logan Jones
// ---------------------------------------------------------------------------
// This program identifies various properties of a provided function
// ---------------------------------------------------------------------------
#include <iostream>
#include <unordered_set>
#include <unordered_map>
using namespace std;
// Valid if no x value has two y values and all x and y values are part of the
// Domain and Range respectively (we will accept partial functions as valid)
bool validFunction = true;
// functin declarations
bool inRange(int i);
bool OneToOne(const unordered_map<int, int> m);
bool Onto(const unordered_map<int, int> m);
ostream &printResults(ostream &out, const unordered_map<int, int>);
// Function is invalid if x or y is less than one or greater than five
// Function is invalid if any x value appears twice
// One-to-one: no repeated y values
// Onto: five y values
// Bijection: both one-to-one and onto
int main(){
int numPairs = 0;
unordered_map<int, int> function;
cout << "How many ordered pairs does your function contain? (1-5)" << endl;
cin >> numPairs; // grab number of ordered pairs.
cout << endl;
// end early if numPairs is out of bounds.
if (!inRange(numPairs)){ // custom in range function
cout << "User input out of range" << endl;
return 0; // end main
}
cout << "Enter ordered pairs one at a time in the form 'x y' between 1-5";
cout << endl;
// grab ordered pairs and store in a map of ints.
for (int i = 1; i <= numPairs; i++){
int x,y;
cout << "Pair " << i << ": " << endl;
cin >> x >> y;
cout << endl;
// if x and y are between 1-5 (in the domain and range)
if (inRange(x) && inRange(y)) {
auto insertion = function.insert({x, y});
if (!insertion.second){ // if insertion fails due to duplicate x
validFunction = false;
}
}
else{ // out of range
validFunction = false;
function.insert({x, y});
}
} // end for loop, function initialized.
// print out function properties
printResults(cout, function);
return 0; // end main
}
// true if between 1-5 otherwise false.
bool inRange(int i){
return (i >= 1 && i <= 5);
}
// One-to-one: No y value has more than one x that maps to it
// (e.g., f(3) = 2 and f(4) = 2 is NOT one-to-one)
bool OneToOne(const unordered_map<int, int> m){
unordered_set<int> yValues;
// iterate through all pairs and check if 1 to 1
for (const auto& pair : m){
if (yValues.count(pair.second)){ // if already exists in set
return false;
}
// else insert y value into set
yValues.insert(pair.second);
}// end for, no duplicate y values
return true;
}
// Onto: for every y in the Range, there is at least one x such that f(x) = y.
bool Onto(const unordered_map<int, int> m){
unordered_set<int> yValues;
for (const auto& pair : m){ // initialize yvalue set
yValues.insert(pair.second);
}
// checks if y value is missing from function
for (size_t i = 1; i <= 5; i++){
if (!yValues.count(i)){
return false;
}
}
return true;
}
ostream& printResults(ostream& out, const unordered_map<int, int> m){
// function invalid, return early
if (!validFunction){
out << "That is not a valid function" << endl;
return out;
}
out << "Function described by:" << endl;
for (const auto& pair : m){
out << "f(" << pair.first << ") = " << pair.second << endl;
}
out << endl;
out << "This function is a valid function" << endl;
bool isOneToOne = OneToOne(m);
bool isOnto = Onto(m);
if(isOneToOne){ // 1-to-1
out << "This function is 1-to-1" << endl;
}
else{
out << "This function is not 1-to-1" << endl;
}
if (isOnto){
out << "This fuction is onto" << endl;
}
else{
out << "This fuction is not onto" << endl;
}
if (isOneToOne && isOnto){
out << "This function is a bijection" << endl;
}
else{
out << "This function is not a bijection" << endl << endl;
}
return out;
}