gpt4 book ai didi

c - c中不同大小的数组相减

转载 作者:行者123 更新时间:2023-11-30 14:45:54 25 4
gpt4 key购买 nike

void function(int first[], int second[], int third[]) {
int i;
for(i=0;i<64;i++) {
third[i] = first[i] - second[i];
}
}

我想从第一个数组中减去第二个数组。第一个包含 32 个数字,第二个包含 13 个数字。对于前 13 个数字来说效果很好。一旦第二个数组“用完”元素,我想使用第二个数组开头的数字。所以我想从第一个数组的第 14 个元素中减去第二个数组的第一个元素,依此类推...我怎样才能实现它?

最佳答案

你可以使用%从数组的长度中获取索引的余数,这样,你就可以循环遍历第二个数组了!我更改了您的代码以按照您的要求进行

// get the length of array before you pass it to the function like this:
// int second_len = sizeof(second) / sizeof(second[0]);
void function(int first[], int second[], int third[], int second_len) {
int i;
for(i=0;i<64;i++) {
third[i] = first[i] - second[i % second_len];
}
}

关于c - c中不同大小的数组相减,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52682044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com