Thread: Need help on making an array in a loop

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    1

    Need help on making an array in a loop

    here's my code
    Code:
    #include<stdio.h>
    int main()
    {
        int setA[1000],setB[1000],a,b,n=1,m=1,i1,i2;   
        printf("Enter size of set A: ");
        scanf("%d",&a);   
        for (i1=0;i1<a;i1++)
        {
            printf("Enter A%d: ",n);
            scanf("%d",&setA[i1]);
            n++;
        }
        printf("Enter size of set B: ");
        scanf("%d",&b);
        for (i2=0;i2<b;i2++)
        {
            printf("Enter B%d: ",m);
            scanf("%d",&setB[i2]);
            m++;
        }
        printf("\n{");
        for(i1=0;i1<a;i1++)
        {
            if (i1 != a-1)
            printf("%d,",setA[i1]);
            else if (i1 == a-1)
            printf("%d",setA[i1]);
        }
        printf("} x {");
        for(i2=0;i2<b;i2++)
        {
            if (i2 != b-1)
            printf("%d,",setB[i2]);
            else if (i2 == b-1)
            printf("%d",setB[i2]);
        }
        printf("} = {");
        for(i1=0;i1<a;i1++)
        {
            for(i2=0;i2<b;i2++)
            {
                if ((i1 != a-1)||(i2 != b-1))
                {
                    printf("(%d,%d),",setA[i1],setB[i2]);
                }
                else if ((i1 == a-1)&&(i2 == b-1))
                {
                    printf("(%d,%d)",setA[i1],setB[i2]);
                }
            }
        }
        printf("}\n");
        printf("The intersection/s of sets {");
        for(i1=0;i1<a;i1++)
        {
            if (i1 != a-1)
            printf("%d,",setA[i1]);
            else if (i1 == a-1)
            printf("%d",setA[i1]);
        }
        printf("} and {");
        for(i2=0;i2<b;i2++)
        {
            if (i2 != b-1)
            printf("%d,",setB[i2]);
            else if (i2 == b-1)
            printf("%d",setB[i2]);
        }
        printf("} is/are {");
        for(i1=0;i1<a;i1++)
        {
            for(i2=0;i2<b;i2++)
            {
                if (setA[i1]==setB[i2])
                {
                    printf("%d,",setA[i1]);
                   //instead of using printf here I wanted to make it an array 
                }
            }
        }
        printf("}\n");
        return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yes, and.... ???

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    This sounds like one of the "Can i haz" posts.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    What they said.

    Anyway, what is this supposed to do, print setA[i1] whether i1 is or isn't equal to a-1? Seems funny...
    Code:
            if (i1 != a-1)
            printf("%d,",setA[i1]);
            else if (i1 == a-1)
            printf("%d",setA[i1]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-29-2010, 11:11 AM
  2. Need alittle help making this loop
    By Daesom in forum C++ Programming
    Replies: 3
    Last Post: 10-31-2006, 01:41 PM
  3. Making an array with x and y
    By |Wiz| in forum C++ Programming
    Replies: 14
    Last Post: 03-06-2006, 09:15 PM
  4. making an empty 2-D array
    By starX in forum C Programming
    Replies: 4
    Last Post: 02-08-2002, 01:09 AM
  5. making an array name a variable
    By Zaarin in forum C++ Programming
    Replies: 5
    Last Post: 09-02-2001, 06:17 AM