简体   繁体   中英

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. 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. 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?

#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:

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.

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

so function declaration could be 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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