Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
删除C中的数组元素_C_Arrays - Fatal编程技术网

删除C中的数组元素

删除C中的数组元素,c,arrays,C,Arrays,我编写了以下程序来删除用户输入的数组元素 #include <stdio.h> #include <conio.h> void main() { int j, i, a[100], n, key, l; clrscr(); printf("Enter the number of elements:"); scanf("%d", &n); printf("\nEnter the elements:\n"); for

我编写了以下程序来删除用户输入的数组元素

#include <stdio.h>
#include <conio.h>

void main() {
    int j, i, a[100], n, key, l;
    clrscr();
    printf("Enter the number of elements:");
    scanf("%d", &n);
    printf("\nEnter the elements:\n");
    for (i = 0; i < n; i++)
        scanf("%d", &a[i]);
    printf("\nEnter the element to delete:");
    scanf("%d", &key);
    l = n;  //Length of the array
    for (i = 0; i < l; i++) {
        if (a[i] == key) {
           for (j = i; j < l; j++)
               a[j] = a[j + 1];
           l--;    //Decreasing the length of the array
        }
    }

    printf("\nThe new array is \n");
    for (i = 0; i < l; i++)
        printf("%d ", a[i]);
     getch();
}
#包括
#包括
void main(){
int j,i,a[100],n,key,l;
clrsc();
printf(“输入元素数:”);
scanf(“%d”和“&n”);
printf(“\n输入元素:\n”);
对于(i=0;i
对于大多数输入,它工作正常,但当输入类似于:
1235
(此处
2
连续重复)且要删除的元素为
2
,则输出为
1235

如何修改程序以删除输入元素的所有实例?

在l--put I--too之后,如下所示

if(a[i]==key)
  {
   for(j=i;j<l;j++)
    a[j]=a[j+1];
   l--;    //Decreasing the length of the array
   i--;    //check again from same index i
  }
if(a[i]==键)
{
对于(j=i;j将“if”改为“while”:


对于(i=0;i其他海报为您提供了两种解决方案……我认为理解它为什么会发生也很好:)

让我们以您的示例
1,2,2,3,5
为例,逐行跟踪代码

i = 0;             /* first time through the loop; i "points" to 1 */
if (a[i] == 2) ... /* nope; next loop */
i = 1;
if (a[1] == 2) ... /* yes! let's go inside the if */
                   /* move all elements back
                   ** and "decrease" array length */
                   /* array is now 1, 2, 3, 5 */
                   /* next loop */
i = 2;
if (a[i] == 2) ... /* nope; OH! Wait ...
                   ** a[1] is the new 2 and it wasn't checked */

如果你不关心数组中元素的顺序,你可以将数组的最后一个元素移动到新形成的间隙中(巧妙地将数组的长度减少1)。这比将元素向下分流要有效得多:在计算机科学术语中,这使得删除元素O(1)而不是O(N)

如果i索引在数组上循环,则需要再次循环此元素:

a[i--] = a[--l];
例如,要从长度为“l”的数组中删除所有元素“3”:

for (i = 0; i < l; ++i) {
    if (a[i] == 3) {
        a[i--] = a[--l];
    }
}
使用新数组

int array[l];
int k=0;
for(i=0;i<l;i++)
{
 if(a[i]!=key)
 {
  array[k]=a[i];
  k++;
 }

}
int数组[l];
int k=0;
对于(i=0;i
#包括
int main(){
整数大小;
int数组[20];
int删除位置;
int i;
printf(“输入数组的大小:”);
scanf(“%d”,大小(&S);

对于(i=0;i您使用2个嵌套的
for
循环的方法太复杂了。您可以使用索引
i
简单地扫描数组,并使用不同的索引
len
复制与
键不同的所有元素。生成的数组长度是
len
的最终值

以下是修改后的版本:

#include <stdio.h>
#include <conio.h>

int main(void) {
    int a[100];
    int i, n, key, len;

    clrscr();
    printf("Enter the number of elements: ");
    if (scanf("%d", &n) != 1) {
        printf("invalid input\n");
        return 1;
    }
    if (n < 0 || n > 100) {
        printf("invalid number of elements\n");
        return 1;
    }
    printf("\nEnter the elements:\n");
    for (i = 0; i < n; i++) {
        if (scanf("%d", &a[i]) != 1) {
            printf("invalid input\n");
            return 1;
        }
    }
    printf("\nEnter the element to delete: ");
    if (scanf("%d", &key) != 1) {
        printf("invalid input\n");
        return 1;
    }

    for (i = len = 0; i < n; i++) {
        if (a[i] != key)
           a[len++] = a[i];
    }

    printf("\nThe new array is:\n");
    for (i = 0; i < len; i++)
        printf("%d ", a[i]);
    printf("\n");
    getch();
    return 0;
}
#包括
#包括
内部主(空){
INTA[100];
int i,n,key,len;
clrsc();
printf(“输入元素数:”);
如果(scanf(“%d”,&n)!=1){
printf(“无效输入\n”);
返回1;
}
如果(n<0 | | n>100){
printf(“元素数无效\n”);
返回1;
}
printf(“\n输入元素:\n”);
对于(i=0;i
注:

  • 没有参数的
    main
    的原型是
    intmain(void)
    ,返回
    0
    被认为是成功的好方式

  • 始终测试
    scanf()
    的返回值。这可以防止许多错误和无效输入的未定义行为。当输入无效时,还可以节省大量时间查找错误位置

  • 避免命名变量
    l
    ,因为在许多固定间距字体中,它看起来太接近
    1

  • 始终使用换行符终止程序输出


这是家庭作业还是用于编码测试?我担心我的技巧是错误的-删除最后一个元素可能会无限循环(取决于数组之外的a[l]的值)。另外,我必须注意数组的大小。我没有检查它是否有过去的爆炸,但a[j]=a[j+1}当j指向数组+1中的最后一个元素时失败:在看到您的帖子之前,我一直很困惑。大的三次是减少循环计数器。谢谢!请在您的答案中添加一些描述。@Dhruv Aditya mittalfst它将询问数组的大小,然后要求将元素写入数组。之后获取元素后,它将询问要从数组中删除的数组。最终输出将打印数组中的剩余元素,即新数组。@RohitPoudel
memmove(a + i, a + i + 1, sizeof(a[0]) * (l - i - 1));
int array[l];
int k=0;
for(i=0;i<l;i++)
{
 if(a[i]!=key)
 {
  array[k]=a[i];
  k++;
 }

}
#include<stdio.h>


int main(){
int size;
int array[20];
int delete_pos;
int i;

printf("Enter the Size of the Array :");
scanf("%d",&size);


for(i=0;i<=size-1;i++){                                //no of elements taken are 1 less than size of the array asked.
    printf("\nEnter the element[%d] :",i+1);
    scanf("%d",&array[i]);
}

printf("\nEnter the Position of the array to be deleted :");
scanf("%d",&delete_pos);


for(i=delete_pos-1;i<=size;i++){                    //every element of the array is replaced by array on next position.
    array[i]=array[i+1];}

size=size-1;                                       // Reducing the size of the array as one element is deleted.
printf("Your new array is \n");
for(i=0;i<=size-1;i++){                            //printing the entire new array.
    printf("%d ",array[i]);

}
printf("\n\n");
return 0;
}
#include <stdio.h>
#include <conio.h>

int main(void) {
    int a[100];
    int i, n, key, len;

    clrscr();
    printf("Enter the number of elements: ");
    if (scanf("%d", &n) != 1) {
        printf("invalid input\n");
        return 1;
    }
    if (n < 0 || n > 100) {
        printf("invalid number of elements\n");
        return 1;
    }
    printf("\nEnter the elements:\n");
    for (i = 0; i < n; i++) {
        if (scanf("%d", &a[i]) != 1) {
            printf("invalid input\n");
            return 1;
        }
    }
    printf("\nEnter the element to delete: ");
    if (scanf("%d", &key) != 1) {
        printf("invalid input\n");
        return 1;
    }

    for (i = len = 0; i < n; i++) {
        if (a[i] != key)
           a[len++] = a[i];
    }

    printf("\nThe new array is:\n");
    for (i = 0; i < len; i++)
        printf("%d ", a[i]);
    printf("\n");
    getch();
    return 0;
}