Help with a problem Array

This is a discussion on Help with a problem Array within the C Programming forums, part of the General Programming Boards category; I have 2 constant integer arrays. I am computing their union, difference, and intersection. I cannot figure out why it ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    12

    Help with a problem Array

    I have 2 constant integer arrays. I am computing their union, difference, and intersection. I cannot figure out why it is outputting 1 in the intersection and not in the difference. 1 Should be in the difference, not the intersection.

    Further, at the end of the program I have it run Element_set for set2[0] and it returns 0, like it should, and to make sure set2[0] is 2, I have it print that as well. Yet, in my output above, it still has the same problem.

    My out put should look like this:

    Set1: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
    Set2: 2, 4, 7, 9, 11
    Union of 1&2: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
    Difference of 1&2: 1, 3, 5, 6, 8, 10
    Intersection of 1&2: 2, 4, 7, 9
    0 2

    Any help appreciated

    Code:
    #include <stdio.h>
    
    void Set_print(int Set[], int n);			/* Declares function Set_print with parameters int Set[] and int n[] */
    void Set_print(int Set[], int n)
    	{
    		int i=n+1;				/* prints all the n values of the array Set[n] */
    		for( n = 0; n < i ; n++)
    		printf(" %d", Set[n]);
    	}	
    
    int Element_set(int Set[], int n, int value);		/* Declares function Element_set with parameters int Set[], int n, */
    int Element_set(int Set[], int n, int value)		/* and int value 						   */
    	{	
    		int i;
    		i=n+1;					/* Checks n-elements of Set[n] for value and if value is in Set[]  */
    		 for(n=0; n<i; n++)			/* it returns 1, if value is not in Set[] it returns 0		   */
    			{
    				if(value == Set[n])
    					return 1; 
    			}
    		 	if(value != Set[i-1])
    				return 0;
    			
    	}
    
    int  Set_union ( int Set1[], int Set2[], int Set3[], int n1, int n2 );	/* Declares function Set_union with parameters */
    int  Set_union ( int Set1[], int Set2[], int Set3[], int n1, int n2 )	/* int Set1[], int Set2[], int Set3[], int n1, */
    	{								/* and int n2, n1 and n2 the elements of Set1  */
    		int value, set1, set2;					/* and Set2, respectively		       */
    		int n3=0;
    		for(value=1; value<12; value++)
    		{							/* Checks Set1[n1] and Set2[n2] to see if they */
    			set1 = Element_set(Set1, n1, value);		/* contain value.  If set1 or set2 contains    */
    			set2 = Element_set(Set2, n2, value);		/* value, then element n3 of Set3 is set to    */
    			if(set1 == 1 || set2 == 1)			/* value, and the element number of set3 is    */
    				{					/* returned				       */
    					Set3[n3] = value;
    					n3++;
    				}
    		}
    		return n3;
    	}
    			
    int Set_differ ( int Set1[], int Set2[], int Set3[], int n1, int n2);	/* Declares function Set_differ with parameters */
    int Set_differ ( int Set1[], int Set2[], int Set3[], int n1, int n2)	/* int Set1[], int Set2[], int Set3[], int n1,  */
    	{								/* int n2, n1 and n2 the elements of Set1 and   */
    		int value, set1, set2=0;					/* Set2, respectively				*/
    		int n3;
    		for(value=1; value<12; value++)
    		{							/* Checks Set1[n1] and Set2[n2] to see if they */
    			set1 = Element_set(Set1, n1, value);		/* contain value.  If set1 contains value AND  */
    			set2 = Element_set(Set2, n2, value);		/* set2 does NOT contain value, then element   */
    			if(set1 == 1 && set2 == 0)			/* n3 of Set3 is set to value, and the element */
    				{					/* number of set3 is returned 		       */
    					Set3[n3] = value;
    					n3++;
    				}
    		}
    		return n3;
    	}
    
    int Set_inter ( int Set1[], int Set2[], int Set3[], int n1, int n2);	/* Declares function Set_inter with parameters */
    int Set_inter ( int Set1[], int Set2[], int Set3[], int n1, int n2)	/* int Set1[], int Set2[], int Set3[], int n1  */
    	{								/* and int n2.  n1 and n2 the elements of Set1 */
    		int value, set1, set2;						/* and Set2, respectively		       */
    		int n3=0;
    		for(value=1; value<12; value++)					/* Checks Set1[n1] and Set2[n2] to see if they */
    		{							/* contain value.  If set1 AND set2 BOTH       */
    			set1 = Element_set(Set1, n1, value);			/* contain value, then element n3 of set3 is   */
    			set2 = Element_set(Set2, n2, value);			/* set to value, and the element number of set3*/
    			if(set1 == 1 && set2 == 1 )				/* is returned.				       */
    				{
    					Set3[n3] = value;
    					n3++;
    				}
    		}
    		return n3;
    	}
    
    main()
    
     {
    	int n1, n2,n3,sz3, l;
    	int set1[10] = {1,2,3,4,5,6,7,8,9,10};
    	int set2[5] = {2,4,7,9,11};
    	int set3[11]={0};
    	
    	printf("\nSet1\t");	
    	Set_print(set1, 9);
    	printf("\n\nSet2\t");
    	Set_print(set2, 4);
    	
    	for(n1=0, n2=0; n1<10 || n2<5; n1++, n2++)
    		{
    			sz3 = Set_union(set1, set2, set3, n1, n2);
    		}
    	printf("\n\nSet3\tUnion of 1&2\t", &Set_union);
    	Set_print(set3, sz3-1);
    
    
    	for(n1=0, n2=0; n1<10 || n2<5 ; n1++, n2++)
    		{
    			sz3 = Set_differ(set1, set2, set3, n1, n2);
    		}
    	printf("\n\nSet3\tDifference of 1&2\t");
    	Set_print(set3, sz3-1);
    
    	for(n1=0, n2=0; n1<10 || n2<5 ; n1++, n2++)
    		{
    			sz3 = Set_inter(set1, set2, set3, n1, n2);
    		}
    	printf("\n\nSet3\tIntersection of 1&2\t", &Set_inter);
    	Set_print(set3, sz3-1);
    	printf("\n");
    
    	printf("\n%d", Element_set(set2, 4, 1));
    	printf(" %d", set2[0]);
    	
     }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    You don't need the for loops in main (which returns an int by the way).
    Code:
    n3 = Set_union(set1, set2, set3, 10, 5, 11);
    This can do just fine by itself. There are also some unused parameters to printfs in main.

    I find the following to be a little easier to read.
    Code:
    int Element_set(const int Set[], int n, int value)
    {
       int i;
       for ( i = 0; i < n; ++i )
       {
          if ( value == Set[i] )
          {
             return 1;
          }
       }
       return 0;
    }
    I recommend rewriting the other loops that begin at one in this manner as well.

    Set_differ does not initialize n3.


    I think I'm leaving some things out, but my working copy has a number of my preferences here and there. I'll leave you to take another pass at it. After the loops in main are gone, the incorrect output was a little easier to debug.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Hmm, this looks a little like the post
    Help with Array
    by someone named webbizdesign, right down to the bad structure and design.

    What gives, guys? Did the instructor give out a program for the class to correct and you guys aren't being forthcoming about the realities of the assignment?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    12
    No, I have different log-ons for different locations. That's my code, not an instructors, and its my bug I'm trying to fix.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by cheeves
    No, I have different log-ons for different locations. That's my code, not an instructors, and its my bug I'm trying to fix.
    Then continue with the same thread (and same user ID) please.

    And if you are having a problem, implement the help that was already given before posting the same question without attempting that suggestion. You were given the answer -- I tested it myself and it worked.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 10:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM

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