如何根据条件将元素从一个数组添加到未定义大小的另一个数组中?

时间:2019-01-04 09:34:56

标签: c arrays concatenation dynamic-memory-allocation

我已经自学了C几个星期,并且正在尝试编写代码,使用户能够确定数组中的大小和元素,然后将其分成两个数组-一个用于奇数,另一个用于以获得偶数。

我很确定动态分配与此有关,但是我不确定如何实现它。这是到目前为止的代码:

#include <stdio.h>
#include <stdlib.h>

int main()
{
  //User decides the size of the array of numbers-------------------------------
  int n;
  printf("How many numbers? ");
  scanf("%d",&n);

  //User inputs values into array the size of array[n]--------------------------
  int i;
  int array[n];
  printf("What are the numbers?\n");
  for(i=0; i<n; i++)
  {
    scanf("%d",&array[i]);
  }

  //loop goes through array, separates even and odds into 2 new arrays----------
  //use dynamic allocation??

  for(i=0;i<n;i++)
  {
    int *evenarray = malloc(sizeof(evenarray)); //not sure if this is setup correctly
    int *oddarray = malloc(sizeof(oddarray)); //not sure if this is setup correctly

    if(array[i] % 2 == 0) //if value in array CAN be divided by 2
    {
      printf("Test statement.\n");
    }
    else //if this is not true, append to odd array
    {
      printf("Second test statement.\n");
    }
  }
}

/*this program accepts a user chosen number of numbers
  then, the program separates the odd and even numbers into
  two different arrays*/

3 个答案:

答案 0 :(得分:3)

没有一种神奇的方法可以一次获得这些信息。但是,您可以尝试以下任一方法:

  • 遍历第一个数组以找出奇数(或偶数)的数量,然后,您知道必须为其分配内存的元素的数量,并且可以使用VLA(作为第一个数组本身)或使用指针和分配器函数分配内存。

    ->但是,在此过程中,您必须执行两次奇/偶检查-一次以计算奇/偶数的出现,而下一次则是实际确定并复制到新位置。

    p>
  • 分配类似于第一数组大小的两个内存块,然后分别将奇数和偶数元素填充到新的内存中。存储完所有元素后,将计数realloc()分配给内存以达到确切大小。

    ->在这种情况下,要进行预分配,但是奇/偶校验只需要执行一次。

答案 1 :(得分:2)

我在这里看到一些问题。 您不应定义具有非恒定值的数组,例如您告诉用户输入的变量 n

通常,当您定义一个具有常量值的数组时,将为您的数组创建的空间确实很多。让我们看一下这个例子:

#define MAX 100
<...>
int array[MAX];
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++) scanf("%d", &array[i]);
<...>

我们刚刚制作了一个整数数组,其常量定义为MAX,具有100个未定义的空格,但是使用for循环,我们设法填充了前n个索引中的间隙。 例如,如果n为20,则剩下的80个空格将作为从内存中获取的随机值(最有可能是垃圾箱,-84134 ...)。这也是堆栈使用的静态内存。

这很好,但是您可以使用名为malloc()的函数动态分配足够的空间(简单来说就是内存位置),以确保您有足够的n个数字。

  int n;
  printf("How many numbers? ");
  scanf("%d", &n);

  // User inputs values into array the size of array[n]
  int i;
  int *array = (int*)malloc(n * sizeof(int));
  printf("What are the numbers?\n");
  for(i=0; i<n; i++) scanf("%d", &array[i]);

现在,我们使用函数malloc()来创建足够的空间。 Malloc用于在堆而不是堆栈上分配动态内存。

函数本身的定义如下:

void *malloc(size_t size)

malloc()返回一个空指针,指向您告诉他创建的新内存块。它只有一个参数,即新块的大小(以字节为单位),它是通过获取所需的“空格”数并使用 sizeof 运算符将它们相乘而得出的。

指向新内存块的指针需要转换为适当的类型,在这种情况下,它是一个整数指针。

int *array = (int*)malloc(n * sizeof(int));

所以,最简单的放置方法是:制作一个指针->让malloc为您动态分配内存->将其转换为适当的类型,瞧,您的数组中只有足够的空间。

现在,让我们进入任务本身:我的想法是首先计算有多少个偶数和奇数,将其存储在单独的整数变量中,然后动态地为该数量的奇数和偶数动态分配足够的空间。

最终解决方案:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int n;

    printf("How many numbers? ");
    scanf("%d", &n);

    int i, odd = 0, even = 0, counter_odd = 0, counter_even = 0;
    int *array = (int*)malloc(n * sizeof(int));

    printf("What are the numbers?\n");
    for(i = 0; i < n; i++) scanf("%d", &array[i]);

    // Counting how many odd and even numbers there are
    for(i = 0; i < n; i++) {
        if(array[i] % 2) odd++;
        else even++; 
    }
    int *even_array = (int*)malloc(sizeof(int) * even);
    int *odd_array = (int*)malloc(sizeof(int) * odd);

    // Placing the numbers in separate arrays
    for(i = 0; i < n; i++) {
        if(array[i] % 2) odd_array[counter_odd++] = array[i];
        else even_array[counter_even++] = array[i]; 
    }

    // Printing them out
    printf("\nOriginal array: ");
    for(i = 0; i < n; i++) printf("%d ", array[i]);

    printf("\nOdd array: ");
    for(i = 0; i < odd; i++) printf("%d ", odd_array[i]);

    printf("\nEven array: ");
    for(i = 0; i < even; i++) printf("%d ", even_array[i]);

    return 0;
}

测试用例:

How many numbers? 5
What are the numbers?
1
2
3
4
5

Original array: 1 2 3 4 5
Odd array: 1 3 5
Even array: 2 4

答案 2 :(得分:-1)

您可以复制到奇数/偶数数组中,并保留单独的计数器来跟踪它。即:

  //loop goes through array, separates even and odds into 2 new arrays----------
  //use dynamic allocation??

  int evencount =0;
  int oddcount =0;
  int *evenarray = malloc(sizeof(evenarray)); //not sure if this is setup correctly
  int *oddarray = malloc(sizeof(oddarray)); //not sure if this is setup correctly
  for(i=0;i<n;i++)
  {
    if(array[i] % 2 == 0) //if value in array CAN be divided by 2
    {
      printf("Printing to even array.\n");
      evenarray[evencount] = array[i];
      evencount++;
    }
    else //if this is not true, append to odd array
    {
      printf("Printing to odd array.\n");
      oddarray[oddcount] = array[i];
      oddcount++;
    }
  }

  printf("evenarray = ");
  for(i=0;i<evencount;i++){
    printf("%d, ", evenarray[i]);
  }
  printf("\n");
  printf("oddarray = ");
  for(i=0;i<oddcount;i++){
    printf("%d, ", oddarray[i]);
  }
  printf("\n");
相关问题