如何创建100个整数的数组,其中每个元素的每个元素都是其c语言的索引
我想为每个数组分配其索引,这是我的代码。
#include <stdio.h>
#include <cs50.h>
int main(void) {
int length = get_int("How many arrys do you need? ");
int s[length];
for (int i = 0; i < length; i++) {
s[i] = i;
printf("s[i] = %d\n", i);
}
}
运行时
s[i] = 0
s[i] = 1
,我想在代码中替换i
,其索引号如下:
s[0] = 0
s[1] = 1
I want to assign every array with its index and this is my code.
#include <stdio.h>
#include <cs50.h>
int main(void) {
int length = get_int("How many arrys do you need? ");
int s[length];
for (int i = 0; i < length; i++) {
s[i] = i;
printf("s[i] = %d\n", i);
}
}
when run
s[i] = 0
s[i] = 1
so I want replace i
in the code with its index number like this:
s[0] = 0
s[1] = 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码在数组的初始化方面很好。
printf
语句应更改以打印索引和存储在索引上的值:这是一个修改版本:
Your code is fine regarding the initialization of the array.
The
printf
statement should be changed to print both the index and the value stored at the index:Here is a modified version: