简体   繁体   English

如何仅返回具有偶数/奇数的数组并删除不必要的单元格?

[英]How to return array with only even/odd numbers and delete the unnecessary cells?

I try to get an array, an number and is_even bool variable from the user and return an only even numbers new array, else return only odd numbers array depending on is_even. 我尝试从用户那里获得一个数组,一个数字和is_even bool变量,并返回一个仅偶数的新数组,否则根据is_even返回仅奇数的数组。 for example: the array {1,2,3,4} and is_even=1 will return {2,4} ,if is_even=0 the returned array will be {1,3} as I get it I should allocate the array dynamically prior to passing it to a function. 例如:数组{1,2,3,4}和is_even = 1将返回{2,4},如果is_even = 0,则返回的数组将是{1,3},因为我知道应该动态分配该数组在将其传递给函数之前。 what I have done so far is. 到目前为止,我所做的是。

I got stuck with the return. 我被退货卡住了。 I checked whether the content pointed by p is even or odd but how do I erase cells? 我检查了p指向的内容是偶数还是奇数,但是如何擦除单元格?

#include <stdio.h>
#include <malloc.h>

  int *new_array(int *p,int number,int is_even){
  int j,i=0;
  int counter=0;
    if(is_even){
      for(j=0;j<number;j++){
        if(*(p+j)%2==0){
        }
      }
      return p;
    }
  }


  void main() {
    int n,i,is_even;
    int *p;
    printf("enter number of elements");
    scanf("%d",&n); rewind(stdin);
    printf("hoose is_even 1 or 0");
    scanf("%d",&is_even);rewind(stdin);

    p=(int *)malloc(n* sizeof(int));
    for(i=0;i<n;i++){
      scanf("%d",p+i);
    }
    p=new_array(p,n,is_even);
    for(i=0;i<n;i++){
      printf("%4d",*(p+i));
    }
  }

Part of your problem is that you are not accounting for one of the pieces of information you need to convey to the caller: the effective number of integers in the returned array. 问题的一部分是您没有考虑需要传递给调用者的信息之一:返回数组中的有效整数数。 Your print loop assumes the same number of elements as were originally read, but by the nature of the function, this will typically be too many. 您的打印循环假定与最初读取的元素数量相同,但是根据函数的性质,通常会太多。

You ask, 你问,

how do I erase cells? 如何删除单元格?

, but "erasure" is not a thing you can do. ,但是您不能做“删除”。 You can overwrite array elements with different values, but you cannot make an individual array element cease to exist, especially not from the middle of an array. 您可以用不同的值覆盖数组元素,但是不能使单个数组元素不复存在,尤其是不能从数组中间开始。 The usual idiom would be to put the elements you want to keep in the initial elements of either the original array or a new one, and return how many elements that is. 通常的习惯用法是将要保留的元素放入原始数组或新数组的初始元素中,并返回多少个元素。 In the case of a new array, you must also return a pointer to the (dynamically allocated) array. 对于新数组,还必须返回一个指向(动态分配的)数组的指针。 The function signature you present is not adequate, because it provides no good means to return the count of elements. 您提供的函数签名不足,因为它没有提供返回元素计数的好方法。

There is a number of ways to address that. 有多种解决方法。 A simple one would be to make number an in/out parameter, by passing a pointer to the number of elements instead of the number of elements value: 一种简单的方法是将number作为输入/输出参数,方法是将指针传递给元素数而不是元素数value:

int *new_array(int *p, int *number, int is_even) {
    // ... 'j' keeps a running count of the number of is_even elements

    *number = j;  // Write the final number of elements back to the caller
    return p;     // return the allocated array
}

The details of the implementation would need to change a bit to accommodate the change in type and usage of the number parameter, and also to fix bugs. 实现的细节将需要稍作改动以适应number参数的类型和用法的更改,并修复错误。

You might then call it like so: 然后,您可以这样称呼它:

p = new_array(p, &n, is_even);

... and afterward continue just as you already were doing. ...然后像您已经在做的那样继续。

You could place all of your even/odd numbers at the beginning of the array, realloc() the array for its new size, and send the value of its new length back in your return. 您可以将所有偶数/奇数放在数组的开头,重新分配数组的新大小,然后将其新长度的值发送回给您。 but you will need your function to receive (int** Array) in order to change the pointer for the array 但是您将需要函数来接收(int ** Array)以更改数组的指针

so function declaration could be int new_array(int **p ,int number ,int is_even) 所以函数声明可以是int new_array(int ** p,int number,int is_even)

The problem is not deleting the cells the problem is that when you delete them your arrays length is not the same anymore.. 问题不在于删除单元格,问题在于当您删除它们时,数组长度不再相同。

By the way you can also change the value of length and return the new add for the new array with int* new_array(int *p, int *number, int is_even) 顺便说一句,您还可以更改长度值并使用int * new_array(int * p,int * number,int is_even)返回新数组的新添加项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM