0

I trying to do my HW, and stuck.

Here is my problem:

I am given two sets: let set1 {a b c} and set2{1 2 3 4 5}

and I need to make 1-1 function. meaning that every element in Set1 need to map to only one element in set2 My program gives me right answers BUT it is good only if there is only 3 elements in 1st set, what if there is 4 or 5? I need to create recursive function but I and not sure how.

Enter number of elements in sets(separated by space): 3 5   
Enter set A (separated by space): a b c
Enter set B (separated by space): 1 2 3 4 5
The 1-1 function has 60 elements.
{(a,1), (b,2), (c,3)}
{(a,1), (b,2), (c,4)}
{(a,1), (b,2), (c,5)}
{(a,1), (b,3), (c,2)}
{(a,1), (b,3), (c,4)}
{(a,1), (b,3), (c,5)}
.......
{(a,5), (b,2), (c,1)}
{(a,5), (b,2), (c,3)}
{(a,5), (b,2), (c,4)}
{(a,5), (b,3), (c,1)}
{(a,5), (b,3), (c,2)}
{(a,5), (b,3), (c,4)}
{(a,5), (b,4), (c,1)}
{(a,5), (b,4), (c,2)}
{(a,5), (b,4), (c,3)}

my function that does element distribution in c++

void getOneToOne(vector <string> &oneTone, vector <string> set1, vector <string> set2){

for (size_t i = 0; i < set2.size(); i++)
{
    for (size_t k = 0; k < set2.size(); k++)
    {
        if (k != i)
        {
        for (size_t j = 0; j < set2.size(); j++)
            {
                if (j != i && k != j)
                {
                    string temp;
                    temp = "{(" + set1[0] + "," + set2[i] + "), (" + set1[1] + "," + set2[k] + "), (" + set1[2] + "," + set2[j] + ")}";
                    oneTone.push_back(temp);
                }
            }
        }
    }

}

}

user117911
  • 79
  • 1
  • 6
  • What do you mean OFF and ON? – user117911 Apr 12 '15 at 05:58
  • :) funny, but that doesn't help, thanks for trying. – user117911 Apr 12 '15 at 06:02
  • I'm just kidding. Anyways I'm pretty sure you won't get a str8 answer here since people here, me included, don't solve homework questions. perhaps try to write your recursive function and if and when you get stuck, upload your code and people would assist you understand your problem – ZivS Apr 12 '15 at 06:03
  • if you have no idea how to start writing recursive methods, read this: http://stackoverflow.com/questions/717725/understanding-recursion – ZivS Apr 12 '15 at 06:06
  • that is my problem I am in need for help on converting my for loop into recursive function. Something I don't understand very well. – user117911 Apr 12 '15 at 06:07
  • Then write some code that ATTEMPTS to solve the problem recursively, and add that to your question. – Mats Petersson Apr 12 '15 at 06:08
  • @pashaUSA , if you want to learn how to write recursive functions, google "recursion ". If you help converting loop to recursion search "loop to recursion" (quick search:http://stackoverflow.com/questions/13369465/converting-a-loop-into-a-recursive-function) and so on... if you want help in this post, just edit your question and include some recursive function you wrote or can't understand – ZivS Apr 12 '15 at 06:11
  • Please decide for *one* language, there is no such language as C/C++. That said, I don't understand your goal. Also, your output, is that the correct output or the faulty one? – Ulrich Eckhardt Apr 12 '15 at 06:49

2 Answers2

0

simple recursion. you can change n1 and n2 as per your needs. Read this Link

#include<iostream>
#include<string>
using namespace std;
void swap(char *a, char *b){char c = *a;*a=*b;*b=c;}
void permute( char *a, char *b, int i, int n1, int n2)
{
    if ( i == n1) 
    {
        cout << "{";
        for( int k=0; k<n1; k++)
        cout << "("<< a[k] << "," << b[k]<<")";
        cout <<"}"<< endl;
    }
    else
    {
        for( int j=i; j < n2; j++) {
            swap( (b+i), (b+j) );
            permute(a,b,i+1,n1,n2);
            swap( (b+i), (b+j) );
        }
    }
}
int main()
{
    char a[] = "abc";
    char b[] = "12345";
    permute(a,b,0,3,5);
    return 0;
}
Cereal_Killer
  • 304
  • 2
  • 13
0

Big thanks to "Cereal_Killer", everything works out now. I had to change from char to String

#include<iostream>
#include<string>
using namespace std;
void swap(string *a, string *b)
{
string c = *a;
*a = *b; 
*b = c; }
void permute(string *a, string *b, int i, int n1, int n2)
{
    if (i == n1)
    {
        cout << "{";
        for (int k = 0; k<n1; k++)
            cout << "(" << a[k] << "," << b[k] << ")";
        cout << "}" << endl;
    }
    else
    {
        for (int j = i; j < n2; j++) {
            swap((b + i), (b + j));
            permute(a, b, i + 1, n1, n2);
            swap((b + i), (b + j));
        }
    }
}
int main()
{
    string a[] = { "a", "b", "c" };
    string b[] = { "1", "2", "3", "4","5" };
    permute(a, b, 0, 3, 5);
    return 0;
}
user117911
  • 79
  • 1
  • 6