Click here to Skip to main content
15,921,454 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
#include<stdio.h>
#include<stdlib.h>
int *add(int *p,int a,int *q,int b);

void sort(int *p,int a);
int tem=0;  /* tem is the count of the total elements without repetition*/
int main()
{
    int *p,*q,a,b,i,*r;
    printf("enter the number of elements in the first array\n");
    scanf("%d",&a);
    p=(int *) malloc(a*sizeof(int));

    printf("enter the number of elements in the second array\n");
    scanf("%d",&b);
    q=(int *) malloc(b*sizeof(int));
    printf("enter the elements in the first array\n");
    for(i=0;i<a;i++)
    {
         printf("enter the %d element\n",i+1);
         scanf("%d",p+i);
    }
    printf("enter the elements in the second array\n");
    for(i=0;i<b;i++)
    {
        printf("enter the %d element\n",i+1);
        scanf("%d",q+i);
    }
    sort(p,a);sort(q,b);
    
    r= add(p,a,q,b);
    sort(r,tem);
    printf("the sorted and combined list without repetitions is\n");
    for(i=0;i<tem;i++)
    {
        printf("%d\n",*(r+i));
    }
    return 0;
}
void sort(int *p,int a)
{
    int i,j,temp;
    for(i=0;i<a;i++)
    {
        for(j=i;j<a;j++)
        {
            if(*(p+i)>*(p+j))
            {
                 temp=*(p+i);
                 *(p+i)=*(p+j);
                 *(p+j)=temp;
            }
        }
    }
}
int *add(int *p,int a,int *q,int b)
{
    int i,j,k;
    int *r;
    int z,count=a+b;/* initialized count as the total number of elements
                       in both including repetitions*/

    r=(int *) malloc(count*sizeof(int));

    for(j=0;j<a;j++)
    {
        for(k=0;k<=tem;k++)
        {
            if(k==tem&&tem!=0)
                break;
            if(*(r+k)!=*(p+j))
            {
                *(r+tem)=*(p+j);
                tem++;
            }
        }
    }
    for(j=0;j<b;j++)
    {
        for(k=0;k<tem;k++)
        {
            if(*(r+k)!=*(q+j))
                *(r+tem)=*(q+j);
            tem++;
        }/*at last tem will be equal to count.*/
    }
    return r;
}
Posted
Updated 20-Jul-18 9:52am
v12
Comments
Richard MacCutchan 14-Jun-15 3:09am    

if(*(r+k)!=*(p+j))
*(r+tem)=*(p+j);

How do you expect anyone (even you in a few weeks) to understand such obfuscated statements? Use readable variable names and comments to make your code clear.
Member 11324568 14-Jun-15 7:07am    
sir,i,j,k are the general variables used for running the loop , while p,q are the pointers to the first element of two arrays.I have used all the variables of a particular kind as continuous alphabets to avoid confusion.
[no name] 14-Jun-15 9:17am    
Variables should have meaningful names otherwise nobody even you can understand the code. Why are you in this situation - think about it? Why would anyone waste valuable time looking at this puzzle? Rewrite it clearly and it may just work.
Member 11324568 14-Jun-15 9:20am    
Alright ,i will use meaningful variables,try to help me find out the error here.
[no name] 14-Jun-15 9:22am    
In the world I live in people debug their own code. That is all you are asking. Try harder.

First of all, you should not SHOUT.

Then, you should explain what is your problem.

Anyway, the following line is probably wrong:
C++
if(*(p+i)==*(p+j))

You are comparing item from first array with item from the same array.

Also, it seems that when this line is executed, tem is not yet initialized to the desired value (but still 0):
C++
for(k=0;k<tem;k++)>

Given that, that loop is never executed. Same problem a few lines below. You are probably using the wrong variables at some places in those loops.

Without looking further and without even trying your program, it is clear that it will not work as intended.

Then, your program is far from optimal. You allocated r at 2 places. The one in main is useless.

Also, if you sort both array, you don't have to compare each item in each array but you can add the minimum item form the current location in each array and you won,t need to sort the result once more at the end.

Alternatively, it you know that original arrays don't have duplicated or you want to removed duplicate, you might make a big array and sort it once then remove duplicates either in place or into a new array.
 
Share this answer
 
Comments
Member 11324568 14-Jun-15 7:24am    
sir, code has been updated,yet it doesn't work, after i run the code and after i give all the details as input i get on the display. its not working...what to do now?
Philippe Mori 14-Jun-15 8:44am    
It seems that you have only considered the first two points.

You should use a debugger to understand your own code although in this case, it is not that hard to figure out that the code is still incorrect. In particular tem is only incremented if it is not zero since loop only executed in that case!

By the way, write your code one funcion at a time. Start with 2 arrays already sorted and no duplicate and element in second arrays all greater than those in first one. Make appending works in that case, then add another case like unsorted array, then overlapping range and finally duplicates.
Member 11324568 14-Jun-15 9:21am    
Yes sir ,the third method you told me to do is very efficient compared to what i had written,but i'm just curious to know what mistake i have made so that i can avoid that sort of mistake,and i'm just a beginner in c programming.
Andreas Gieriet 14-Jun-15 9:49am    
Use qsort from stdlib.h instead of re-inventing sorting.
Don't use global variables in functions (like you use tem in the add function, etc.).
Try run the code with paper and pencil (or run it in the debugger...) to identify what control flow or data which is not as expected.
Cheers
Andi
Member 11324568 14-Jun-15 10:46am    
thanks :) i didn't know about qsort..and i started watching videos on how to use debugger..
#include<stdio.h>
int main()
{
    int a[30],b[30];
    int n,k,i,j,p,c=0;
    printf("How many element in first array\n");
    scanf("%d",&n);
    printf("Enter elements");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }


     printf("How many element in sceond array\n");
    scanf("%d",&k);
    printf("Enter elements");
    for(i=0;i<k;i++)
    {
        scanf("%d",&b[i]);
    }
     printf("The new array is \n");
     for(i=n,j=0;i<(n+k);i++)
     {
         a[i]=b[j++];
     }

     for(i=0;i<(n+k);i++)
     {
         printf("%d ",a[i]);
     }
    for(i=0,p=0;i<(n+k);i++)
    {
        if(a[i]!=NULL)
        {

            for(j=i+1;j<(n+k);j++)
            {
                if(a[i]==a[j])
                {
                    a[j]=NULL;
                }
            }
            a[p++]=a[i];
            c++;
        }


    }
printf("\nAfter eliminate the dublicate elements,the new arry is : \n");
for(i=0;i<c;i++)
     {
         printf("%d ",a[i]);
     }

}
 
Share this answer
 
Comments
jeron1 20-Jul-18 16:04pm    
Dumping code for a question over three years old, with no corresponding explanations is maybe not the best thing to do. What happens if I enter 80 for the number of elements?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900