Question

In: Computer Science

Write a C++ program Given fuzzy sets A and B, find complement A, A ∪ B,...

Write a C++ program

Given fuzzy sets A and B, find complement A, A ∪ B, and A ∩ B

A = {0.2 : a, 0.7 : b, 0.5 : c, 0.01 : k, 0.98 : z} and

B = {0.42 : a, 0.6 : b, 0.5 : h, 0.1 : k, 0.44 : m, 0.8 : z}

Solutions

Expert Solution

the above problem can be broken down in following steps :

  1. create a function gen_fuzzy_sets which creates a map between keys and values provided as vector .
  2. create a function union which return the union of two fs , by comparing the values of keys present in both fs and taking the max of both.
  3. create a function union which return the intersection of two fs , by comparing the values of keys present in both fs and taking the min of both.
  4. create a function to calculate the complement of the fs by subtracting the 1 from values of each key of that fs.
  5. create a function to print the details of the fuzzy set.
  6. create the main function to pipe line all the above created function .

more details can be seen in the code .:

text code :

#include <iostream>
#include <map>
#include <iterator>
#include <vector>
using namespace std;

map<string , double>::iterator itr;

map<string , double > gen_fuzzy_set(vector<string> members , vector<double> elements){
//fucntion to generate a fuzzy set given its keys and values
map<string , double> fuzzy_set;
for(int i=0;i<members.size();i++){
fuzzy_set[members[i]] = elements[i];
}
return fuzzy_set;
}

map<string , double> get_union(map<string , double> &fs1 , map<string , double> &fs2){
//fucntion to calculate the union betweeen two fs by taking the max value of two same keys in both the fs.
vector<string> mem;
vector<double> ele;
map<string , double> smaller_fs = (fs1.size() <= fs2.size())?fs1:fs2;
map<string , double> bigger_fs = (fs1.size() > fs2.size())?fs1:fs2;
for(itr = smaller_fs.begin(); itr != smaller_fs.end() ; itr++){
string key = itr->first;
if(bigger_fs.count(key) != 0){
mem.push_back(key);
ele.push_back(max(smaller_fs[key] , bigger_fs[key]));
}
}
return gen_fuzzy_set(mem , ele);
}

map<string , double> get_intersection(map<string , double> &fs1 , map<string , double> &fs2){
//fucntion to calculate the union betweeen two fs by taking the min value of two same keys in both the fs.
vector<string> mem;
vector<double> ele;
map<string , double> smaller_fs = (fs1.size() <= fs2.size())?fs1:fs2;
map<string , double> bigger_fs = (fs1.size() > fs2.size())?fs1:fs2;
for(itr = smaller_fs.begin(); itr != smaller_fs.end() ; itr++){
string key = itr->first;
if(bigger_fs.count(key) != 0){
mem.push_back(key);
ele.push_back(min(smaller_fs[key] , bigger_fs[key]));
}
}
return gen_fuzzy_set(mem , ele);
}

map<string , double> complement(map<string , double> &fs){
//given a fuzzy set , calculating its complement , by subtracting 1 from value of each key in the fs.
for(itr = fs.begin(); itr != fs.end() ; itr++){
fs[itr->first] = 1 - itr->second;
}
return fs;
}

void print_fs(map<string , double> &fs){
for(itr = fs.begin(); itr != fs.end();itr++){
cout<<itr->first<< " "<<itr->second<<endl;
}
}

int main(){
vector<string> mem1;
mem1.push_back("a");
mem1.push_back("b");
mem1.push_back("c");
mem1.push_back("k");
mem1.push_back("z");
vector<double> ele1;
ele1.push_back(0.2);
ele1.push_back(0.7);
ele1.push_back(0.5);
ele1.push_back(0.01);
ele1.push_back(0.98);
vector<string> mem2;
mem2.push_back("a");
mem2.push_back("b");
mem2.push_back("h");
mem2.push_back("k");
mem2.push_back("m");
mem2.push_back("z");
vector<double> ele2;
ele2.push_back(0.42);
ele2.push_back(0.6);
ele2.push_back(0.5);
ele2.push_back(0.1);
ele2.push_back(0.44);
ele2.push_back(0.8);


map<string , double> fs1 = gen_fuzzy_set(mem1 , ele1);
cout<<"fuzzy_set 1"<<endl;
print_fs(fs1);
map<string , double> fs2 = gen_fuzzy_set(mem2 , ele2);
cout<<"fuzzy_set 2"<<endl;
print_fs(fs2);
map<string , double> uni = get_union(fs1 , fs2);
map<string , double> inter = get_intersection(fs1 , fs2);
map<string , double> comp = complement(fs1);
cout<<"union"<<endl;
print_fs(uni);
cout<<"intersection"<<endl;
print_fs(inter);
cout<<"complement of fs1"<<endl;
print_fs(comp);
}


Related Solutions

Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file...
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file prog2 input.txt. The first line of the file corresponds to the set ’A’, the second line is the set ’B’, and the third line is the set ’C’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The...
Write a C++ program to find the number of pairs of integers in a given array...
Write a C++ program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number.
Part1: Write a program in C/C++ to find the maximum flow in the given Flow Network...
Part1: Write a program in C/C++ to find the maximum flow in the given Flow Network using (i)Ford -Fulkerson algorithm and (ii) Edmond-Karps algorithm Go through the related text and implement each of these algorithms using the efficient data structure. Show the results of different steps of these algorithms for an instance of the flow network with total number of nodesV=6 (please note down that in a flow network there are two special nodes source and sink) and total number...
Write a C++ program to find K largest elements in a given array of integers. For...
Write a C++ program to find K largest elements in a given array of integers. For eeample, if K is 3, then your program should ouput the largest 3 numbers in teh array. Your program is not supposed to use any additional array.
Write a program in Java that will take as input two sets A and B, and...
Write a program in Java that will take as input two sets A and B, and returns a list of all functions from A to B.
write pseudocode not c program If- else programming exercises 1.    Write a C program to find...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find maximum between two numbers. 2.    Write a C program to find maximum between three numbers. 3.    Write a C program to check whether a number is negative, positive or zero. 4.    Write a C program to check whether a number is divisible by 5 and 11 or not. 5.    Write a C program to check whether a number is even or odd. 6.    Write...
A, B and C be sets. (a) Suppose that A ⊆ B and B ⊆ C....
A, B and C be sets. (a) Suppose that A ⊆ B and B ⊆ C. Does this mean that A ⊆ C? Prove your answer. Hint: to prove that A ⊆ C you must prove the implication, “for all x, if x ∈ A then x ∈ C.” (b) Suppose that A ∈ B and B ∈ C. Does this mean that A ∈ C? Give an example to prove that this does NOT always happen (and explain why...
Write down the C++ Program To Find Factorial.
Write a function, which accepts an integer value as an argument, finds the factorial of that integer value, and then returns the factorial value to the main program. Write a main program that will call the function by passing an integer value and print the factorial value returned by the function. 
Let A, B, C be arbitrary sets. Prove or find a counterexample to each of the...
Let A, B, C be arbitrary sets. Prove or find a counterexample to each of the following statements: (b) A ⊆ B ⇔ A ⊕ B ⊆ B
write a program on c++ that outputs a calendar for a given month in a given...
write a program on c++ that outputs a calendar for a given month in a given year, given the day of the week on which the 1st of the month was. The information in numeric format (months are: 1=Januay, 2=February 3=March, 4= April, 5= May, 6= June, 7= July... etc days are 1=Sunday, 2=Monday, 3= Tuesday, 4= Wednesday, 5= Thursday, 6= Friday and 7= Saturday ). The program output that month’s calendar, followed by a sentence indicating on which day...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT