在C中从数组中删除零项
我有一个值数组在C中从数组中删除零项,c,arrays,algorithm,zero,C,Arrays,Algorithm,Zero,我有一个值数组x={0,0,1,2,3,0,0,7,8},我想用C删除零项 尝试: 1,2,3,7,8, 我试图循环遍历数组中的每个值,并检查条目是否不等于零。如果此条件为true,则我将尝试使用原始数组值填充新数组 #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { int x[] = { 0, 0, 1, 2, 3, 0, 0, 7, 8 };
x={0,0,1,2,3,0,0,7,8}
,我想用C删除零项
尝试:
1,2,3,7,8,
我试图循环遍历数组中的每个值,并检查条目是否不等于零。如果此条件为true,则我将尝试使用原始数组值填充新数组
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int x[] = { 0, 0, 1, 2, 3, 0, 0, 7, 8 };
int i;
int x_upd[100];
for (i = 0; i < 9; i++) {
if (x[i] != 0) {
x_upd[i] = x[i]; // if true, populate new array with value
}
}
for (i = 0; i < 9; i++) {
printf(" Peak updated %d\t", x_upd[i]); //
}
return 0;
}
#包括
#包括
#包括
int main(){
intx[]={0,0,1,2,3,0,0,7,8};
int i;
int x_upd[100];
对于(i=0;i<9;i++){
如果(x[i]!=0){
x_upd[i]=x[i];//如果为true,则使用值填充新数组
}
}
对于(i=0;i<9;i++){
printf(“峰值更新%d\t”,x_upd[i])//
}
返回0;
}
输出没有给我所需的值
{1,2,3,7,8}
。取而代之的是,我在零原来所在的位置获取垃圾值
对我在这里做错了什么有什么建议吗?我需要else语句吗?这个问题可以通过使用两个索引来解决:一个用于源数组(
x
),另一个用于目标数组(x\u upd
),分别是下面代码中的I
和j
int i, j;
for(i=0,j=0; i<9; i++) {
if (!x[i]) // is x[i] zero?
continue; // then skip this element
// otherwise copy current element and update destination index
x_upd[j++] = x[i];
}
inti,j;
对于(i=0,j=0;i
那么,这里
for(i=0;i<9;i++)
{
printf(" Peak updated %d\t",x_upd[i]); //
}
这种情况下的诀窍是使用不同的变量索引到另一个数组中
因此,与此相反:
x_upd[i] = x[i];
您可以使用另一个变量j
,当您为x\u upd
x_upd[j++] = x[i];
应该使用单独的计数器变量,否则在分配给新数组时,将“跳过”原始数组包含零的索引
#include <stdio.h>
int main() {
int x[] = { 0, 0, 1, 2, 3, 0, 0, 7, 8 };
int i;
int j = 0;
int x_upd[100];
for (i = 0; i < 9; i++) {
if (x[i] != 0) {
x_upd[j++] = x[i]; // if true, populate new array with value
}
}
for (i = 0; i < j; i++) {
printf(" Peak updated %d\t", x_upd[i]); //
}
return 0;
}
#包括
int main(){
intx[]={0,0,1,2,3,0,0,7,8};
int i;
int j=0;
int x_upd[100];
对于(i=0;i<9;i++){
如果(x[i]!=0){
x_upd[j++]=x[i];//如果为true,则使用值填充新数组
}
}
对于(i=0;i
无需创建新数组,请参阅使用一个数组的工作代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int x[] = { 0, 0, 1, 2, 3, 0, 0, 7, 8 };
int i, n;
for (i = 0, n = 0; i<9; i++)
{
if (x[i] != 0)
{
x[n++] = x[i];
}
}
for (i = 0; i<n; i++)
{
printf("%d,", x[i]);
}
return 0;
}
您应该分别递增x_upd数组索引。类似于:
int y = 0;
for(i=0;i<9;i++)
{
if(x[i] != 0)
{
x_upd[y] = x[i]; // if true, populate new array with value
y++;
}
}
inty=0;
对于(i=0;i您按顺序遍历值0-9一次,然后跳过0
,因此您得到{garbage,garbage,1,2,3,garbage,garbage,7,8}
。您必须为非0
的值的数量保留一个单独的计数器:
int position = 0;
for(i = 0; i < 9; i++)
{
if(x[i] != 0)
{
x_upd[position] = x[i]; // if true, populate new array with value
position++;
}
}
//loop until you get to counter
for(i = 0; i < position; i++)
{
printf(" Peak updated %d\t", x_upd[i]);
}
int位置=0;
对于(i=0;i<9;i++)
{
如果(x[i]!=0)
{
x_upd[position]=x[i];//如果为true,则使用值填充新数组
位置++;
}
}
//循环直到你们到达柜台
对于(i=0;i
无需索引
int populate(int *src, int *dest, int size)
//usage: - src - source table
//src - source table
//dest - destination table
//size of the source table - source table
//Return: -1 if pointers are null, -2 if source table has zero elements, or number of non zero elements copied
{
int result = (src == NULL || dest == NULL) * -1;
// it an equivalen of:
// int result;
// if(src == NULL || dest == NULL)
// result = -1;
// else
// result = 0;
if(size == 0) result = -2;
if (!result)
{
while(size--)
if (*src)
{
*dest++ = *src;
result++;
}
src++;
}
return result;
}
int main()
{
int x[] = { 0,0,1,2,3,0,0,7,8 };
int x_upd[100];
int result = populate(x,x_upd, sizeof(x) / sizeof(x[0]))
for (int i = 0; i<result; i++)
{
printf(" Peak updated %d\t", x_upd[i]); //
}
return 0;
}
int填充(int*src、int*dest、int-size)
//用法:-src-源表
//src-源表
//目的地表
//源表的大小-源表
//Return:-1(如果指针为null),-2(如果源表有零个元素)或复制的非零元素数
{
int result=(src==NULL | | dest==NULL)*-1;
//它相当于:
//int结果;
//if(src==NULL | | dest==NULL)
//结果=-1;
//否则
//结果=0;
如果(大小==0)结果=-2;
如果(!结果)
{
而(大小--)
如果(*src)
{
*dest++=*src;
结果++;
}
src++;
}
返回结果;
}
int main()
{
int x[]={0,0,1,2,3,0,0,7,8};
int x_upd[100];
int result=填充(x,x_upd,sizeof(x)/sizeof(x[0]))
对于(int i=0;i),C++中已经有这样的函数,它被命名为“代码> ReavyIsPox< /Cord>”。
#include <stdio.h>
int * remove_copy(const int *in, size_t n, int *out, int value)
{
for (size_t i = 0; i != n; i++)
{
if (in[i] != value) *out++ = in[i];
}
return out;
}
int main( void )
{
int a[] = { 0, 0, 1, 2, 3, 0, 0, 7, 8 };
int b[sizeof(a) / sizeof(*a)];
const size_t N = sizeof(a) / sizeof(*a);
int *last = remove_copy(a, N, b, 0);
for (int *first = b; first != last; ++first)
{
printf("%d ", *first);
}
putchar('\n');
return 0;
}
或者,该函数可以返回复制值的数目
size_t remove_copy(const int *in, size_t n, int *out, int value)
{
size_t m = 0;
for (size_t i = 0; i != n; i++)
{
if (in[i] != value) out[m++] = in[i];
}
return m;
}
对于你的代码,你需要使用一个额外的变量来保存目标数组中的索引
int m = 0;
for ( i = 0; i < sizeof( x ) / sizeof( *x ); i++ )
{
if ( x[i] != 0 )
{
x_upd[m++] = x[i]; // if true, populate new array with value
}
}
for ( i = 0; i < m; i++ )
{
printf(" Peak updated %d\t", x_upd[i] ); //
}
intm=0;
对于(i=0;i
事实上,第一个循环对应于上面显示的第二个函数实现。问题在于:
for(i=0;i<9;i++)
{
if(x[i] != 0)
{
x_upd[i] = x[i]; // if true, populate new array with value
}
}
for(i=0;iinti,j=0;
x_upd[j++]=x[i];
for(i=0;ifor(i=0;i
在第二个循环中,在打印值的地方,对于未初始化的x\u upd
元素,您认为您将打印什么?您还需要跟踪新的大小。这是我仍然不完全理解的,但我是否会打印一些对变量地址的引用?“我在零原来所在的位置上获取垃圾值。”-->x_upd[]的未初始化部分中您希望有什么值
?也许他需要保留原始数组。他没有明确说明他需要什么,因为他是初学者,我认为他使用第二个数组只是因为他不知道如何使用一个。这个int result=(src==NULL | | dest==NULL | | size==0)*-1;
不适合作为一个教育性的例子。@alk我知道你的意思-在注释中解释:)通常我认为你应该把注释放在你要注释的行上方。如果你在int-populate(int*src,int*dest,int-size)
和{
当size==0时,为什么不直接返回0
?为什么不简单地写:if(src==NULL | | dest==NULL)return-1;
@chqrlie我不喜欢返回点超过1的函数,原因很多,我在这里已经多次解释过了
int populate(int *src, int *dest, int size)
//usage: - src - source table
//src - source table
//dest - destination table
//size of the source table - source table
//Return: -1 if pointers are null, -2 if source table has zero elements, or number of non zero elements copied
{
int result = (src == NULL || dest == NULL) * -1;
// it an equivalen of:
// int result;
// if(src == NULL || dest == NULL)
// result = -1;
// else
// result = 0;
if(size == 0) result = -2;
if (!result)
{
while(size--)
if (*src)
{
*dest++ = *src;
result++;
}
src++;
}
return result;
}
int main()
{
int x[] = { 0,0,1,2,3,0,0,7,8 };
int x_upd[100];
int result = populate(x,x_upd, sizeof(x) / sizeof(x[0]))
for (int i = 0; i<result; i++)
{
printf(" Peak updated %d\t", x_upd[i]); //
}
return 0;
}
#include <stdio.h>
int * remove_copy(const int *in, size_t n, int *out, int value)
{
for (size_t i = 0; i != n; i++)
{
if (in[i] != value) *out++ = in[i];
}
return out;
}
int main( void )
{
int a[] = { 0, 0, 1, 2, 3, 0, 0, 7, 8 };
int b[sizeof(a) / sizeof(*a)];
const size_t N = sizeof(a) / sizeof(*a);
int *last = remove_copy(a, N, b, 0);
for (int *first = b; first != last; ++first)
{
printf("%d ", *first);
}
putchar('\n');
return 0;
}
1 2 3 7 8
size_t remove_copy(const int *in, size_t n, int *out, int value)
{
size_t m = 0;
for (size_t i = 0; i != n; i++)
{
if (in[i] != value) out[m++] = in[i];
}
return m;
}
int m = 0;
for ( i = 0; i < sizeof( x ) / sizeof( *x ); i++ )
{
if ( x[i] != 0 )
{
x_upd[m++] = x[i]; // if true, populate new array with value
}
}
for ( i = 0; i < m; i++ )
{
printf(" Peak updated %d\t", x_upd[i] ); //
}
for(i=0;i<9;i++)
{
if(x[i] != 0)
{
x_upd[i] = x[i]; // if true, populate new array with value
}
}
for(i=0;i<9;i++) {
printf(" Peak updated %d\t",x_upd[i]); //
}
int j;
for(i=0,j=0;i<9;i++) {
if(x[i] != 0)
{
x_upd[j] = x[i]; // if true, populate new array with value
j++; // index for values inserted
}
}
int k;
for(k=0;k<=j;k++) {
printf(" Peak updated %d\t",x_upd[k]); //
}