【问题标题】:Insert a number between every element in my array in C在 C 中我的数组中的每个元素之间插入一个数字
【发布时间】:2016-05-31 17:23:34
【问题描述】:

当涉及到 C 语言和插入数组时,我完全陷入困境。我在下面有我的代码并解释它说用户想要一个 3 的数组。所以用户将 4 3 2 输入到数组a1[n] 中。我需要数组a2[] 来输出相同的数字,但每个数字之间为零。当输出a2[] 时,最终结果将是 4 0 3 0 2 0。如何在所有其他元素之间获得零?

#include <stdio.h>

    int main() {

    int n = 0; 
    int number = 0; 
    int a1[n]; 
    int a2[2 * n];

    printf("Enter the length of the array: ");  
    scanf("%d", &n);

    printf("Enter the elements of the array: ");

    for(i = 0; i < n; i++){ //adds values to first array
            scanf("%d",&number);
            a1[i] = number; } 

   for(i = 0; i < n; i++){ //copies and sets the arrays the same
            a2[i] = a1[i]; }

【问题讨论】:

  • 是否一定要有一个带有这些零的数组?您可以在输出数组时简单地插入它们。
  • 不,这没有必要,但可以帮助我学习任何一种方式

标签: c arrays


【解决方案1】:

假设您的数组已正确定义和初始化(静态或动态),只需在复制期间正确计数即可:

for(int i = 0; i < n; i++){ 
        a2[i+i] = a1[i]; 
        if(i < n-1) a2[i+i+1] = 0;
        } 

【讨论】:

  • 非常感谢,这是我需要的 for 循环!比我想象的要复杂一点。
【解决方案2】:
int n = 0; 
int number = 0; 
int a1[n]; 
int a2[2 * n];

恭喜,现在a1a2 是长度为零的数组。即使您稍后更改n,这也不会影响数组的长度。在 C 中,您不能使数组更长或更短。

改用int*calloc

【讨论】:

    【解决方案3】:

    首先,您不能创建具有运行时定义大小的stack-allocatedstatic 数组。

    int a[N]; // N should be determined during compilation
    

    您应该使用heap-alloceteddynamic 数组:

    int *a;
    a = (int *)malloc(2 * n, sizeof(int)); // n may be defined by user input
    

    如果不将数组移动到另一个地方,就无法调整数组的大小,您可以创建一个新数组(比第一个大)并用源数字和零填充它:

    #include <stdio.h>
    
    int main() {
        int n = 0; 
        printf("Enter the length of the array: ");  
        scanf("%d", &n);
    
        int *a1 = (int *)malloc(n, sizeof(int));
        int *a2 = (int *)malloc(n * 2, sizeof(int));
    
        printf("Enter the elements of the array: ");
    
        int i, number;
        for(i = 0; i < n; i++){ //adds values to first array
            scanf("%d",&number);
            a1[i] = number;
        }
    
        for(i = 0; i < n; i++){ //copies and sets the arrays the same
            a2[i * 2] = a1[i];
            a2[i * 2 + 1] = 0;
        }
    
        for(i = 0; i < n * 2; ++i){
            printf("%d ", a2[i]);
        }
    }
    

    【讨论】:

    • Variable length arrays 允许您创建具有运行时定义大小的堆栈分配数组。
    • "you can't create stack-allocated ... with runtime-defined size" --> 你是说代码不能使用scanf("%d", &amp;n); int a1[n];? (C99 中的 VLA)
    • 那些malloc应该被释放。
    • @chux 正是我
    • @3kt 有关系吗?
    【解决方案4】:

    在最后一个循环中添加以下代码。

    for (j=0, i=0; i

    a2[j++] = a1[i];
    a2[j++] = 0;
    

    }

    【讨论】:

      【解决方案5】:

      您必须将索引乘以 2:

      for(i = 0; i < n; ++i) { //copies and sets the arrays the same
          a2[2 * i] = a1[i];
      }
      

      不仅如此,a2 的奇数索引元素应设置为零。您可以在循环中显式执行此操作:

      for(i = 0; i < n; ++i) a2[2 * i + 1] = 0;
      

      但更简单的是先用零初始化数组:

      int a2[2 * n] = {0};
      

      偶数元素稍后将被a1 的元素覆盖。

      【讨论】:

        猜你喜欢
        • 2021-12-17
        • 1970-01-01
        • 2020-08-23
        • 2019-09-30
        • 1970-01-01
        • 1970-01-01
        • 2017-06-18
        • 2017-01-15
        • 1970-01-01
        相关资源
        最近更新 更多