Let m and n be integers such that 1S m,nS5. Write a program that will prompt for
ID: 3710777 • Letter: L
Question
Let m and n be integers such that 1S m,nS5. Write a program that will prompt for m and n as input and then output a list all the functions f from the set 11,2,., m to the set fl,2,., ni such that a) The functions are be listed one function per line with a line number. b) The functions are labeled appropriately labeled as constant, injective, surjective, bijective or other c) The total numbers of each type of function is printed at the end. fotal noumbers of each type of fiunction is printed at the end. Example 1: If ?-1, 2, 3} and B-1, 2 } , then the programs output should be For m 3 and n 2, the functions are: 1: f1(1)- 1, f1(2) -1, f1(3) -1 Constant 2: f2(1)-1, f2(2) - 1, f2(3) - 2 Surjective 3: f3(1)-1, f3(2) - 2, f3(3) 1 Surjective 4: f4(1) -1, f4(2) - 2, f4(3) 2 Surjective 5: f5C1)-2, f5(2) - 1, f5(3) 1 Surjective 6: f6(1) -2, f6C2) - 1, f6(3) 2 Surjective 7: f7(1) -2, f7(2) -2, f7(3) -1 Surjective 8: f8(1) 2, f8(2) -2, f8(3) 2 Constant 2 Constant, 0 Injective, 6 Surjective 0 Bijective, 0 OtherExplanation / Answer
here is your program : ---------->>>>>>>>>>>>
#include<iostream>
using namespace std;
bool surjective(int arr[],int n,int n2){
int arr1[n2];
for(int i = 0;i<n2;i++){
arr1[i] = 0;
}
for(int i = 0;i<n;i++){
arr1[arr[i]-1] = 1;
}
for(int i = 0;i<n2;i++){
if(arr1[i] == 0){
return false;
}
}
return true;
}
bool injective(int arr[],int n,int n2){
int arr1[n2];
for(int i = 0;i<n2;i++){
arr1[i] = 0;
}
for(int i = 0;i<n;i++){
arr1[arr[i]-1] = arr1[arr[i]-1] + 1;
}
for(int i = 0;i<n2;i++){
if(arr1[i] >= 2 || arr1[i] == 0){
return false;
}
}
return true;
}
bool constants(int arr[],int n){
int s = arr[0];
for(int i = 1;i<n;i++){
if(s != arr[i]){
return false;
}
}
return true;
}
int main(){
double n1,n2;
while(true){
cout<<" Please Enter Two Values(int 1-5) : ";
cin>>n1>>n2;
if(n1 >= 1 && n1 <= 5 && n2 >= 1 && n2 <= 5){
if((n1 - (int)(n1)) != 0){
continue;
}
if((n2 - (int)(n2)) != 0){
continue;
}
break;
}
}
int n11 = (int)n1;
int n12 = (int)(n2);
int arr[n11];
cout<<" For m = "<<n11<<" and n = "<<n12<<", The functions are : ";
for(int i = 0;i<n11;i++){
arr[i] = 1;
}
int bij = 0,sur = 0,inj = 0,con = 0,oth = 0;
int count = 1;
while(true){
if(arr[0] > n12){
break;
}
cout<<" "<<count<<" : ";
for(int j = 0;j<n11;j++){
cout<<"f"<<count<<"("<<(j+1)<<" ) = "<<arr[j]<<", ";
}
if(constants(arr,n11)){
con++;
cout<<" Constant";
}
else if(injective(arr,n11,n12) && surjective(arr,n11,n12)){
bij++;
cout<<" Bijective";
}
else if(injective(arr,n11,n12)){
inj++;
cout<<" Injective";
}else if(surjective(arr,n11,n12)){
sur++;
cout<<" Surjective";
}else{
oth++;
cout<<" Other";
}
for(int j = n11-1;j >= 0;j--){
arr[j] = arr[j] + 1;
if(arr[j] <= n12){
break;
}else if(j != 0){
arr[j] = 1;
}
}
count++;
}
cout<<" Surjective = "<<sur<<" Bijective = "<<bij<<" Injective = "<<inj<<" Constant = "<<con<<" other = "<<oth;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.