用于打印数组唯一编号的C代码

时间:2015-12-09 14:32:33

标签: c

我想创建一个C程序,它合并4个未排序的数组,对它们进行排序并打印新合并数组中的所有唯一数字。 结果不正确,但我不知道为什么。 这是一个例子:

  1. 一个[1,5,13,​​3]
  2. B [3,5,9,1,23]
  3. C [5,23,3,18]
  4. d [1]
  5. A_B [1,5,13,​​3,3,5,9,1,23]
  6. C_D [5,23,3,18,1]
  7. E [1,1,1,3,3,3,5,5,5,9,13,18,23,23]
  8. 仅打印数组e中的唯一数字: 13,18,9

    #include <stdio.h>
    #include <stdlib.h>
    
    void print_uniques(int x[], int sizex)
    {
    int i, j;
    if(sizex == 0)
        return;
    if(sizex == 1){
        printf("%d\n", x[0]);
        return;
    }
    i = j = 0;
    while(i != sizex){
        i++;
        if(i != sizex && x[i] == x[j])
            continue;
        if((i - j) == 1)
            printf("%d\n", x[j]);
        j = i;
    }
    }
    
    /* print unique numbers from sorted array */
    
    /* merge two already sorted arrays */
    void merge(int m[], int x[], int sizex, int y[], int sizey)
    {
    int i=0, j=0, k=0;
    while (i < sizex && j < sizey)
    {
        if (x[i] <= y[j])
            m[k++] = x[i++];
        else
            m[k++] = y[j++];
    }
    while (i < sizex)
        m[k++] = x[i++];
    while (j < sizey)
        m[k++] = y[j++];
    }
    
    /* compare function for qsort() */
    int cmp(const int *i, const int *j)
    {
    return *i - *j;
    } 
    
    int main()
    {
    int i, j,a[30],b[30],c[30],d[30],sizea,sizeb,sizec,sized;
    printf("Enter the size of array A: ");
    scanf("%d",&sizea);
    printf("Enter the %d elements of array A:\n", sizea);
    for(i=0;i<sizea;i++)
    scanf("%d",&a[i]);
    printf("Enter the size of array B: ");
    scanf("%d",&sizeb);
    printf("Enter the %d elements of array B:\n", sizeb);
    for(i=0;i<sizeb;i++)
    scanf("%d",&b[i]);
    printf("Enter the size of array C: ");
    scanf("%d",&sizec);
    printf("Eisagete ta %d stoixeia tou pinaka C:\n", sizec);
    for(i=0;i<sizec;i++)
    scanf("%d",&c[i]);
    printf("Eisagete to mege8os tou pinaka D: ");
    scanf("%d",&sized);
    printf("Eisagete ta %d stoixeia tou pinaka D:\n", sized);
    for(i=0;i<sized;i++)
    scanf("%d",&d[i]);
    int a_b[32];
    int c_d[32];
    int e[64];
    int sizea_b, sizec_d, sizee;
    
    sizea = sizeof(a)/sizeof(a[0]);
    sizeb = sizeof(b)/sizeof(b[0]);
    sizec = sizeof(c)/sizeof(c[0]);
    sized = sizeof(d)/sizeof(d[0]);
    sizea_b = sizea   + sizeb;
    sizec_d = sizec   + sized;
    sizee   = sizea_b + sizec_d;
    
    /* sort the arrays */
    qsort(a, sizea, sizeof(a[0]), cmp);
    qsort(b, sizeb, sizeof(b[0]), cmp);
    qsort(c, sizec, sizeof(c[0]), cmp);
    qsort(d, sized, sizeof(d[0]), cmp);
    
    /* merge the arrays */
    merge(a_b, a, sizea, b, sizeb);
    merge(c_d, c, sizec, d, sized);
    merge(e, a_b, sizea_b, c_d, sizec_d);
    
    print_uniques(e, sizee);
    
    return 0;
    }
    

    感谢任何帮助!

3 个答案:

答案 0 :(得分:0)

a_bc_d合并后,您可以使用

将尺寸放回到前面
while(i<size3+size4 && j<size1+size2)

这应该是

while(i<size1+size2 && j<size3+size4)

同样适用于合并数组的其余部分,这些还有一个问题:不是重复用于构建whilea_b的{​​{1}}循环,而是#39; ve使用了不同的技术。我建议您重新审核此部分c_d,并使其与您在构建ea_b时的工作方式相同。

以下是更正后的代码 - 除非我在创建c_d后未查看该部分,因为我不确定您要执行的操作。

e[]

节目输出:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int size1 = 4, a[] = {1,1,1,2};
    int size2 = 4, b[] = {2,2,2,3};
    int size3 = 4, c[] = {3,3,3,4};
    int size4 = 5, d[] = {4,4,4,5,6};
    int a_b[30], c_d[30], e[30];
    int i,j,k,t, sizefull;

    sizefull=size1+size2+size3+size4;
    printf("%d\n", sizefull);

    i=j=k=0;
    while (i < size1 && j < size2) 
    {
        if (a[i] <= b[j]) 
            {
            a_b[k] = a[i];
            i++;
            k++;
            }
       else {
            a_b[k] = b[j];
            k++;
            j++;
            }
    }
    while (i < size1) 
    {
        a_b[k] = a[i];
        i++;
        k++;
    }
    while (j < size2) 
    {
        a_b[k] = b[j];
        k++;
        j++;
    }
    printf("\nMerged array is :");
    for (i = 0; i < size1 + size2; i++)
        printf("%d ", a_b[i]);

    i=j=k=0;
    while (i < size3 && j < size4) {
        if (c[i] <= d[j]) {
            c_d[k] = c[i];
            i++;
            k++;
        } else {
            c_d[k] = d[j];
            k++;
            j++;
        }
    }
    while (i < size3) {
        c_d[k] = c[i];
        i++;
        k++;
    }
    while (j < size4) {
        c_d[k] = d[j];
        k++;
        j++;
    }
    printf("\nMerged array is :");
    for (i = 0; i < size3 + size4; i++)
        printf("%d ", c_d[i]);

    i=j=k=0;
    while (i < size1+size2 && j < size3+size4) 
    {
        if (a_b[i] <= c_d[j]) 
            {
            e[k] = a_b[i];
            i++;
            k++;
            }
       else {
            e[k] = c_d[j];
            k++;
            j++;
            }
    }
    while (i < size1+size2) 
    {
        e[k] = a_b[i];
        i++;
        k++;
    }
    while (j < size3+size4) 
    {
        e[k] = c_d[j];
        k++;
        j++;
    }

    printf("\n After merging: \n");
    for (i = 0; i < sizefull; i++)
    {
        printf("%d ", e[i]);
    }

    for (i=0;i<sizefull;i++)
    {
        for(j=i+1;j<sizefull;j++)
        {
            if(e[i]>e[j])
            {
                t=e[i];
                e[i]=e[j];
                e[j]=t;
            }
        }
    }
    printf("\nAscending Order is:");
    for(j=0;j<sizefull;j++)
    {
        printf("%d ",e[j]);
    }

    printf("\n\n Final Array is \n");
    for(i=0;i<sizefull;i++)
    {
        printf(" %d",e[i]);
    }

    for(i=0; i<sizefull;)
    {
        for(j = (i + 1); j<sizefull; j++)
        {
            if( e[i] != e[j])
                break;
        }
        printf("\nunique element = %d, at index = %d\n", e[i], i);
        printf("\nunique element = %d, at index = %d\n", e[i], i);
        i = j;
        return 0;
    }
}

答案 1 :(得分:0)

首先,您应该尝试对您的代码进行一些评论,以便其他人可以轻松地一次性阅读您的代码。我可以在代码中看到很多错误。你已经采用了不正确的数组大小,而且你的for循环也是不正确的。打印完最终数组for(i=0; i<sizefull;)后的循环不正确。 我将尝试为您的问题提供简单的解决方案。 首先,合并四个数组,其次,执行排序算法(bubble,比如说),然后尝试从中找到唯一的数字。

int main()
{
    int size1, size2, size3, size4,sizefull,a[30],b[30],c[30],d[30],e[120];      
    int i,j,t,match,temp;
    printf("Insert the size of A: ");
        scanf("%d",&size1);
    printf("Insert %d elements of A:\n", size1);
    for(i=0;i<size1;i++)
        scanf("%d",&a[i]);
    printf("Insert the size of B: ");
        scanf("%d",&size2);
    printf("Insert %d elements of B:\n", size2);
    for(i=0;i<size2;i++)
        scanf("%d",&b[i]);
    printf("Insert the size of C: ");
        scanf("%d",&size3);
    printf("Insert %d elements of C:\n", size3);
    for(i=0;i<size3;i++)
        scanf("%d",&c[i]);
    printf("Insert the size of D: ");
        scanf("%d",&size4);
    printf("Insert %d elements of D:\n", size4);
    for(i=0;i<size4;i++)
        scanf("%d",&d[i]);
    sizefull=size1+size2+size3+size4;
    //The below code is for copying.
    i=0;
    j=0;
    while(i<size1)  
    {
             e[j]=a[i];
             i++;
             j++;
    }
    i=0;
    while(i<size2)  
    {
             e[j]=b[i];
             i++;
             j++;
    }
    i=0;
    while(i<size3)  
    {
             e[j]=c[i];
             i++;
             j++;
    }
    i=0;
    while(i<size4)  
    {
             e[j]=d[i];
             i++;
             j++;
    }
     //bubble sort
    for(i=0;i<sizfull-i;i++)
    {
         for(j=0;j<sizefull-i;j++)
         {
              if(e[j]>e[j+1])
              {
                  temp=e[j];
                  e[j]=e[j+1];
                  e[j+1]=temp;
              }
         }
    }
    //Code below is for finding and printing unique numbers.
    for(i=0;i<sizefull-1;i++)
    {
        match=0; 
        for(j=i+1;j<sizefull;j++)
         {
             if(e[i]==e[j])
             {
                 match++;
                 break;
             }
         }
         if(match==0)
         printf("unique number=%d at index %d",e[i],i);
    }


}

答案 2 :(得分:0)

更改了示例代码以获取用户输入。合并部分与Antoine Mathys的答案相同,因此此代码示例主要显示了从排序数组中打印唯一数字的简单方法。添加了qsort()调用来对4个数组进行排序,因为不清楚这些数组是否应该已经排序。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* print unique numbers from sorted array */
void print_uniques(int x[], int sizex)
{
int i, j;
    if(sizex == 0)
        return;
    i = j = 0;
    while(i != sizex){
        i++;
        if(i != sizex && x[i] == x[j])
            continue;
        if((i - j) == 1)
            printf("%d\n", x[j]);
        j = i;
    }
}

/* merge two already sorted arrays */
void merge(int m[], int x[], int sizex, int y[], int sizey)
{
int i=0, j=0, k=0;
    while (i < sizex && j < sizey) 
    {
        if (x[i] <= y[j]) 
            m[k++] = x[i++];
        else 
            m[k++] = y[j++];
    }
    while (i < sizex) 
        m[k++] = x[i++];
    while (j < sizey) 
        m[k++] = y[j++];
}

/* compare function for qsort() */
int cmp(const int *i, const int *j)
{
    return *i - *j;
}

/* get data (no size check) */
int getdata(int x[], char l)
{
int s, i;
    printf("Enter the number of elements for %c : ", l);
    if(0 == scanf("%d", &s))
        return 0;
    for(i = 0; i < s; i++){
        printf("Enter an element for %c : ", l);
        scanf("%d", x+i);
    }
    return s;
}

int main()
{
int a[64], b[64], c[64], d[64];
int a_b[128], c_d[128];
int e[256];
int sizea, sizeb, sizec, sized;
int sizea_b, sizec_d, sizee;

    sizea = getdata(a, 'A');
    sizeb = getdata(b, 'B');
    sizec = getdata(c, 'C');
    sized = getdata(d, 'D');

    sizea_b = sizea   + sizeb;
    sizec_d = sizec   + sized;
    sizee   = sizea_b + sizec_d;

    /* sort the arrays */
    qsort(a, sizea, sizeof(a[0]), cmp);
    qsort(b, sizeb, sizeof(b[0]), cmp);
    qsort(c, sizec, sizeof(c[0]), cmp);
    qsort(d, sized, sizeof(d[0]), cmp);

    /* merge the arrays */
    merge(a_b, a, sizea, b, sizeb);
    merge(c_d, c, sizec, d, sized);
    merge(e, a_b, sizea_b, c_d, sizec_d);

    printf("\n");
    print_uniques(e, sizee);

    return 0;
}

替代获取数据,允许用户在一行上输入多个元素,而无需指定元素数量。

/* get data (no size check) */
int getdata(int x[], char l)
{
char d[] = " ,\t\n";
char t[80];
int i = 0;
char *pt;
    printf("enter element(s) for %c :\n", l);
    if(NULL == fgets(t, sizeof(t), stdin))
        return 0;
    pt = strtok(t, d);
    while(pt){
        sscanf(pt, "%d", x+i);
        i++;
        pt = strtok(NULL, d);
    }
    return i;
}