What am I missing? (Arrays/Sets)

This is a discussion on What am I missing? (Arrays/Sets) within the C Programming forums, part of the General Programming Boards category; I am trying to write a program that has a universal array and an array A and B. I need ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    New Mexico
    Posts
    10

    Question What am I missing? (Arrays/Sets)

    I am trying to write a program that has a universal array and an array A and B. I need to find the union and intersection of A and B, but for now I am just working on the union. Below is the code I have written so far. It compiles with no errors but doesn't print the union of A and B. Can somebody please help me?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int U[20], A[20], B[20], U1[20]; // U1 should stores the elemnts that are the result of A U B.
        int i, j, flag;
        int lengthU, lengthA, lengthB, lengthU1;
        lengthU = 0;
        lengthA = 0;
        lengthB = 0;
    
        printf("\n Enter no more than 20 elements (Integer Between -100 and 100) for the universal set. \n");
        for (i = 0; i < 20; i++)
        {
           printf("Please enter a number between -100 and 100\n");
           scanf("%i", &U[i]);
           if((U[i] < -100) || (U[i] > 100))
           {
               break;
           }
           else
           {
               lengthU = lengthU + 1;
           }
        }
    
    
         printf("Elements of the universal set you entered:\n");
         for (i = 0; i < lengthU; i++)
         {
            printf("\t%d", U[i]);
         }
    
    
    
         printf("\n\n Enter no more than 20 elements (Integer Between -100 and 100) for set A.  \n");
        for (i = 0; i < 20; i++)
        {
           printf("Please enter a number between -100 and 100\n");
           scanf("%i", &A[i]);
           if((A[i] < -100) || (A[i] > 100))
           {
               break;
           }
           else
           {
               lengthA = lengthA + 1;
           }
        }
    
    
         printf("Elements of the set A you entered:\n");
         for (i = 0; i < lengthA; i++)
         {
            printf("\t%d", A[i]);
         }
    
    
    
          printf("\n\n Enter no more than 20 elements (Integer Between -100 and 100) for set B. \n");
        for (i = 0; i < 20; i++)
        {
           printf("Please enter a number between -100 and 100\n");
           scanf("%i", &B[i]);
           if((B[i] < -100) || (B[i] > 100))
           {
               break;
           }
           else
           {
               lengthB = lengthB + 1;
           }
        }
    
    
         printf("Elements of set B you entered:\n");
         for (i = 0; i < lengthB; i++)
         {
            printf("\t%d", B[i]);
         }
    
    
         for(i=0; i<lengthA; i++)
           {
               U1[i] = A[i];
           }
    
            lengthU1 = lengthA;
    
          for(i=0; i<lengthB; i++)
            {
                flag = 0;
                j=0;
    
                while ((j<lengthU1) && (flag==0))
                {
                    if (B[i]=U1[j])
                    {
                        flag = 1;
                    }
                     j = j+1;
    
                }
    
                if(flag==0)
                {
                    U1[lengthU1]=B[i];
                    lengthU1=lengthU1 + 1;
                }
            }
    
    
           printf("\n\n The Union of A and B is: ");
           printf ("\t%d", U1[i]);
    
    
         return 0;
     }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
      if (B[i]=U1[j])
    = assigns a value
    == tests for equality


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Location
    New Mexico
    Posts
    10
    Quote Originally Posted by quzah View Post
    Code:
      if (B[i]=U1[j])
    = assigns a value
    == tests for equality


    Quzah.

    Did I only make that mistake in that one line of code or throughout the whole program? When I use == on this line of code it still doesn't print out the union of both arrays. Thanks for your help!!!!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    Well how about creating some functions, rather than several hundred lines of rambling main?

    Eg.
    Code:
    // add suitable parameters
    int inputSet ( ) {
        printf("\n Enter no more than 20 elements (Integer Between -100 and 100) for the universal set. \n");
        for (i = 0; i < 20; i++)
        {
           printf("Please enter a number between -100 and 100\n");
           scanf("%i", &U[i]);
           if((U[i] < -100) || (U[i] > 100))
           {
               break;
           }
           else
           {
               lengthU = lengthU + 1;
           }
        }
    }
    Which you could call from main with
    Code:
    lengthU = inputSet( U, 20 );
    lengthA = inputSet( A, 20 );
    lengthB = inputSet( B, 20 );
    Another function to define would be printSet() to print a given set.

    Finally, write a function called isMemberOf(), which takes a set, a length, and a value, and tells you whether the value is already a member of the set.
    This would replace the "while ((j<lengthU1) && (flag==0))" loop in your code.

    Then your pseudo code would read something like "for all of set a, if not isMemberOf() then add to set"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Map of sets?
    By Orange Lozenge in forum C++ Programming
    Replies: 6
    Last Post: 01-23-2011, 10:16 AM
  2. Am I missing something? (2d arrays)
    By k2712 in forum C Programming
    Replies: 2
    Last Post: 09-23-2007, 04:10 PM
  3. ATD: C++ Sets
    By stalker in forum C++ Programming
    Replies: 9
    Last Post: 11-09-2004, 05:40 AM
  4. Need help on Character Sets
    By vsriharsha in forum Linux Programming
    Replies: 1
    Last Post: 06-08-2004, 01:40 AM
  5. Problem with My Sets ADT
    By jawwadalam in forum C++ Programming
    Replies: 2
    Last Post: 10-04-2002, 06:36 AM

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