Results 1 to 2 of 2

Thread: Array Problem

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2002
    Posts
    16

    Question Array Problem

    I want to compare 2 arrays and if the element is common to both the arays then store it in the third aray and then print the common elements from 3rd array.But I am not understanding the correct way of its implementation.Here's what I have tried to do :

    #include <stdio.h>

    main()
    {
    int x,y,e;
    int i,j;
    int a[20];
    int c[20];
    int b[20];
    printf("How many no of elements : ");
    scanf("%d",&x);
    printf("Enter the elements of set A");
    for(i=0;i<x;i++)
    {
    scanf("%d",&a[i]);
    }
    printf("Enter the elements of set B");
    for(j=0;j<x;j++)
    {
    scanf("%d",&b[i]);
    }

    // How do I use the assignment operation ?

    for(i=0;i<x-1;i++) {
    for(j=i+1;j<x;j++)
    if(a[j]==b[i])

    c[y++]=a[j]; //Is this right?
    }

    //How Do I print the 3rd array ?

    return 0;
    }

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    This code seems correct to me (I didn't look thoroughly).
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width