intersection and union

This is a discussion on intersection and union within the C Programming forums, part of the General Programming Boards category; HI.... part1 > Let the universal set be { x: 0<=x<=31 }, a program to read 2 sets and cumpute ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    1

    intersection and union

    HI....

    part1 >

    Let the universal set be { x: 0<=x<=31 }, a program to read 2 sets and cumpute their
    union and intersection is to be writen.
    you are asked to write 2 versions of this program

    Version 1 will use an array to store the elements of a set (repetition of an element is not allowed).

    Version 2 will use an array of flags, such that a[i] is 1 if i belongs to the set and 0 otherwuse.

    the user unterface for both versions should be the same
    moreover, the main() in both versions shoild look like :
    [code]int main (){ .......
    [code]readSet(............) ; //to read the first set
    [code] readSet(............) ; //to read the second set
    [code]displayset(......) ; //to display their union
    [code]displayset(......) ; //to display their intersection
    [code]displayset(......) ; //you may add printf statments the main()
    [code]}


    this is my project i didnt untersatnd the difrent from the 2 versions and please can u tell me how to do them
    please help me
    by the way when I want to make a readSet function that what i got

    [code]#include <stdio.h>
    [code]void ReadSet(int s[] , int *x )
    [code]{
    [code] int k=0 ;
    [code] printf ("enter the numbers , -1 this mean u stop \n") ;
    [code] scanf ("%d" , &x) ;
    [code] if(x>0 && 31>x)
    [code] {
    [code] while (x != -1)
    [code] {
    [code] s[k] = x ;
    [code] k++ ;
    [code] scanf ("%d" , &x) ;
    [code] }
    [code] }
    [code]}
    [code]
    [code]int main ()
    [code]{
    [code] int n1 , s1[99] ;
    [code] n1 = ReadSet(s1[] , &n1) ; ((((error))))
    [code] printf ("%d",n1) ;
    [code] return 0 ;
    [code]}

    Compiling...
    project1.c
    C:\Documents and Settings\Administrator\Desktop\projec1\project1.c( 8) : warning C4047: '>' : 'const int ' differs in levels of indirection from 'int *'
    C:\Documents and Settings\Administrator\Desktop\projec1\project1.c( 10) : warning C4047: '!=' : 'int *' differs in levels of indirection from 'const int '
    C:\Documents and Settings\Administrator\Desktop\projec1\project1.c( 12) : warning C4047: '=' : 'int ' differs in levels of indirection from 'int *'
    C:\Documents and Settings\Administrator\Desktop\projec1\project1.c( 22) : error C2059: syntax error : ']'
    Error executing cl.exe.

    project1.exe - 1 error(s), 3 warning(s)




    thanks for reading all this

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The first problem is in your read function; scanf uses pointers, so this:
    Code:
    scanf ("%d" , &x) ;
    Would be this instead:
    Code:
    scanf ("%d" , x) ;
    Because x is already a pointer. If it were not a pointer, you'd use the address of operator as you did. Since it is a pointer, you just use the variable name itself.

    You have a similar problem where you're getting your error. When passing an array to a function, like so:
    Code:
    int n1 , s1[99] ;
    n1 = ReadSet(s1[], &n1) ;
    You just use the name of the array, like this:
    Code:
    n1 = ReadSet( s1, &n1 );
    Finally, code tags for the forum are simple to use once you get the hang of it. It goes like this:
    [code]
    ...your code goes in here...
    [/code]
    You just put the starting [code] before all of your code, and the closing [/code] after all of your code.

    [edit]
    It sounds like they want two similar concepts, but done the opposite of eachother:
    Code:
    int array1[SOMESIZE];
    
    array1[x] = some_set_member;
    Where 'x' is the next spot free in the array, and you actually put the value of the set number in it.

    And the second, I'm guessing would be:
    Code:
    int array2[SOMESIZE];
    
    array2[some_set_member] = 1;
    Where the set number itself is the position in the array, instead of storing the set number in the array. That is to say, if the number 5 was a member of a set, you'd make "array2[5] = 1;" to show that 5 was a member.

    That's my guess anyway.
    [/edit]

    Quzah.
    Last edited by quzah; 02-28-2004 at 05:20 AM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    19
    void ReadSet(int s[] , int *x ) {
    ...
    }

    ...
    n1 = ReadSet(s1[] , &n1);
    another mistake. you're return typ of ReadSet is void, and in main you specify an integer to it. just do the following:

    Code:
    ReadSets(s1 , &n1);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. optimising program
    By SONU in forum C Programming
    Replies: 1
    Last Post: 05-18-2008, 10:28 AM
  2. how to compute set intersection efficiently?
    By George2 in forum C Programming
    Replies: 4
    Last Post: 06-02-2006, 09:06 AM
  3. union of two sets
    By axon in forum C++ Programming
    Replies: 1
    Last Post: 12-04-2003, 02:18 PM
  4. Help with Array
    By webbizdesign in forum C Programming
    Replies: 3
    Last Post: 10-11-2003, 12:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21