gpt4 book ai didi

c - 如何使用for循环初始化数组?

转载 作者:行者123 更新时间:2023-11-30 21:41:39 25 4
gpt4 key购买 nike

我希望有人能帮助我。我有一个作业问题,要求声明一个可以存储 7 个 int 值的数组。显示 2 种不同的方法来初始化数组中的单元格,以便它们都包含值 65。其中一种技术必须使用循环。

我找到了一种无需循环即可实现的方法,但是有人可以告诉我一种使用循环来实现的方法吗?谢谢!

最佳答案

以下代码即可:

#include <stdio.h>

#define MAX_SIZE 7

int main()
{
int arr[MAX_SIZE];

for (int i = 0; i < MAX_SIZE; ++i)
arr[i] = 65;

// Verify array output :
for (int i = 0; i < MAX_SIZE; ++i)
printf("%d\n", arr[i]);

return 0;
}

关于c - 如何使用for循环初始化数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23319519/

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