【问题标题】:How to generate an array of a specific value with given input array in C code如何在C代码中使用给定的输入数组生成特定值的数组
【发布时间】:2021-11-04 13:10:51
【问题描述】:

标题可能有点混乱,所以我将在这里更详细地解释我的目标; 我想编写一个代码,该代码采用 x 个数字的输入数组

index_x = [0,0,1,0,1,0,0,0,1,0,0,1,0];

然后我想编写一个逻辑,它将生成一个索引为所有 1 的数组

index_ones = [3,5,9,12];

从 Jacon 略微修改的当前代码:

编辑 1:

#include <stdio.h>

int index_x[] = {0,0,1,0,1,0,0,0,1,0,0,1,0};      // any number of elements
int len = 12;

int main(void) {

    int arr[len];
    int j = 0;

    for (int i = 0; i < len; i++)
        if (index_x[i])
            arr[j++] = i;                     // save and advance j only if value is "1"

    for (int i = 0; i < j; i++)               // only print below j !
        printf("%d\n", arr[i]);
}

输出:

2
4
8
11

从这个输出中,我想生成另一个数组,它是这些元素之间的差异。在这种情况下,新数组将是 {2,4,3}。

编辑 2:我将把它移到另一个线程,因为讨论现在已经从一个问题转移到另一个问题。不想让未来的用户复杂化。

【问题讨论】:

  • 你知道怎么写循环吗?你知道如何遍历数组吗?您能否编写将 1 复制到另一个数组 ([1,1,1,1]) 的代码?您如何用纸和铅笔执行此操作?
  • 从一个类似但更简单的问题开始。你能遍历并打印index_x 中的每个元素吗?您可以循环并使用索引打印它们吗?
  • 'arr[j++] = i + 1;' ?
  • @tstanisl 是的,可行,但我意识到我不必为整体范围这样做,所以我已经把它划掉了
  • @tstanisl 是的。或者稍后更正:printf("%d\n", ++arr[i]);(少两个空格,但隐藏在 printf() 中)。但我看到 Q 又变了!

标签: arrays c indexing element


【解决方案1】:

由于您不知道需要多少索引,因此您需要动态分配内存。您还需要记住索引的数量

struct index
{
    size_t size;
    size_t indexes[];
};


struct index *addIndex(struct index *index, size_t pos)
{
    size_t new_size = index ? index -> size + 1 : 1;

    index = realloc(index, sizeof(*index) + new_size * sizeof(index -> indexes[0]));
    if(index)
    {
        index -> size = new_size;
        index -> indexes[new_size - 1] = pos;
    }
    return index;
}

struct index *buildIndex(int *arr, size_t arr_size, int val)
{
    struct index *index = NULL, *tmp; 

    for(size_t pos = 0; pos < arr_size; pos++)
    {
        if(arr[pos] == val) 
        {
            tmp = addIndex(index, pos);
            if(tmp) index = tmp;
            else { /* error handling */ }
        }
    }
    return index;
}

【讨论】:

  • 如果我假设输入数组总是一个 128 布尔数组,我就不必动态分配内存了?
  • @CodingCurry 我不明白这个问题您将根据需要分配尽可能多的内存(在所有元素为零的边界情况下,您将不会分配任何内存)
【解决方案2】:

查找index_x[] 的数组元素计数。

int index_x[] = {0,0,1,0,1,0,0,0,1,0,0,1,0}; 
...
// e.g. 13, not 12 as implied with int len = 12;
size_t index_x_count = sizeof index_x / sizeof index_x[0]; 

然后通过index_x[] 查找个数

size_t ones = 0;
for (size_t i = 0; i < index_x_count; i++) {
   if (index_x[i] == 1) {
     ones++; 
  }
}

现在我们知道“生成一个索引全为 1 的数组”所需的大小

// Error check, can't have array size 0
if (ones == 0) Handle_patholocial_case();
// We should avoid forming huge arrays.  10000 is arbitrary.
// When large, rather than form _arrays_, allocate memory (not shown).
if (ones > 10000) Handle_patholocial_case();
// Form the array
int arr[ones];

size_t a = 0;
for (size_t i = 0; i < ones; i++) {
   if (index_x[i] == 1) {
    arr[a++] = i; 
  }
}

生成另一个数组,即这些元素之间的差异。

// Error check for at least one difference, can't have array size 0
if (ones <= 1) Handle_patholocial_case();
// Form the array
size_t diff_count = ones - 1;
int diff[diff_count];

for (size_t d = 0; d < diff_count; d++) {
  diff[a] = arr[d+1] - arr[d]; 
}

diff[]做点什么

for (size_t d = 0; d < diff_count; d++) {
  printf("%d ", diff[d]);
}

【讨论】:

  • 我无法理解这一点。我正在使用我发布的代码中的变量,但应用你提到的逻辑。这背后的原因是什么:size_t diff_count = one - 1?
  • @CodingCurry,在 4 个 int 的数组中,比如 { 2, 4, 8, 11},有多少不同?
  • 有2个不同
  • @CodingCurry 嗯,4-2、8-4、11-8 看起来有 3 个不同。
猜你喜欢
  • 2018-05-31
  • 2013-04-19
  • 1970-01-01
  • 1970-01-01
  • 2021-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多