sets

ghjkhjkhjk
Last edited on
Yes. I know how to do it :P Do you know how to do it?

First off, do you know what symmetric difference is?

If set_1 is {a, b, c, d, e} and set_2 is {c, d, e, f, g},
what is the symmetric difference of set_1 and set_2?
Last edited on
yes..
symmetric difference is:{a,b,f,g}
I have an idea in doing this but i don't know what is the best code that fits this conditions.
Last edited on
Cool. Now, tell me how the sets are represented in your code.
Do you just use char arrays? Should the code look like this:

1
2
3
4
5
char set_1[10] = "abcde";
char set_2[10] = "cdefg";
char sym_dif[10];

//... 
?

EDIT: Tell me your idea.
Last edited on
yes..
my idea is just like this...
for the elements I AM GOING TO remove repeated strings.., it goes like this: string:aaaghjjfg and the result is:aghj then after removing I Am going to merge all sets and then remove duplicates...
Okay.

There are a couple of things I want to point
out and a couple of questions I want to ask.

If you remove duplicates here -> aaaghjjfg,
you'll get this -> aghjfg, not this -> aghjf
(there is an extra 'g' on the former).

If you want to get this -> aghjf, you'll
have to sort your initial string first.

gujinni wrote:
I Am going to merge all sets and then remove duplicates

But this won't give you the symmetric difference. It will give you the union.

What compiler do you use? Are you allowed to use any built - in
functions or classes or do you have to write everything yourself?

For example, are you allowed to use qsort for sorting?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdlib.h>
#include <stdio.h>

int compare(const void * left, const void * right)
{
    return *(char *)left - *(char *)right;
}

int main()
{
    char set_1[] = "cdeba";
    char set_2[] = "fgdec";

    qsort(set_1, 5, sizeof(char), compare);
    qsort(set_2, 5, sizeof(char), compare);

    printf("%s\n", set_1);
    printf("%s\n", set_2);

    return 0;
}

Also, are you allowed to use std::strings instead of char arrays?
Just suppose that the sets are already sorted and don't have repeated elements (because they are sets).

gujinni wrote:
I Am going to merge all sets and then remove duplicates
Indeed that does work. (explain it to m4ster r0shi)
You could avoid the repeated values when doing the merge.
ne555 wrote:
Indeed that does work.

Well, it really depends on what the OP means by saying "remove duplicates".
Using the conventional meaning for "remove duplicates" will give you the union.
Last edited on
vfghfgh
Last edited on
xjdfhkzdhgffdh
fgkjxcfgnkxcjhkjgngvjxchvmxx
fg
xdf

gf
dfhfghfgh
dfgh
fghdfghfgh
Last edited on
gfhfgh
Last edited on
Assuming that your sets (char arrays) are already sorted,
you can use these algorithms to perform set operations:

http://www.cplusplus.com/reference/algorithm/set_union/
http://www.cplusplus.com/reference/algorithm/set_intersection/
http://www.cplusplus.com/reference/algorithm/set_difference/
http://www.cplusplus.com/reference/algorithm/set_symmetric_difference/

http://www.cplusplus.com/reference/algorithm/copy/

Just replace InputIterators with const char *, and OutputIterator with char *.

Also, make sure your result array is big enough.
tnx
By the way what is the use of const char*???
char * means pointer to char. That's
how you usually pass arrays to functions.

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
#include <iostream>

using namespace std;

void build(char * array, int size)
{
    int i;

    for (i = 0; i < size; ++i)
        array[i] = 'a' + i;
}

int main()
{
    char my_array[20];

    build(my_array, 7);

    int i;

    for (i = 0; i < 7; ++i)
        cout << my_array[i] << endl;

    return 0;
}

const char * means pointer to const char. This means
that you are not allowed to use the pointer to modify the
char pointed to by it. If you try, your compiler will complain.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main()
{
    char my_array[20] = "abcdefg";

    char       *  ptr = &my_array[0];
    const char * cptr = &my_array[0];

    cout << *ptr  << endl; // ok
    cout << *cptr << endl; // ok

    *ptr  = 'b'; // ok
    *cptr = 'b'; // error

    return 0;
}

That's how you usually pass arrays to functions
when you don't intend to modify them (the arrays).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

void print(const char * array, int size)
{
    int i;

    for (i = 0; i < size; ++i)
        cout << array[i] << " ";

    cout << endl;
}

int main()
{
    char my_array[20] = "abcdefg";

    print(my_array, 3);

    return 0;
}

More info on pointers -> http://cplusplus.com/doc/tutorial/pointers/
Last edited on
tnx for your help.
Topic archived. No new replies allowed.