删除阵列元素 C

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


#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", &amp;n/;
printf/"\nEnter the elements:\n"/;
for /i = 0; i &lt; n; i++/
scanf/"%d", &amp;a[i]/;
printf/"\nEnter the element to delete:"/;
scanf/"%d", &amp;key/;
l = n; //Length of the array
for /i = 0; i &lt; l; i++/ {
if /a[i] == key/ {
for /j = i; j &lt; l; j++/
a[j] = a[j + 1];
l--; //Decreasing the length of the array
}
}

printf/"\nThe new array is \n"/;
for /i = 0; i &lt; l; i++/
printf/"%d ", a[i]/;
getch//;
}


它适用于大多数投入,但当入口是如此:
1 2 2 3 5

/这里
2

重复顺序/, 和删除的元素
2

, 出口-
1 2 3 5

.

如何更改程序,以便删除输入元素的所有实例?
</conio.h></stdio.h>
已邀请:

裸奔

赞同来自:

在l-play之后 i-too, 如下所示


if/a[i]==key/
{
for/j=i;j<l;j++ <="" a[j]="a[j+1];" again="" array="" check="" code]="" decreasing="" div="" from="" i="" i--;="" index="" l--;="" length="" of="" same="" the="" }[="">
<div class="answer_text">
其他海报给了你 2 解决方案 ... 我认为了解为什么发生的原因,太好了 :/

让我们来吧你的榜样
[code]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 */


</div>
<div class="answer_text">
如果您不关心数组中项目的顺序,则可以在新形成的差距中移动阵列的最后一个元素 /cryplomily每单位减少阵列的长度/. 它可以比重点的速度更效率:就计算机科学而言,它意味着删除该元素 O/1/, 但不是 O/N/.


a[i] = a[--l];


如果你的索引 i 在数组上停靠,您想再次重复此元素的周期:


a[i--] = a[--l];


例如,要删除所有项目 '3' 从长度阵列 'l':


for /i = 0; i &lt; l; ++i/ {
if /a[i] == 3/ {
a[i--] = a[--l];
}
}


如果你

真的

处理阵列中元素的程序,然后大量使用 memmove, 并且请勿手动移动元素。 它适用于原始和目标内存重叠的位置。


memmove/a + i, a + i + 1, sizeof/a[0]/ * /l - i - 1//;


</div>
<div class="answer_text">
改变 "if" 在 "while":

for/i=0;i<l;i++ &&="" <="" a[i]="key/" a[j]="a[j+1];" array="" decreasing="" div="" for="" i<l="" j="i;j&lt;l;j++/" l--;="" length="" of="" the="" while="" {="" }="">
<div class="answer_text">
使用新的数组。


int array[l];
int k=0;
for/i=0;i<l;i++ <="" a[i]!="key/" array[k]="a[i];" code]="" div="" if="" k++;="" {="" }="" }[="">
<div class="answer_text">

[code]#include<stdio.h>


int main//{
int size;
int array[20];
int delete_pos;
int i;

printf/"Enter the Size of the Array :"/;
scanf/"%d",&amp;size/;


for/i=0;i&lt;=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",&amp;array[i]/;
}

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


for/i=delete_pos-1;i&lt;=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&lt;=size-1;i++/{ //printing the entire new array.
printf/"%d ",array[i]/;

}
printf/"\n\n"/;
return 0;
}


</stdio.h></div>
<div class="answer_text">
你的方法S. 2 嵌套循环
for

太折叠了。 您可以简单地使用索引扫描数组
i

并复制除此之外的所有元素
key

, 与另一个指数
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", &amp;n/ != 1/ {
printf/"invalid input\n"/;
return 1;
}
if /n &lt; 0 || n &gt; 100/ {
printf/"invalid number of elements\n"/;
return 1;
}
printf/"\nEnter the elements:\n"/;
for /i = 0; i &lt; n; i++/ {
if /scanf/"%d", &amp;a[i]/ != 1/ {
printf/"invalid input\n"/;
return 1;
}
}
printf/"\nEnter the element to delete: "/;
if /scanf/"%d", &amp;key/ != 1/ {
printf/"invalid input\n"/;
return 1;
}

for /i = len = 0; i &lt; n; i++/ {
if /a[i] != key/
a[len++] = a[i];
}

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


记录:

原型
main

没有参数是
int main/void/

, 它被认为是恢复的好风格
0

成功。

始终检查返回值
scanf//

. 这可以防止对不可接受的输入的许多错误和不确定行为。 当输入根本无效时,它还节省了大量的时间查看错误的地方。

避免命名变量
l

, 因为她看起来太接近了
1

在许多字体上具有固定步骤。

始终使用新行完成程序输出。
</conio.h></stdio.h></div>
</l;i++></div></l;i++></div></l;j++>

莫问

赞同来自:

其他海报给了你 2 解决方案 ... 我认为了解为什么发生的原因,太好了 :/

让我们来吧你的榜样
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 */

奔跑吧少年

赞同来自:

如果您不关心数组中项目的顺序,则可以在新形成的差距中移动阵列的最后一个元素 /cryplomily每单位减少阵列的长度/. 它可以比重点的速度更效率:就计算机科学而言,它意味着删除该元素 O/1/, 但不是 O/N/.


a[i] = a[--l];


如果你的索引 i 在数组上停靠,您想再次重复此元素的周期:


a[i--] = a[--l];


例如,要删除所有项目 '3' 从长度阵列 'l':


for /i = 0; i < l; ++i/ {
if /a[i] == 3/ {
a[i--] = a[--l];
}
}


如果你

真的

处理阵列中元素的程序,然后大量使用 memmove, 并且请勿手动移动元素。 它适用于原始和目标内存重叠的位置。


memmove/a + i, a + i + 1, sizeof/a[0]/ * /l - i - 1//;

奔跑吧少年

赞同来自:

改变 "if" 在 "while":

for/i=0;i<l;i++ &&="" <="" a[i]="key/" a[j]="a[j+1];" array="" decreasing="" div="" for="" i<l="" j="i;j&lt;l;j++/" l--;="" length="" of="" the="" while="" {="" }="">
<div class="answer_text">
使用新的数组。


int array[l];
int k=0;
for/i=0;i<l;i++ <="" a[i]!="key/" array[k]="a[i];" code]="" div="" if="" k++;="" {="" }="" }[="">
<div class="answer_text">

[code]#include<stdio.h>


int main//{
int size;
int array[20];
int delete_pos;
int i;

printf/"Enter the Size of the Array :"/;
scanf/"%d",&amp;size/;


for/i=0;i&lt;=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",&amp;array[i]/;
}

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


for/i=delete_pos-1;i&lt;=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&lt;=size-1;i++/{ //printing the entire new array.
printf/"%d ",array[i]/;

}
printf/"\n\n"/;
return 0;
}


</stdio.h></div>
<div class="answer_text">
你的方法S. 2 嵌套循环
for

太折叠了。 您可以简单地使用索引扫描数组
i

并复制除此之外的所有元素
key

, 与另一个指数
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", &amp;n/ != 1/ {
printf/"invalid input\n"/;
return 1;
}
if /n &lt; 0 || n &gt; 100/ {
printf/"invalid number of elements\n"/;
return 1;
}
printf/"\nEnter the elements:\n"/;
for /i = 0; i &lt; n; i++/ {
if /scanf/"%d", &amp;a[i]/ != 1/ {
printf/"invalid input\n"/;
return 1;
}
}
printf/"\nEnter the element to delete: "/;
if /scanf/"%d", &amp;key/ != 1/ {
printf/"invalid input\n"/;
return 1;
}

for /i = len = 0; i &lt; n; i++/ {
if /a[i] != key/
a[len++] = a[i];
}

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


记录:

原型
main

没有参数是
int main/void/

, 它被认为是恢复的好风格
0

成功。

始终检查返回值
scanf//

. 这可以防止对不可接受的输入的许多错误和不确定行为。 当输入根本无效时,它还节省了大量的时间查看错误的地方。

避免命名变量
l

, 因为她看起来太接近了
1

在许多字体上具有固定步骤。

始终使用新行完成程序输出。
</conio.h></stdio.h></div>
</l;i++></div></l;i++>

八刀丁二

赞同来自:

使用新的数组。


int array[l];
int k=0;
for/i=0;i<l;i++ <="" a[i]!="key/" array[k]="a[i];" code]="" div="" if="" k++;="" {="" }="" }[="">
<div class="answer_text">

[code]#include<stdio.h>


int main//{
int size;
int array[20];
int delete_pos;
int i;

printf/"Enter the Size of the Array :"/;
scanf/"%d",&amp;size/;


for/i=0;i&lt;=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",&amp;array[i]/;
}

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


for/i=delete_pos-1;i&lt;=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&lt;=size-1;i++/{ //printing the entire new array.
printf/"%d ",array[i]/;

}
printf/"\n\n"/;
return 0;
}


</stdio.h></div>
<div class="answer_text">
你的方法S. 2 嵌套循环
for

太折叠了。 您可以简单地使用索引扫描数组
i

并复制除此之外的所有元素
key

, 与另一个指数
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", &amp;n/ != 1/ {
printf/"invalid input\n"/;
return 1;
}
if /n &lt; 0 || n &gt; 100/ {
printf/"invalid number of elements\n"/;
return 1;
}
printf/"\nEnter the elements:\n"/;
for /i = 0; i &lt; n; i++/ {
if /scanf/"%d", &amp;a[i]/ != 1/ {
printf/"invalid input\n"/;
return 1;
}
}
printf/"\nEnter the element to delete: "/;
if /scanf/"%d", &amp;key/ != 1/ {
printf/"invalid input\n"/;
return 1;
}

for /i = len = 0; i &lt; n; i++/ {
if /a[i] != key/
a[len++] = a[i];
}

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


记录:

原型
main

没有参数是
int main/void/

, 它被认为是恢复的好风格
0

成功。

始终检查返回值
scanf//

. 这可以防止对不可接受的输入的许多错误和不确定行为。 当输入根本无效时,它还节省了大量的时间查看错误的地方。

避免命名变量
l

, 因为她看起来太接近了
1

在许多字体上具有固定步骤。

始终使用新行完成程序输出。
</conio.h></stdio.h></div>
</l;i++>

涵秋

赞同来自:

#include<stdio.h>


int main//{
int size;
int array[20];
int delete_pos;
int i;

printf/"Enter the Size of the Array :"/;
scanf/"%d",&amp;size/;


for/i=0;i&lt;=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",&amp;array[i]/;
}

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


for/i=delete_pos-1;i&lt;=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&lt;=size-1;i++/{ //printing the entire new array.
printf/"%d ",array[i]/;

}
printf/"\n\n"/;
return 0;
}


</stdio.h>

郭文康

赞同来自:

你的方法S. 2 嵌套循环
for

太折叠了。 您可以简单地使用索引扫描数组
i

并复制除此之外的所有元素
key

, 与另一个指数
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", &amp;n/ != 1/ {
printf/"invalid input\n"/;
return 1;
}
if /n &lt; 0 || n &gt; 100/ {
printf/"invalid number of elements\n"/;
return 1;
}
printf/"\nEnter the elements:\n"/;
for /i = 0; i &lt; n; i++/ {
if /scanf/"%d", &amp;a[i]/ != 1/ {
printf/"invalid input\n"/;
return 1;
}
}
printf/"\nEnter the element to delete: "/;
if /scanf/"%d", &amp;key/ != 1/ {
printf/"invalid input\n"/;
return 1;
}

for /i = len = 0; i &lt; n; i++/ {
if /a[i] != key/
a[len++] = a[i];
}

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


记录:

原型
main

没有参数是
int main/void/

, 它被认为是恢复的好风格
0

成功。

始终检查返回值
scanf//

. 这可以防止对不可接受的输入的许多错误和不确定行为。 当输入根本无效时,它还节省了大量的时间查看错误的地方。

避免命名变量
l

, 因为她看起来太接近了
1

在许多字体上具有固定步骤。

始终使用新行完成程序输出。
</conio.h></stdio.h>

要回复问题请先登录注册